线程顺序执行,分享,有错误可以指出。

Phoenix_FuliMa 2013-01-10 02:01:45
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

方法1:

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include <pthread.h>

sem_t g_sem;
int count = 0;

void *thread_fun1(void *arg)
{
while(1)
{
if(count == 10)
{
break;
}
sem_wait(&g_sem);
printf("thread1's id is %d\n", pthread_self());
sem_post(&g_sem);
sleep(1);
}
return (void*)0;
}

void *thread_fun2(void* arg)
{
while(1)
{

if(count == 10)
{
break;
}
sem_wait(&g_sem);
printf("thread2's id is %d\n", pthread_self());
sem_post(&g_sem);

sleep(1);
}
return (void*)0;
}

void *thread_fun3(void *arg)
{
while(1)
{
sem_wait(&g_sem);
printf("thread3's id is %d\n\n", pthread_self());
sem_post(&g_sem);
if(++count == 10)
{
break;
}
sleep(1);
}
return (void*) 0;
}

int main()
{
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;

int res = 0;
res = sem_init(&g_sem,0,0);
if(res)
{
printf("Semaphore initilization failed\n");
return -1;
}
res = pthread_create(&thread1, NULL, thread_fun1, NULL);
if(res)
{
printf("Thread1 created failed\n");
}
res = pthread_create(&thread2, NULL, thread_fun2, NULL);
if(res)
{
printf("thread2 created fialed\n");
}
res = pthread_create(&thread3, NULL, thread_fun3, NULL);
if(res)
{
printf("thread3 created failed\n");
}
sem_post(&g_sem);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);

}



方法2:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t g_mutex;
int count = 0;
int num_count = 0;
void *thread_fun1(void* arg)
{
while(1)
{
if(count % 3 == 0)
{
pthread_mutex_lock(&g_mutex);
count++;
printf("thread1's id is %d\n",pthread_self());
pthread_mutex_unlock(&g_mutex);
}
if(num_count == 10) break;
}
return (void*) 0;
}

void* thread_fun2(void* arg)
{
while(1)
{
if(count %3 == 1)
{
pthread_mutex_lock(&g_mutex);
count++;
printf("thread2's id is %d\n",pthread_self());
pthread_mutex_unlock(&g_mutex);
}
if(num_count == 10) break;
}
return (void*) 0;
}

void* thread_fun3(void* arg)
{
while(1)
{
if(count %3 == 2)
{
pthread_mutex_lock(&g_mutex);
count++;
printf("thread3's id is %d\n\n", pthread_self());
num_count ++;
pthread_mutex_unlock(&g_mutex);
}
if(num_count == 10) break;

}
return (void*) 0;
}

int main()
{
pthread_t thread1, thread2, thread3;
int res = 0;
pthread_mutex_init(&g_mutex, NULL);

res = pthread_create(&thread1, NULL, thread_fun1, NULL);
if(res)
{
printf("trhead1 created failed\n");
}
res = pthread_create(&thread2, NULL, thread_fun2, NULL);
if(res)
{
printf("thread2 created faield\n");
}
res = pthread_create(&thread3, NULL, thread_fun3, NULL);
if(res)
{
printf("thread3 cerated failed\n");
}

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
}

...全文
191 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuachua66 2013-01-19
  • 打赏
  • 举报
回复
如果三个线程每个有一个ID,共享一个int,int标识哪个线程有权打印,每个打完++那个int,用锁来锁这个int,就能保证打印的顺序了吧。
XY欢欢 2013-01-19
  • 打赏
  • 举报
回复
线程执行顺序是随机的,不一定能保证顺序一定是ABC
brikehuang 2013-01-11
  • 打赏
  • 举报
回复
在练习中可以这么写,但工作中,就千万不要这么写了。其它2个张线程有20%的机率是无法正常结束的
漫步者、 2013-01-10
  • 打赏
  • 举报
回复
可以尝试改变他们的执行顺序看看
jimette 2013-01-10
  • 打赏
  • 举报
回复

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧