Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2002-09-20 03:13:09


Output to cout is thread-unsafe in
libs/thread/example/condition.cpp:

// skiped

void sender() {
    int n = 0;
    while (n < 100) {
       buf.send(n);
       std::cout << "sent: " << n << std::endl;
       ++n;
    }
    buf.send(-1);
}

void receiver() {
    int n;
    do {
       n = buf.receive();
       std::cout << "received: " << n << std::endl;
    } while (n != -1); // -1 indicates end of buffer
}

int main(int, char*[])
{
    boost::thread thrd1(&sender);
    boost::thread thrd2(&receiver);
    thrd1.join();
    thrd2.join();
    return 0;
}

--
Alexander Nasonov,
e-mail: alnsn REMOVE SPAM PROTECTION @ mail . ru

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk