我有一个创建线程的应用程序,它将监听传入的连接。主线程将做其他事情。
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 做什么?
最佳答案
您的应用程序在线程有机会完成其工作之前终止(通过离开 main())。一旦实现等待连接循环,问题就会得到解决。因此,无需执行任何操作。
关于c++ - Boost Thread - 创建一个没有 join() 的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8169723/