c语言如何交替打印大小写字母,C/C++语言实现两个线程交替打印奇偶数

C/C++两个线程交替打印

C语言第一种方式

C语言第二种方式

C++实现的第一种方式

C语言第一种方式

实现思想主要是让两个线程互相唤醒对方来交替打印数字

#include #include #include #include int g_num = 1;

pthread_mutex_t mutex;

pthread_cond_t cond1,cond2;

void* thread1(void* arg)

{

while(1)

{

pthread_mutex_lock(&mutex);

//如果需要交替打印一定范围(例如1-10)内的数字,那么可以加上下面两行代码

//if(g_num > 10)

//exit(1);

printf("Thread1: %d \n",g_num);

g_num ++;

pthread_cond_signal(&cond2);

pthread_cond_wait(&cond1,&mutex);

pthread_mutex_unlock(&mutex);

sleep(1);

}

return NULL;

}

void* thread2(void* arg)

{

while(1)

{

//这个sleep(1)加在前面是因为开启线程时有可能是线程2先打印,

//导致变成thread2输出奇数,threa1输出偶数。为了避免这种情况,可以在延迟下线程2的打印。

sleep(1);

pthread_mutex_lock(&mutex);

printf("Thread2: %d \n",g_num);

g_num++;

pthread_cond_signal(&cond1);

pthread_cond_wait(&cond2,&mutex);

pthread_mutex_unlock(&mutex);

}

return NULL;

}

int main()

{

pthread_t p1,p2;

pthread_mutex_init(&mutex,NULL);

pthread_cond_init(&cond1,NULL);

pthread_cond_init(&cond2,NULL);

pthread_create(&p1,NULL,thread1,NULL);

pthread_create(&p2,NULL,thread2,NULL);

pthread_join(p1,NULL);

pthread_join(p2,NULL);

pthread_mutex_destroy(&mutex);

pthread_cond_destroy(&cond1);

pthread_cond_destroy(&cond2);

return 0;

}

运行结果如下:

4beb4b9c45fefb8d23d458d08a79c7e7.png

C语言第二种方式

思想主要是引入第三个线程来管理唤醒信号

#include #include #include #include int g_num = 0;

pthread_mutex_t mutex;

pthread_cond_t cond1,cond2;

void* thread1(void* arg)

{

while(1)

{

pthread_mutex_lock(&mutex);

pthread_cond_wait(&cond1,&mutex);

printf("Thread1: %d \n",g_num);

pthread_mutex_unlock(&mutex);

sleep(1);

}

return NULL;

}

void* thread2(void* arg)

{

while(1)

{

pthread_mutex_lock(&mutex);

pthread_cond_wait(&cond2,&mutex);

printf("Thread2: %d \n",g_num);

pthread_mutex_unlock(&mutex);

sleep(1);

}

return NULL;

}

void* thread3(void* arg)

{

while(1)

{

//有可能出现线程3都运行了1次了,线程1还没开始,导致不是从1开始打印,为了避免这种情况,所以先让管理线程休眠一会。

sleep(1);

pthread_mutex_lock(&mutex);

++g_num;

pthread_mutex_unlock(&mutex);

if((g_num % 2) == 0)

pthread_cond_signal(&cond2);

else if((g_num % 2) == 1){

pthread_cond_signal(&cond1);}

}

return NULL;

}

int main()

{

pthread_t p1,p2,p3;

pthread_mutex_init(&mutex,NULL);

pthread_cond_init(&cond1,NULL);

pthread_cond_init(&cond2,NULL);

pthread_create(&p1,NULL,thread1,NULL);

pthread_create(&p2,NULL,thread2,NULL);

pthread_create(&p3,NULL,thread3,NULL);

pthread_join(p1,NULL);

pthread_join(p2,NULL);

pthread_join(p3,NULL);

pthread_mutex_destroy(&mutex);

pthread_cond_destroy(&cond1);

pthread_cond_destroy(&cond2);

return 0;

}

C++实现的第一种方式

#include #include #include #include #include using namespace std;

mutex mut;

condition_variable cond1, cond2;

int g_nums = 1;

void thread1() {

while (1) {

unique_locklocker(mut);

cout < locker(mut);

cout << "Thread2:" << g_nums << endl;

g_nums++;

cond1.notify_one();

cond2.wait(locker);

locker.unlock();

}

}

int main() {

thread t1(thread1);

thread t2(thread2);

t1.join();

t2.join();

system("pause");

return 0;

}

C++实现的第二种方式同理也很容易写出

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值