我有一个注入到进程中的DLL,当进程终止时,我希望线程在DLL卸载时终止。
因此,我提出了以下建议:
// Wrapper around std::thread that notifies the task it should stop..
class TaskThread {
private:
std::mutex mutex;
std::thread thread;
std::atomic_bool stop;
std::function<void()> onStop;
public:
TaskThread(std::function<void(TaskThread*)> &&task,std::function<void()> &&onStop);
~TaskThread();
bool stopped();
};
TaskThread::TaskThread(std::function<void(TaskThread*)> &&task,std::function<void()> &&onStop) : onStop(onStop)
{
this->thread = std::thread([this,task]{
task(this);
});
}
TaskThread::~TaskThread()
{
//set stop to true..
std::unique_lock<std::mutex> lock(this->mutex);
this->stop = true;
lock.unlock();
//signal the task
onStop();
//join the thread..
this->thread.join();
}
bool TaskThread::stopped()
{
std::unique_lock<std::mutex> lock(this->mutex);
bool stopped = this->stop;
lock.unlock();
return stopped;
}
BOOL APIENTRY DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
static std::unique_ptr<TaskThread> task_thread;
static std::unique_ptr<Semaphore> semaphore;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
DisableThreadlibraryCalls(hinstDLL);
semaphore.reset(new Semaphore(0));
task_thread.reset(new TaskThread([&](TaskThread* thread){
while(thread && !thread->stopped()) {
if (!semaphore)
{
return;
}
semaphore->wait();
if (!thread || thread->stopped())
{
return;
}
runTask(); //execute some function
}
},[&]{
if (semaphore)
{
semaphore->signal();
}
}));
}
break;
case DLL_PROCESS_DetaCH:
{
task_thread.reset(); //delete the thread.. triggering the destructor
}
break;
}
return TRUE;
}
但是,这将导致我的程序在退出时挂起。我必须通过任务管理器将其杀死。如果我改为分离线程,则一切正常,并且可以干净地退出(在创建线程之后还是在析构函数中分离线程都没关系)。
那为什么在我加入线程时进程挂起?