Sample that uses a barrier that waits for threads.
The BarrierDemo
function does the following.
-
Uses the
nn::nlib::threading::Barrier::Init
function to set the number of threads to wait for in kNumThread
.
-
Starts
kNumThread
threads, and runs ThreadFunc
.
ThreadFunc
waits for other threads every time it displays to the console.
using ::nlib_ns::threading::Barrier;
const int kNumThread = 8;
Barrier g_barrier;
void ThreadFunc(void* p) {
int n = static_cast<int>(reinterpret_cast<intptr_t>(p));
g_barrier.Wait();
g_barrier.Wait();
g_barrier.Wait();
}
bool BarrierDemo() {
nlib_printf(
"%d threads run, and sync by a Barrier object.\n", kNumThread);
nlib_printf(
"The Barrier object can sync mutiple times.\n\n");
g_barrier.Init(kNumThread);
intptr_t i;
bool fatal = false;
for (i = 0; i < kNumThread; ++i) {
if (e != 0) {
fatal = true;
}
}
if (fatal) return false;
for (i = 0; i < kNumThread; ++i) {
}
return true;
}
static bool SampleMain(int, char**) { return BarrierDemo(); }
NLIB_MAINFUNC