当前位置:首页 » 操作系统 » linuxc线程创建

linuxc线程创建

发布时间: 2023-07-07 15:54:09

1. 在linux下用C++创建新线程

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* thread(void* arg)
{
printf ("The child process...\n");
}

int main(int argc, char *argv[])
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *)thread,NULL);
if(ret!=0)
{
printf ("Create pthread error!\n");
exit (1);
}

}
程序如上就可以编译
它属于linux下C编程中多线程编程的范围。
用命令
gcc -lpthread 1.c -o 1
./1
就可以出结果。
多线程编程的基础可以参考
http://hi..com/huifeng00/blog/item/ed13ddc0d6c59c170ff47715.html

2. linux下C多线程编程,为每个文件创建一个线程,转换内容大小写

你main里创建完线程就直接退出了,线程还没来的及干活就结束当然不行了。需要加pthread_join等待,像下面这样:
int main(int argc, char ** argv)
{
pthread_t tid[10];
int i;
for(i=1; i<argc; i++)
{
if(pthread_create(&tid[i], NULL, thr_convert, (void*)argv[i]) != 0)
{
perror("pthread_create");
exit(1);
}
}
for(i=1; i<argc; i++)
pthread_join(tid[i],NULL);

return EXIT_SUCCESS;
}

热点内容
php办公系统 发布:2025-07-19 03:06:35 浏览:898
奥德赛买什么配置出去改装 发布:2025-07-19 02:53:18 浏览:40
请与网络管理员联系请求访问权限 发布:2025-07-19 02:37:34 浏览:189
ipad上b站缓存视频怎么下载 发布:2025-07-19 02:32:17 浏览:844
phpcgi与phpfpm 发布:2025-07-19 02:05:19 浏览:527
捷达方向机安全登录密码是多少 发布:2025-07-19 00:57:37 浏览:692
夜魔迅雷下载ftp 发布:2025-07-19 00:39:29 浏览:99
增值税票安全接入服务器地址 发布:2025-07-19 00:20:45 浏览:486
solidworkspcb服务器地址 发布:2025-07-18 22:50:35 浏览:822
怎么在堆叠交换机里配置vlan 发布:2025-07-18 22:42:35 浏览:630