栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++多线程-stop

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++多线程-stop

condition_variable_any类是 std::condition_variable 的泛化。 虽然std::condition_variable 仅适用于 std::unique_lock,但 condition_variable_any 可以对满足 BasicLockable 要求的任何锁进行操作。

示例
#include 
#include 
#include 
#include 
#include 



using namespace std::chrono_literals;


void Xworker(std::stop_token stoken, int argX)
{
    std::cout << "thread-" << std::this_thread::get_id() << " argX = " << argX << "n";

    while (true) {
        std::this_thread::sleep_for(1s);
        if (stoken.stop_requested()) {
            std::cout << "thread-" << std::this_thread::get_id() << " Xworker is requested to stopn";
            return;
        }
        std::cout << "thread-" << std::this_thread::get_id() << " Xworker goes back to sleepn";
    }
}

int main()
{
    unsigned int n = std::thread::hardware_concurrency(); // 当前硬件支持线程并发数
    std::cout << n << " concurrent threads are supported.n";

    // A sleepy worker thread
    std::jthread sleepy_worker([](std::stop_token stoken) {
       // for (int i = 10; i; --i) {
        while (true) {
            std::this_thread::sleep_for(100ms);
            if (stoken.stop_requested()) {
                std::cout << "thread-" << std::this_thread::get_id() << " Sleepy worker is requested to stopn";
                return;
            }
            std::cout << "thread-" << std::this_thread::get_id() << " Sleepy worker goes back to sleepn";
        }
        });

    // A waiting worker thread
    // The condition variable will be awoken by the stop request.
    std::jthread waiting_worker([](std::stop_token stoken) {
        std::mutex mutex;
        std::unique_lock lock(mutex);
        std::condition_variable_any().wait(lock, stoken,  [&stoken] {
                if (stoken.stop_requested()) {
                    std::cout << "thread-" << std::this_thread::get_id() << " Waiting worker wakeupn";
                    return true;
                }
                else {
                    std::cout << "thread-" << std::this_thread::get_id() << " Waiting worker sleep...n";
                    return false;
                }
            });

            //if (stoken.stop_requested()) {
            //    std::cout << "thread-" << std::this_thread::get_id() << " Waiting worker is requested to stopn";
            //    return;
            //}
        });

    std::jthread xworker(Xworker, 2);

    // std::jthread::request_stop() can be called explicitly:
    std::this_thread::sleep_for(1s);
    std::cout << "Requesting stop of sleepy workern";
    sleepy_worker.request_stop();
    sleepy_worker.join();
    std::cout << "Sleepy worker joinedn";

    std::this_thread::sleep_for(5s);
    // Or automatically using RAII:
    // waiting_worker's destructor will call request_stop()
    // and join the thread automatically.
}
运行结果

16 concurrent threads are supported.
thread-1616 argX = 2
thread-2248 Waiting worker sleep...
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
thread-24232 Sleepy worker goes back to sleep
Requesting stop of sleepy worker
thread-1616 Xworker goes back to sleep
thread-24232 Sleepy worker is requested to stop
Sleepy worker joined
thread-1616 Xworker goes back to sleep
thread-1616 Xworker goes back to sleep
thread-1616 Xworker goes back to sleep
thread-1616 Xworker goes back to sleep
thread-1616 Xworker goes back to sleep
thread-1616 Xworker is requested to stop
thread-2248 Waiting worker wakeup

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008889.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号