lundi 4 novembre 2019

Joining a thread in DLL_DETACH

I have a DLL that is injected into a process and when the process terminates, I want the thread to be terminated as the DLL unloads.

Thus I came up with the following:

// 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;
}

However, this will cause my program to hang when I exit.. and I'll have to kill it via task-manager. If I detach the thread instead, everything works and exits cleanly (doesn't matter if I detach the thread right after creating it or within the destructor).

So why is it when I join the thread the process hangs?

Aucun commentaire:

Enregistrer un commentaire