zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    mysql工具导出数据库表数据
    c#接收http的post请求的多个文件流
    java上传文件和参数到服务器
    windows server 2008 w3svc服务无法启动
    java调用c#webapi的接口实现文件上传
    postman上线文件上传,并用c#服务端接收
    sql语句修改数据库字段的长度
    VB2015运行项目时出现的错误
    JavaWeb实现分页功能
    会话跟踪技术
  • 原文地址:https://www.cnblogs.com/endenvor/p/13564345.html
Copyright © 2011-2022 走看看