C++ 多线程交替输出1~100

使用互斥锁实现:互斥锁+自增判断奇偶

代码:

#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

int number;
mutex mutex_number;

const int MAXNUM = 10;

// 打印奇数
void add_1() {
    
    
    while (1) {
    
    
        mutex_number.lock();
        if (number >= MAXNUM) {
    
    
            mutex_number.unlock();
            break;
        }
        if (number % 2 == 0) {
    
    
            number++;
            cout << "mythread_1: " << number << endl;    // 输出
        }

        mutex_number.unlock();
    }
    cout  << "mythread_1 finish" << endl;     // mythread_1完成
}

// 打印偶数
void add_2() {
    
    
    while (1) {
    
    
        mutex_number.lock();

        if (number >= MAXNUM) {
    
    
            mutex_number.unlock();
            break;
        }
        if (number % 2 == 1) {
    
    
            number++;
            cout << "mythread_2: " << number << endl;    // 输出
        }

        mutex_number.unlock();
    }
    cout  << "mythread_2 finish" << endl;     // mythread_2完成
}

int main() {
    
    
    number = 0;

    cout << endl << "Create and Start!" << endl;

    thread mythread_1(add_1);
    thread mythread_2(add_2);

    mythread_1.join();
    mythread_2.join();

    cout << endl << "Finish and Exit!" << endl;
    return 0;
}

输出结果:
在这里插入图片描述
https://www.freesion.com/article/9065193208/

猜你喜欢

转载自blog.csdn.net/weixin_43202635/article/details/115419239