dispenso 1.6.2
A library for task parallelism
Loading...
Searching...
No Matches
Synchronization

Synchronization primitives. More...

Files

file  distributed_rw_lock.h
 
file  latch.h
 
file  rw_lock.h
 

Detailed Description

Synchronization primitives.

Locks, latches, and other synchronization primitives for coordinating between threads.

Example: Latch for Thread Coordination

#include <dispenso/latch.h>
dispenso::Latch latch(4); // Wait for 4 threads
// Every participant must be running at once, so give each its own thread.
dispenso::TaskSet tasks(pool);
for (int i = 0; i < 4; ++i) {
tasks.schedule([&]() {
// Do initialization work...
latch.arrive_and_wait(); // Synchronize before continuing
// All threads proceed together from here
});
}
tasks.wait();

Tasks that block on a Latch occupy their thread while they wait. Run them on a pool with a thread for each, not on the global pool or inside parallel_for, which may have fewer threads than participants or run several on one thread.

Example: RWLock for Reader/Writer Access

std::vector<int> sharedData;
// Reader (multiple allowed concurrently)
{
std::shared_lock<dispenso::RWLock> guard(lock);
int value = sharedData[0];
}
// Writer (exclusive access)
{
std::unique_lock<dispenso::RWLock> guard(lock);
sharedData.push_back(42);
}