【发布时间】:2015-01-05 11:16:56
【问题描述】:
我必须实现提升线程间通信。考虑以下代码:
#include <boost/thread/thread.hpp>
#include <Windows.h>
void threadA()
{
while(true)
{
std::cout << "From thread A" << std::endl;
Sleep(3000); //pretend to do the work
}
}
void threadB()
{
while(true)
{
std::cout << "From thread B" << std::endl;
Sleep(3000); //pretend to do the work
}
}
int main()
{
boost::thread *th1 = new boost::thread(&threadA);
boost::thread *th2 = new boost::thread(&threadB);
th1->join();
th2->join();
delete th1;
delete th2;
}
如果我运行上面的代码,它将产生两个线程。我想要做的是启动threadA 并向threadB 发送一些消息,收到后将启动线程。或者更一般地说,如果这两个线程独立运行,我该如何处理通信?
【问题讨论】:
标签: c++ multithreading boost