How to print in a thread-safe way in C / Cilk?

I play with Cilk and I have a printing problem synchronously. Printing is slow, so it’s hard to keep synchronous prints. For instance...

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 fine. However, it sometimes fails, and a second print statement from another thread runs in the middle of one thread print report.

How do you do C / Cilk printing inline? How can I guarantee that they remain synchronous?

+3
source share
2 answers

- (, ), - cout . Cilk, .

, , , . "" - - , , .

+1

, . Cilk Plus , "" . , reducer_ostream:

cilk::reducer<cilk::op_ostream> hyper_cout(std::cout);
*hyper_cout << "Reducer cout:  ";

A - Cilk Plus.

+1

Source: https://habr.com/ru/post/1767666/


All Articles