我使用的是一个简单的线程池 -
template<typename T>
class thread_safe_queue // thread safe worker queue.
{
private:
std::atomic<bool> finish;
mutable std::mutex mut;
std::queue<T> data_queue;
std::condition_variable data_cond;
public:
thread_safe_queue() : finish{ false }
{}
~thread_safe_queue()
{}
void setDone()
{
finish.store(true);
data_cond.notify_one();
}
void push(T new_value)
{
std::lock_guard<std::mutex> lk(mut);
data_queue.push(std::move(new_value));
data_cond.notify_one();
}
void wait_and_pop(T& value)
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this]
{
return false == data_queue.empty();
});
if (finish.load() == true)
return;
value = std::move(data_queue.front());
data_queue.pop();
}
bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
};
//Thread Pool
class ThreadPool
{
private:
std::atomic<bool> done;
unsigned thread_count;
std::vector<std::thread> threads;
public:
explicit ThreadPool(unsigned count = 1);
ThreadPool(const ThreadPool & other) = delete;
ThreadPool& operator = (const ThreadPool & other) = delete;
~ThreadPool()
{
done.store(true);
work_queue.setDone();
// IF thread is NOT marked detached and this is uncommented the worker threads waits infinitely.
//for (auto &th : threads)
//{
// if (th.joinable())
// th.join();
// }
}
void init()
{
try
{
thread_count = std::min(thread_count, std::thread::hardware_concurrency());
for (unsigned i = 0; i < thread_count; ++i)
{
threads.emplace_back(std::move(std::thread(&ThreadPool::workerThread, this)));
threads.back().detach();
// here the problem is if i dont mark it detatched thread infinitely waits for condition.
// if i comment out the detach line and uncomment out comment lines in ~ThreadPool main threads waits infinitely.
}
}
catch (...)
{
done.store(true);
throw;
}
}
void workerThread()
{
while (true)
{
std::function<void()> task;
work_queue.wait_and_pop(task);
if (done == true)
break;
task();
}
}
void submit(std::function<void(void)> fn)
{
work_queue.push(fn);
}
};
用法如下:
struct start
{
public:
ThreadPool::ThreadPool m_NotifPool;
ThreadPool::ThreadPool m_SnapPool;
start()
{
m_NotifPool.init();
m_SnapPool.init();
}
};
int main()
{
start s;
return 0;
}
我在visual studio 2013上运行此代码。问题是主线程何时退出。程序崩溃了。它抛出异常。 请帮我解决我做错了什么?如何正确停止工作线程?我花了很长时间但仍在弄清问题是什么。
提前感谢您的帮助。
答案 0 :(得分:1)
我不熟悉c ++中的线程,但是在C中使用了线程。在C中实际发生的是当你从主线程创建子线程时,你必须停止主线程,直到子节点完成。如果主要退出线程变成僵尸。我认为C不会在Zombies的情况下抛出异常。也许你因为这些僵尸而异常。尝试停止使用主体,直到孩子完成并看看它是否有效。
答案 1 :(得分:1)
当main
退出时,允许分离的线程继续运行,但是对象s
将被销毁。因此,当您的线程尝试访问对象s
的成员时,您将遇到UB。
有关您的问题的详细信息,请参阅此问题的已接听答案:What happens to a detached thread when main() exits?
答案 2 :(得分:0)
经验法则不是从主要分离线程,而是应用程序结束并加入所有线程的信号线程池。或按照What happens to a detached thread when main() exits?
中的回答