首页 > 解决方案 > 加入 DLL_DETACH 中的线程

问题描述

我有一个注入进程的 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;
}

但是,这将导致我的程序在我退出时挂起。我必须通过任务管理器将其杀死。如果我分离线程,一切都会正常工作并干净地退出(无论我是在创建线程之后还是在析构函数中分离线程都没有关系)。

那么为什么当我加入线程时进程挂起?

标签: multithreadingc++11winapidll

解决方案


调用时会持有锁DllMain(),因此等待线程退出最终会递归调用DllMain()( THREAD_DETACHand PROCESS_DETACH) 并挂在该锁上。

进一步阅读:动态链接库最佳实践


推荐阅读