• c++标准库std::thread测试一(流程控制)


    1.需求, 现在10个线程分别打印AAA... BBB... CCC...要求按顺序打印, 如图所示

    Plan1:

     1 #define THREAD_CNT 5
     2 
     3 std::condition_variable g_cvs[THREAD_CNT];
     4 std::condition_variable g_cv;
     5 std::mutex g_mtx;
     6 
     7 int g_curIndex = -1;
     8 
     9 void PrintChar(char ch)
    10 {
    11     int index = ch - 'A';
    12     int sleepMsTime = rand() % 2000;
    13     printf("%c ready
    ", ch);
    14     std::this_thread::sleep_for(std::chrono::milliseconds(sleepMsTime));
    15     printf("%c lock
    ", ch);
    16     std::unique_lock<std::mutex> lg(g_mtx);
    17     while (1)
    18     {
    19         g_cv.wait(lg);
    20         if (g_curIndex == index)
    21         {
    22             break;
    23         }
    24     }
    25     printf("%c unlock
    ", ch);
    26     printf("
    ");
    27     for (int i = 0; i < 10; ++i)
    28     {
    29         printf("%c", ch);
    30     }
    31     printf("
    ");
    32     ++g_curIndex;
    33     g_cv.notify_all();
    34 }
    35 
    36 
    37 int main()
    38 {
    39     srand(time(nullptr));
    40     std::vector<std::thread> vecThreads;
    41     for (int i = 0; i < THREAD_CNT; ++i)
    42     {
    43         vecThreads.push_back(std::thread(PrintChar, i + 'A'));
    44     }
    45     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    46     g_curIndex = 0;
    47     g_cv.notify_all();
    48     for (auto &t : vecThreads)
    49     {
    50         t.join();
    51     }
    52     return 0;
    53 }

    Plan2:

     1 #define THREAD_CNT 5
     2 
     3 std::condition_variable g_cvs[THREAD_CNT];
     4 std::condition_variable g_cv;
     5 std::mutex g_mtx;
     6 
     7 void PrintChar(char ch)
     8 {
     9     int index = ch - 'A';
    10     int sleepMsTime = rand() % 2000;
    11     printf("%c ready
    ", ch);
    12     std::this_thread::sleep_for(std::chrono::milliseconds(sleepMsTime));
    13     printf("%c lock
    ", ch);
    14     std::unique_lock<std::mutex> lg(g_mtx);
    15 
    16     g_cvs[index].wait(lg);
    17 
    18     printf("%c unlock
    ", ch);
    19     printf("
    ");
    20     for (int i = 0; i < 10; ++i)
    21     {
    22         printf("%c", ch);
    23     }
    24     printf("
    ");
    25     if (index != THREAD_CNT - 1)
    26     {
    27         g_cvs[index + 1].notify_one();
    28     }
    29 }
    30 
    31 
    32 int main()
    33 {
    34     srand(time(nullptr));
    35     std::vector<std::thread> vecThreads;
    36     for (int i = 0; i < THREAD_CNT; ++i)
    37     {
    38         vecThreads.push_back(std::thread(PrintChar, i + 'A'));
    39     }
    40     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    41     g_cvs[0].notify_one();
    42     for (auto &t : vecThreads)
    43     {
    44         t.join();
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    java 算法最长连续递增子序列
    java 算法最长连续递增序列
    最大连续子数组和(Java)
    mysql时间序列与窗口函数
    CSS控制br高度
    小知识随手记(九):兄弟选择器(~和+)区别
    VUE组件递归实现自定义目录及拖拽效果
    VUE的插件解析
    VUE的mixin混入解析
    VUE高阶组件解析
  • 原文地址:https://www.cnblogs.com/endenvor/p/13564345.html
Copyright © 2020-2023  润新知