Linux多线程编程
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
//线程要执行的函数
void printf_msg(void * ptr){
char * msg;
msg = (char *)ptr;
printf("%s\n",msg);
pthread_exit(0);
}
void main(void){
pthread_t thread1,thread2;
char * msg1 = "Hello";
char * msg2 = "World";
//创建线程
pthread_create(&thread1,NULL,(void *)printf_msg,(void *)msg1);
sleep(10);//休息10s
pthread_create(&thread2,NULL,(void *)printf_msg,(void *)msg2);
sleep(10);
exit(0);
}
作用 创建俩个线程分别打印Hello, World
gcc -o thread thread.c -lpthread
./thread