首页 > 编程语言 > 详细

C++并发实战:一道多线程笔试题

时间:2014-03-24 20:59:58      阅读:539      评论:0      收藏:0      [点我收藏+]

题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;
mutex m;
condition_variable cond;
int flag=0;
int parm_0=0;
int parm_1=1;//后来想了下没必要用parm_0和parm_1两个变量,只用一个来同步两个线程也可以
int loop=5;
void fun(){
    for(int i=0;i<loop;i++){
        unique_lock<mutex> lk(m);//A unique lock is an object that manages a mutex object with unique ownership in both states: locked and unlocked.
        while(parm_0!=flag)
            cond.wait(lk);//在调用wait时会执行lk.unlock()
        cout<<"child thread ";
        for(int j=0;j<10;j++)
            cout<<j<<" ";
        cout<<endl;
        flag=(flag+1)%2;
        cond.notify_one();//被阻塞的线程唤醒后lk.lock()恢复在调用wait前的状态
    }
}
int main(){
    thread one(fun);
    for(int i=0;i<loop;i++){
        unique_lock<mutex> lk(m);
        while(parm_1!=flag)
            cond.wait(lk);
        cout<<"main thread ";
        for(int j=0;j<100;j++)
            cout<<j<<" ";
        cout<<endl;
        flag=(flag+1)%2;
        cond.notify_one();
    }
    one.join();
    return 0;
}

程序输出:

child thread 0 1 2 3 4 5 6 7 8 9 
main thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
child thread 0 1 2 3 4 5 6 7 8 9 
main thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
child thread 0 1 2 3 4 5 6 7 8 9 
main thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
child thread 0 1 2 3 4 5 6 7 8 9 
main thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
child thread 0 1 2 3 4 5 6 7 8 9 
main thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

C++并发实战:一道多线程笔试题,布布扣,bubuko.com

C++并发实战:一道多线程笔试题

原文:http://blog.csdn.net/liuxuejiang158blog/article/details/21977009

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!