/usr/include/ossim/base/Barrier.h is in libossim-dev 2.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #ifndef ossimBarrier_HEADER
#define ossimBarrier_HEADER 1
#include <ossim/base/ossimConstants.h>
#include <mutex>
#include <condition_variable>
#include <atomic>
namespace ossim{
/**
* Barrier is a class used to block threads so we can synchronize and entry point.
*
* In this example we show how to block the threads so they all start at the
* same time when executing their work.
* Example:
*
* @code
* #include <ossim/base/Thread.h>
* #include <ossim/base/Barrier.h>
* int nThreads = 2;
* ossim::Barrier barrierStart(nThreads)
* // one more for main thread
* ossim::Barrier barrierFinished(nThreads+1);
*
* class TestThread : public ossim::Thread
* {
* public:
* TestThread():ossim::Thread(){}
* ~TestThread(){
* waitForCompletion();
* }
*
* protected:
* virtual void run()
* {
* barrierStart.block();
* for(int x =0 ; x < 10;++x){
* std::cout << "THREAD: " << getCurrentThreadId() << "\n";
* sleepInMilliSeconds(10);
* interrupt();
* }
* barrierFinished.block();
* }
* };
*
* int main(int agrc, char* argv[])
* {
* std::vector<std::shared_ptr<TestThread> > threads(nThreads);
* for(auto& thread:threads)
* {
* thread = std::make_shared<TestThread>();
* thread->start();
* }
* // block main until barrier enters their finished state
* barrierFinished.block();
*
* // you can also reset the barriers and run again
* barrierFinished.reset();
* barrierStart.reset();
* for(auto& thread:threads)
* {
* thread->start();
* }
* barrierFinished.block();
* }
*
* @endcode
*/
class OSSIM_DLL Barrier
{
public:
/**
* Constructor
*
* @param n is the number of threads you wish to block
*/
Barrier(ossim_int32 n);
/**
* Destructor will reset and release all blocked threads.
*/
~Barrier();
/**
* block will block the thread based on a wait condition. it will verify
* if the thread can be blocked by testing if the number
* of blocked threads is less than the total number to blocked threads. If
* the total is reached then all threads are notified and woken up and released
*/
void block();
/**
* Will reset the barrier to the original values.
* It will also release all blocked threads and wait for their release
* before resetting.
*/
void reset();
/**
* Will reset the barrier to a new block count.
* It will also release all blocked threads and wait for their release
* before resetting.
*
* @param maxCount is the max number of threads to block
*/
void reset(ossim_int32 maxCount);
ossim_int32 getMaxCount()const;
ossim_int32 getBlockedCount()const;
protected:
ossim_int32 m_maxCount;
ossim_int32 m_blockedCount;
std::atomic<ossim_int32> m_waitCount;
mutable std::mutex m_mutex;
std::condition_variable m_conditionalBlock;
/**
* Will be used for destructing and resetting.
* resetting should only happen in the main
* thread
*/
std::condition_variable m_conditionalWait;
};
}
#endif
|