leobegin2016 2017-05-21 09:00 采纳率: 100%
浏览 1872
已采纳

linux下 c/c++多线程简单程序编译不过

1 #include
2 #include
3 #include
4 #include
5
6
7 void *thr_fn1(void *arg)
8 {
9 printf("thread 1 returning\n");
10 return((void *)1);
11 }
12
13 void *thr_fn2(void *arg)
14 {
15 printf("thread 2 exiting\n");
16 return((void *)2);
17 }
18
19 int main(void)
20 {
21 pthread_t tid1,tid2;
22 void *tret;
23 pthread_creat(&tid1,NULL,thr_fn1,NULL);
24 pthread_creat(&tid2,NULL,thr_fn2,NULL);
25 pthread_join(tid1,&tret);
26 printf("thread 1 exit code %d\n",(int)tret);
27 pthread_join(tid2,&tret);
28 printf("thread 2 exit code %d\n",(int)tret);
29 exit(0);
30 }

  • 写回答

4条回答 默认 最新

  • oyljerry 2017-05-21 09:01
    关注

    编译的时候要连接lpthread线程库

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • threenewbee 2017-05-21 09:13
    关注

    行号是什么玩意,头文件也没有,报什么错误也不贴,怎么帮你看。

    评论
  • jklinux 2017-05-21 09:50
    关注

    没显示行号,是因为楼主没选择插入代码的方式贴出来。 估计oyljerry说对了

    gcc test.c -lpthread就好了

    评论
  • askunix_hjh 2019-01-22 17:30
    关注

    gcc test.c -o test -lpthread

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 页面调接口时加载卡住不响应
  • ¥35 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多
  • ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
  • ¥15 求减少这个c++程序内存的解决方案
  • ¥30 改正代码,错误比较多