progschj / ThreadPool

A simple C++11 Thread Pool implementation
zlib License
7.63k stars 2.21k forks source link

Unexpected stuck thread #110

Open DeepLearnerYe opened 5 months ago

DeepLearnerYe commented 5 months ago

The thread is running well when the function is simple. However, I encountered a stuck thread using my own function. Here is a simple example.

// running perfectly good
thread_pool->enqueue([]()
{
while(1)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "thread running...." << std::endl;
} 
});
// I can see "myFunction() running". But my logic never starts to execute.
void myFunction()
{
std::cout << "myFunction() running" << std::endl;
    while(1)
    {
        // some logic...
    }
}

thread_pool->enqueue([]()
{
    myFunction();
});

I created a thread myself to run my function, and it succeeded. So, that should not be the problem of my funtion, maybe there's a bug or something in the threadpool?

sunbuny commented 3 months ago

sorry, I can not reproduce your error code in a win11 and msvc environment. Could you please use the godbolt to reproduce the error.