C++使用多线程交替打印1~10

方法1:
pthread
https://subingwen.cn/linux/thread-sync/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>

#define MAX 100
// 全局变量
int number;

// 创建一把互斥锁
// 全局变量, 多个线程共享
pthread_mutex_t mutex;

// 线程处理函数
void* funcA_num(void* arg)
{
    for(int i=0; i<MAX; ++i)
    {
        // 如果线程A加锁成功, 不阻塞
        // 如果B加锁成功, 线程A阻塞
        pthread_mutex_lock(&mutex);
        int cur = number;
        cur++;
        usleep(10);
        number = cur;
        pthread_mutex_unlock(&mutex);
        printf("Thread A, id = %lu, number = %d\n", pthread_self(), number);
    }

    return NULL;
}

void* funcB_num(void* arg)
{
    for(int i=0; i<MAX; ++i)
    {
        // a加锁成功, b线程访问这把锁的时候是锁定的
        // 线程B先阻塞, a线程解锁之后阻塞解除
        // 线程B加锁成功了
        pthread_mutex_lock(&mutex);
        int cur = number;
        cur++;
        number = cur;
        pthread_mutex_unlock(&mutex);
        printf("Thread B, id = %lu, number = %d\n", pthread_self(), number);
        usleep(5);
    }

    return NULL;
}

int main(int argc, const char* argv[])
{
    pthread_t p1, p2;

    // 初始化互斥锁
    pthread_mutex_init(&mutex, NULL);

    // 创建两个子线程
    pthread_create(&p1, NULL, funcA_num, NULL);
    pthread_create(&p2, NULL, funcB_num, NULL);

    // 阻塞,资源回收
    pthread_join(p1, NULL);
    pthread_join(p2, NULL);

    // 销毁互斥锁
    // 线程销毁之后, 再去释放互斥锁
    pthread_mutex_destroy(&mutex);

    return 0;
}



作者: 苏丙榅
链接: https://subingwen.cn/linux/thread-sync/#2-1-%E4%BA%92%E6%96%A5%E9%94%81%E4%BD%BF%E7%94%A8
来源: 爱编程的大丙
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

方法2:

使用多线程交替打印1-10,线程1打印奇数,线程2打印偶数

#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 == 1) {
			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 == 0) {
			cout << "mythread_2: " << number++ << endl;    // 输出
		}

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

int main() {
	number = 1;

	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;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

触不可及<>

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值