Hazelcast C++ Client
Public Member Functions | Friends | List of all members
hazelcast::client::ICountDownLatch Class Reference

ICountDownLatch is a backed-up distributed alternative to the java.util.concurrent.CountDownLatch java.util.concurrent.CountDownLatch. More...

#include <ICountDownLatch.h>

+ Inheritance diagram for hazelcast::client::ICountDownLatch:

Public Member Functions

bool await (long timeoutInMillis)
 Causes the current thread to wait until the latch has counted down to zero, an exception is thrown, or the specified waiting time elapses. More...
 
void countDown ()
 Decrements the count of the latch, releasing all waiting threads if the count reaches zero. More...
 
int getCount ()
 Returns the current count. More...
 
bool trySetCount (int count)
 Sets the count to the given value if the current count is zero. More...
 

Friends

class impl::HazelcastClientInstanceImpl
 

Detailed Description

ICountDownLatch is a backed-up distributed alternative to the java.util.concurrent.CountDownLatch java.util.concurrent.CountDownLatch.

ICountDownLatch is a cluster-wide synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

There are a few differences compared to the ICountDownLatch :

  1. the ICountDownLatch count can be re-set using trySetCount(int) after a countdown has finished but not during an active count. This allows the same latch instance to be reused.
  2. there is no await() method to do an unbound wait since this is undesirable in a distributed application: it can happen that for example a cluster is split or that the master and replica's all die. So in most cases it is best to configure an explicit timeout so have the ability to deal with these situations.

Member Function Documentation

◆ await()

bool hazelcast::client::ICountDownLatch::await ( long  timeoutInMillis)

Causes the current thread to wait until the latch has counted down to zero, an exception is thrown, or the specified waiting time elapses.

If the current count is zero then this method returns immediately with the value true.

If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happen:

  • The count reaches zero due to invocations of the countDown method;
  • This ICountDownLatch instance is destroyed;
  • The countdown owner becomes disconnected;
  • Some other thread interrupts the current thread; or
  • The specified waiting time elapses.

If the count reaches zero then the method returns with the value true.

If the countdown owner becomes disconnected while waiting then MemberLeftException will be thrown.

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting,

then InterruptedException is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

Parameters
timeoutInMillisthe maximum time to wait
Returns
true if the count reached zero and false if the waiting time elapsed before the count reached zero

◆ countDown()

void hazelcast::client::ICountDownLatch::countDown ( )

Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

If the current count is greater than zero then it is decremented. If the new count is zero:

  • All waiting threads are re-enabled for thread scheduling purposes

If the current count equals zero then nothing happens.

◆ getCount()

int hazelcast::client::ICountDownLatch::getCount ( )

Returns the current count.

Returns
current count

◆ trySetCount()

bool hazelcast::client::ICountDownLatch::trySetCount ( int  count)

Sets the count to the given value if the current count is zero.

The calling cluster member becomes the owner of the countdown and is responsible for staying connected to the cluster until the count reaches zero. If the owner becomes disconnected before the count reaches zero:

  • Count will be set to zero;
  • All awaiting threads will be thrown a MemberLeftException.

If count is not zero then this method does nothing and returns false.

Parameters
countthe number of times countDown must be invoked before threads can pass through await
Returns
true if the new count was set or false if the current count is not zero
Exceptions
IllegalArgumentExceptionif count is negative

The documentation for this class was generated from the following files: