繁体   English   中英

C++ 条件变量表示分离线程执行停止的结束

[英]C++ Condition variable to signal end of detached thread execution stalls

我有一些代码,我正在处理产生分离线程的地方,做一些工作,然后应该等待来自main()的信号,然后再将另一个信号发送回 main,指示线程已退出。

我对条件变量相当陌生,但是我之前使用过一些多线程代码。 (主要是互斥体。)

这是我试图实现的,但它的行为不像我预期的那样。 (可能我误解了什么。)

这背后的想法是将包含两个标志的结构传递给每个分离的线程。 第一个标志表示main()表示“可以退出,并放下线程函数的结尾”。 第二个标志由线程本身设置,并向main()发出线程确实退出的信号。 (这只是为了确认来自main()的信号可以正常接收并发送回来。)

#include <cstdlib> // std::atoi
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <future>
#include <condition_variable>
#include <mutex>

struct ThreadStruct
{

    int id;

    std::condition_variable cv;
    std::mutex m;

    int ok_to_exit;
    int exit_confirm;

};



void Pause()
{
    std::cout << "Press enter to continue" << std::endl;
    std::cin.get();
}


void detachedThread(ThreadStruct* threadData)
{
    std::cout << "START: Detached Thread " << threadData->id << std::endl;
    
    // Performs some arbitrary amount of work.
    for(int i = 0; i < 100000; ++ i);

    std::cout << "FINISH: Detached thread " << threadData->id << std::endl;
    
    std::unique_lock<std::mutex> lock(threadData->m);
    std::cout << "WAIT: Detached thread " << threadData->id << std::endl;
    threadData->cv.wait(lock, [threadData]{return threadData->ok_to_exit == 1;});
    std::cout << "EXIT: Detached thread " << threadData->id << std::endl;
    threadData->exit_confirm = 1;

}

int main(int argc, char** argv)
{
    
    int totalThreadCount = 1;

    ThreadStruct* perThreadData = new ThreadStruct[totalThreadCount];
    std::cout << "Main thread starting " << totalThreadCount << " thread(s)" << std::endl;

    for(int i = totalThreadCount - 1; i >= 0; --i)
    {
        perThreadData[i].id = i;
        perThreadData[i].ok_to_exit = 0;
        perThreadData[i].exit_confirm = 0;
                
        std::thread t(detachedThread, &perThreadData[i]);
        t.detach();
            
    }


    for(int i{0}; i < totalThreadCount; ++i)
    {
        ThreadStruct *threadData = &perThreadData[i];
        
        std::cout << "Waiting for lock - main() thread" << std::endl;
        std::unique_lock<std::mutex> lock(perThreadData[i].m);
        std::cout << "Lock obtained - main() thread" << std::endl;
        perThreadData[i].cv.wait(lock);

        threadData->ok_to_exit = 1;

        // added after comment from Sergey
        threadData->cv.notify_all(); 

        std::cout << "Done - main() thread" << std::endl;
        
    }

    for(int i{0}; i < totalThreadCount; ++i)
    {
        std::size_t thread_index = i;
        ThreadStruct& threadData = perThreadData[thread_index];
        
        std::unique_lock<std::mutex> lock(threadData.m);
        std::cout << "i=" << i << std::endl;
        int &exit_confirm = threadData.exit_confirm;
        threadData.cv.wait(lock, [exit_confirm]{return exit_confirm == 1;});
        std::cout << "i=" << i << " finished!" << std::endl;
    }

    Pause();

    return 0;
}

这运行到该行:

WAIT: Detached thread 0

但分离的线程永远不会退出。 我做错了什么?

编辑:进一步的实验——这有帮助吗?

我认为通过删除一个步骤来简化事情可能会有所帮助。 在下面的示例中, main()不会分离线程发出信号,它只是等待来自分离线程的信号。

但同样,这段代码挂起 - 在打印DROP ...这意味着分离的线程正常退出,但main()不知道它。

#include <cstdlib> // std::atoi
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <future>
#include <condition_variable>
#include <mutex>

struct ThreadStruct
{

    int id;

    std::condition_variable cv;
    std::mutex m;

    int ok_to_exit;
    int exit_confirm;

};



void Pause()
{
    std::cout << "Press enter to continue" << std::endl;
    std::cin.get();
}


void detachedThread(ThreadStruct* threadData)
{
    std::cout << "START: Detached Thread " << threadData->id << std::endl;
    
    // Performs some arbitrary amount of work.
    for(int i = 0; i < 100000; ++ i);

    std::cout << "FINISH: Detached thread " << threadData->id << std::endl;
    
    std::unique_lock<std::mutex> lock(threadData->m);
    
    std::cout << "EXIT: Detached thread " << threadData->id << std::endl;
    threadData->exit_confirm = 1;

    threadData->cv.notify_all();
    std::cout << "DROP" << std::endl;

}

int main(int argc, char** argv)
{
    
    int totalThreadCount = 1;

    ThreadStruct* perThreadData = new ThreadStruct[totalThreadCount];
    std::cout << "Main thread starting " << totalThreadCount << " thread(s)" << std::endl;

    for(int i = totalThreadCount - 1; i >= 0; --i)
    {
        perThreadData[i].id = i;
        perThreadData[i].ok_to_exit = 0;
        perThreadData[i].exit_confirm = 0;
                
        std::thread t(detachedThread, &perThreadData[i]);
        t.detach();
            
    }

    for(int i{0}; i < totalThreadCount; ++i)
    {
        std::size_t thread_index = i;
        ThreadStruct& threadData = perThreadData[thread_index];
        
        std::cout << "Waiting for mutex" << std::endl;
        std::unique_lock<std::mutex> lock(threadData.m);
        std::cout << "i=" << i << std::endl;
        int &exit_confirm = threadData.exit_confirm;
        threadData.cv.wait(lock, [exit_confirm]{return exit_confirm == 1;});
        std::cout << "i=" << i << " finished!" << std::endl;
    }

    Pause();

    return 0;
}

您的 lambda 正在按值捕获,因此它永远不会看到对exit_confim所做的更改。

而是通过引用捕获:

int& exit_confirm = threadData.exit_confirm;
threadData.cv.wait(lock, [&exit_confirm] { return exit_confirm == 1; });
//                        ^
//                        | capture by-reference

您还需要delete[]new[]这样做

delete[] ThreadStruct;

当你完成了struct s。


我还注意到释放后的一些堆使用,但是当我对代码进行一些简化时,这种情况神奇地消失了。 我没有进一步调查。

一些建议:

  • 将代码移动到处理ThreadStruct成员变量和锁的ThreadStruct class 中。 它通常使阅读和维护更简单。
  • 删除未使用的变量和标题。
  • 不要使用new[] / delete[] 对于此示例,您可以改用std::vector<ThreadStruct>
  • 根本不要detach() - 我没有在下面做任何事情,但我建议使用join() (在附加的线程上)进行最终同步。 这就是它的用途。
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>

struct ThreadStruct {
    int id;

    // move this function into the ThreadStruct class
    void detachedThread() {
        std::cout << "START: Detached Thread " << id << std::endl;
        // Performs some arbitrary amount of work (optimized away here)
        std::cout << "FINISH: Detached thread " << id << std::endl;

        std::lock_guard<std::mutex> lock(m);

        std::cout << "EXIT: Detached thread " << id << std::endl;
        exit_confirm = 1;

        cv.notify_all();
        std::cout << "DROP" << std::endl;
    }

    // add support functions instead of doing these things in your normal code
    void wait_for_exit_confirm() {
        std::unique_lock<std::mutex> lock(m);
        cv.wait(lock, [this] { return exit_confirm == 1; });
    }

    void spawn_detached() {
        std::thread(&ThreadStruct::detachedThread, this).detach();
    }

private:
    std::condition_variable cv;
    std::mutex m;

    int exit_confirm = 0;           // initialize
};

有了以上内容, main变得更干净了:

int main() {
    int totalThreadCount = 1;

    std::vector<ThreadStruct> perThreadData(totalThreadCount);

    std::cout << "Main thread starting " << perThreadData.size() << " thread(s)\n";

    int i = 0;
    for(auto& threadData : perThreadData) {
        threadData.id = i++;
        threadData.spawn_detached();
    }

    for(auto& threadData : perThreadData) {
        std::cout << "Waiting for mutex" << std::endl;
        std::cout << "i=" << threadData.id << std::endl;
        threadData.wait_for_exit_confirm();
        std::cout << "i=" << threadData.id << " finished!" << std::endl;
    }

    std::cout << "Press enter to continue" << std::endl;
    std::cin.get();
}

为了将来的兴趣:修复了问题中发布的原始 MWE。 有两个问题

  • 未通过引用捕获 lambda 中的局部变量(请参阅其他答案)

  • 1 太多的wait()调用

     #include <cstdlib> // std::atoi #include <iostream> #include <thread> #include <vector> #include <random> #include <future> #include <condition_variable> #include <mutex> struct ThreadStruct { int id; std::condition_variable cv; std::mutex m; int ok_to_exit; int exit_confirm; }; void Pause() { std::cout << "Press enter to continue" << std::endl; std::cin.get(); } void detachedThread(ThreadStruct* threadData) { std::cout << "START: Detached Thread " << threadData->id << std::endl; // Performs some arbitrary amount of work. for (int i = 0; i < 100000; ++i); std::cout << "FINISH: Detached thread " << threadData->id << std::endl; std::unique_lock<std::mutex> lock(threadData->m); std::cout << "WAIT: Detached thread " << threadData->id << std::endl; threadData->cv.wait(lock, [&threadData]{return threadData->ok_to_exit == 1;}); std::cout << "EXIT: Detached thread " << threadData->id << std::endl; threadData->exit_confirm = 1; threadData->cv.notify_all(); std::cout << "DROP" << std::endl; } int main(int argc, char** argv) { int totalThreadCount = 1; ThreadStruct* perThreadData = new ThreadStruct[totalThreadCount]; std::cout << "Main thread starting " << totalThreadCount << " thread(s)" << std::endl; for (int i = totalThreadCount - 1; i >= 0; --i) { perThreadData[i].id = i; perThreadData[i].ok_to_exit = 0; perThreadData[i].exit_confirm = 0; std::thread t(detachedThread, &perThreadData[i]); t.detach(); } for(int i{0}; i < totalThreadCount; ++ i) { ThreadStruct *threadData = &perThreadData[i]; std::cout << "Waiting for lock - main() thread" << std::endl; std::unique_lock<std::mutex> lock(perThreadData[i].m); std::cout << "Lock obtained - main() thread" << std::endl; //perThreadData[i].cv.wait(lock, [&threadData]{return threadData->ok_to_exit == 1;}); std::cout << "Wait complete" << std::endl; threadData->ok_to_exit = 1; threadData->cv.notify_all(); std::cout << "Done - main() thread" << std::endl; } for (int i{ 0 }; i < totalThreadCount; ++i) { std::size_t thread_index = i; ThreadStruct& threadData = perThreadData[thread_index]; std::cout << "Waiting for mutex" << std::endl; std::unique_lock<std::mutex> lock(threadData.m); std::cout << "i=" << i << std::endl; int& exit_confirm = threadData.exit_confirm; threadData.cv.wait(lock, [&exit_confirm] {return exit_confirm == 1; }); std::cout << "i=" << i << " finished:" << std:;endl; } Pause(); return 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM