当前位置:首页 » 操作系统 » 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;
}

热点内容
联想电脑怎么查配置 发布:2025-03-18 07:49:38 浏览:360
云虚机和云服务器 发布:2025-03-18 07:49:00 浏览:683
点击php函数 发布:2025-03-18 07:45:12 浏览:617
算法应该包含 发布:2025-03-18 07:40:25 浏览:512
oss有加密 发布:2025-03-18 07:36:00 浏览:341
如何获取ftp服务器的ip地址 发布:2025-03-18 07:35:53 浏览:538
log4jmybatissql 发布:2025-03-18 07:33:33 浏览:450
苹果6sp相册怎么加密 发布:2025-03-18 07:27:50 浏览:211
ark怎么开联机服务器 发布:2025-03-18 07:27:10 浏览:130
居家矛盾脚本 发布:2025-03-18 07:24:48 浏览:832