【发布时间】:2011-11-17 15:26:39
【问题描述】:
我有一个创建线程的应用程序,它将监听传入的连接。而主线程会做其他事情。
boost::mutex mutex;
void
ThreadFunction(int port, int(*callbackFunc)(int, int))
{
mutex.lock();
std::cout << "Cannot get to this point" << std::endl;
mutex.unlock();
Application app;
app.run(port, callbackFunc);
}
void
Init(int port, int(*callbackFunc)(int, int))
{
std::cout << callbackFunc(1,1) << std::endl;
boost::thread t(boost::bind(&ThreadFunction, port, callbackFunc));
}
int
main(){
int port = 2340;
Init(port, *callbackfunction);
return 0;
}
我遇到的问题是它从不访问 std::cout << "Cannot get to this point" << std::endl; 但是,如果我在创建线程后调用 join() ,它工作得很好,但它会阻塞应用程序。
线程调用ThreadFunction我需要做什么?
【问题讨论】:
-
只是一个补充,我相信标准规定
terminate()将在std::thread被破坏时被调用。 Boost 的实现改为调用detach(),但它可能会在将来更改以符合标准,因此让它超出范围可能不是一个好主意。
标签: c++ multithreading boost boost-thread