我正在尝试使用Boost库在Visual Studio 2019上创建3个执行线程。这3个线程函数每个都有一个while(1)循环,以保持连续执行。
但是,当我执行程序时,我看到只执行了第一个线程(并且命中了断点)。因此,我知道正在创建第一个线程,并且执行仍保留在第一个函数的while(1)循环内,其余两个线程未执行(未命中断点)。
在这种情况下,如何修改代码以使所有3个线程都运行?
使用的部分代码段如下:
myfunction()
{
// Some code here..
boost::thread t(&myclass::kafkaSvc1ProducerThread, this, rk);
t.join();
boost::thread t2(&myclass::kafkaSvc2ProducerThread, this, rk);
t2.join();
boost::thread t3(&myclass::kafkaSvc3ProducerThread, this, rk);
t3.join();
// Some code here..
}
func1(1)
{
while(1)
{
// Some code here...
}
}
void kafkaSvc1ProducerThread()
{
func1();
}
func2(1)
{
while(1)
{
// Some code here...
}
}
void kafkaSvc2ProducerThread()
{
func2();
}
func3(1)
{
while(1)
{
// Some code here...
}
}
void kafkaSvc3ProducerThread()
{
func3();
}
只是推迟加入线程:
boost::thread t(&myclass::kafkaSvc1ProducerThread, this, rk);
boost::thread t2(&myclass::kafkaSvc2ProducerThread, this, rk);
boost::thread t3(&myclass::kafkaSvc3ProducerThread, this, rk);
t.join();
t2.join();
t3.join();
考虑使用thread_group
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句