I am playing around with Cilk and I am having an issue with printing synchronously. Printing is slow, so it's hard to keep the prints synchronous. For example...
void ftn(int x)
{
if (x % 2 == 0)
{
std::cout << "printing.. " << x << std::endl;
}
else
{
cilk_spawn ftn(x/2);
cilk_spawn ftn(x++);
cilk_spawn ftn(x*x);
cilk_sync;
}
}
For the most part, printing is ok. However, it occasionally goes out of sync and a second print statement from another thread will start in the middle of one thread's print statement.
How do you make C/Cilk printing threadsafe? How can I ensure that these stay synchronous?