linux c多线程编程范例

时间:2024-05-23 08:06:37
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h> void* func_1(void* args)
{
while(){
sleep();
printf("this is func_1!\n");
}
} void* func_2(void* args)
{
while(){
sleep();
printf("this is func_2!\n");
}
} int main()
{
pthread_t pid1, pid2; if(pthread_create(&pid1, NULL, func_1, NULL))
{
return -;
} if(pthread_create(&pid2, NULL, func_2, NULL))
{
return -;
} while(){
sleep();
} return ;
}

编译命令:gcc thread.c -o thread -lpthread