双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

进程中创建的线程,线程如何终止的

gfdsg_____ gfdsg_____ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
i am one thread!
i am one thread!
i am one thread!
i am one thread!
i am one thread!
i am one thread!
back to the main funciton!
i am the main!
[root@Robot 2]#
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <syspes.h>
8 #include <sys/stat.h>
9 #include <errno.h>
10 #include <pthread.h>
11
12 void *my1(void)
13 {
14 int fd;
15 while(1)
16 {
17 printf("i am one thread!\n"); 18 }
19 }
20
21 int main(void)
22 {
23 pthread_t id1;
24 pthread_create(&id1, NULL, (void *)my1, NULL);
25 printf("back to the main funciton!\n");
26 printf("i am the main!\n");
27 return 0;
28 }
为什么我在线程中执行while(1)这个循环,最后线程自己退出了呢??认为线程把进程的资源用的差不多了,进程把线程给终止了,这样子对吗??程序执行到pthread_create(&id1, NULL, (void *)my1, NULL);之后不是去执行线程的代码了吗?
在线程中死循环的话,怎么又返回到进程中去了。
...全文
评论 9
收藏 已收藏
分享
返回顶部
gfdsg_____
Linux/Unix社区 23,224 成员 74,535 内容