急!有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC… 用linux下的c实现!
答案:3 悬赏:40
解决时间 2021-03-14 13:53
- 提问者网友:陪我到最后
- 2021-03-13 22:58
给出思路就行!
最佳答案
- 二级知识专家网友:闲懒诗人
- 2021-03-14 00:29
gcc Thread.c -o Thread -lpthread
#include <stdio.h>
#include <pthread.h>
void main()
{
pthread_t t1 , t2 , t3;
void *p_msg(void *);
pthread_create(&t1,NULL,p_msg,(void * )"A ");
pthread_create(&t2,NULL,p_msg,(void * )"B ");
pthread_create(&t3,NULL,p_msg,(void * )"C");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
}
void *p_msg(void *m)
{
int i;
for(i = 0 ; i < 10 ; i ++)
{
printf("%s",m);
fflush(stdout);
sleep(1);
}
return NULL;
}
#include <stdio.h>
#include <pthread.h>
void main()
{
pthread_t t1 , t2 , t3;
void *p_msg(void *);
pthread_create(&t1,NULL,p_msg,(void * )"A ");
pthread_create(&t2,NULL,p_msg,(void * )"B ");
pthread_create(&t3,NULL,p_msg,(void * )"C");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
}
void *p_msg(void *m)
{
int i;
for(i = 0 ; i < 10 ; i ++)
{
printf("%s",m);
fflush(stdout);
sleep(1);
}
return NULL;
}
全部回答
- 1楼网友:夢想黑洞
- 2021-03-14 02:40
//文件:Thread.c
//编译命令:gcc Thread.c -o Thread -lpthread
#include <stdio.h>
#include <pthread.h>
void main()
{
pthread_t t1 , t2 , t3;
void *p_msg(void *);
pthread_create(&t1,NULL,p_msg,(void * )"A ");
pthread_create(&t2,NULL,p_msg,(void * )"B ");
pthread_create(&t3,NULL,p_msg,(void * )"C\n");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
}
void *p_msg(void *m)
{
int i;
for(i = 0 ; i < 10 ; i ++)
{
printf("%s",m);
fflush(stdout);
sleep(1);
}
return NULL;
}
- 2楼网友:夢想黑洞
- 2021-03-14 01:58
java代码
public class main {
public static void main(string[] args){
data data=new data();
new threada("a",data).start();
new threadb("b",data).start();
new threadc("c",data).start();
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯