Java standard library comes with CountDownLatch which is synchronization aid which allows one or more threads to wait on until a set of operations being performed by other threads completes.
For example, let one thread input a string to some queues (one or two queues) and wait for completion of two operations, which are:
We may achieve similar functionalities using Posix thread mutexes and condition variables (Threading libraries other than pthread will work as well). But as CountDownLatch is an useful and frequently used patterns, so it will be nice to have a library for the same.
I implemented a CountDownLatch class for C++ in less than 100 lines of code which provides similar functionalities as its Java counterpart. The code is available in Github(countdownlatch). May be useful for you as well :):):)...........
Below is a sample program demonstrating the use of countdownlatch:
For example, let one thread input a string to some queues (one or two queues) and wait for completion of two operations, which are:
- Calculating the hashcode of the string
- Finding the count of unique chars in the string
We may achieve similar functionalities using Posix thread mutexes and condition variables (Threading libraries other than pthread will work as well). But as CountDownLatch is an useful and frequently used patterns, so it will be nice to have a library for the same.
I implemented a CountDownLatch class for C++ in less than 100 lines of code which provides similar functionalities as its Java counterpart. The code is available in Github(countdownlatch). May be useful for you as well :):):)...........
Below is a sample program demonstrating the use of countdownlatch:
#include <unistd.h> #include <thread> #include <vector> #include <iostream> #include <countdownlatch.hpp> void fun(clatch::countdownlatch *cl) { cl->await(); std::cout << "Wait is over " << std::endl; } int main() { auto cl = new clatch::countdownlatch(10); int i = 0; std::vector<std::thread*> ts; while (i++ < 2) { std::thread *t = new std::thread(fun, cl); ts.push_back(t); } i = 0; while (i++ < 10) { sleep(1); cl->count_down(); } i = 0; while (i < 2) { ts[i++]->join(); } i = 0; while (i < 2) { delete ts[i++]; } delete cl; return 0; }
So, in other words, you are actually implementing a 'barrier' mechanism ?
ReplyDeleteNo.. It's very similar to the countdown latch that Java provides.
Delete