I am trying to use the boost library to create 3 threads of execution, on Visual Studio 2019. The 3 thread functions each has a while(1) loop, to keep executing continuously.
However, when I execute the program, I see that only the first thread is executed (and a breakpoint is hit). So I understand that the first thread is getting created, and execution remains inside the first function's while(1) loop, the remaining 2 threads are not executed (breakpoints are NOT hit).
In such a case, how do I modify code, to get all 3 threads running?
Part of the code snippet used is below:
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();
}
Just postpone joining the threads:
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();
Consider using a thread_group
.