當前位置:首頁 » 操作系統 » 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-09-17 09:01:40 瀏覽:734
c語言入門基礎 發布:2025-09-17 08:54:30 瀏覽:666
副卡服務密碼是多少位 發布:2025-09-17 08:45:44 瀏覽:436
白條密碼是什麼情況 發布:2025-09-17 08:43:01 瀏覽:317
高中信息演算法與程序 發布:2025-09-17 08:41:34 瀏覽:25
伺服器禁止設置幾個ip 發布:2025-09-17 08:41:26 瀏覽:503
側限壓縮儀 發布:2025-09-17 08:41:24 瀏覽:173
php登陸系統 發布:2025-09-17 08:35:55 瀏覽:419
wincc全局腳本中加減運算 發布:2025-09-17 08:05:48 瀏覽:337
如何將離線緩存轉至qq文件視頻 發布:2025-09-17 07:55:45 瀏覽:304