Boost Thread Synchronization
Posted
by Dave18
on Stack Overflow
See other posts from Stack Overflow
or by Dave18
Published on 2010-05-23T18:06:14Z
Indexed on
2010/05/23
18:11 UTC
Read the original article
Hit count: 424
I don't see synchronized output when i comment the the line wait(1)
in thread()
. can I make them run at the same time (one after another) without having to use 'wait(1)'?
#include <boost/thread.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
boost::mutex mutex;
void thread()
{
for (int i = 0; i < 100; ++i)
{
wait(1);
mutex.lock();
std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
mutex.unlock();
}
}
int main()
{
boost::thread t1(thread);
boost::thread t2(thread);
t1.join();
t2.join();
}
© Stack Overflow or respective owner