Linux多线程编程 Demo

来源:互联网 发布:手机百度云网络出错 编辑:程序博客网 时间:2024/05/21 10:59

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


原创粉丝点击