
C语言的多线程编程
C语言的多线程编程
1.直接上代码
#include <stdio.h> #define NUM 6 void *print_msg(void* m); int main() { // pthread_t t1,t2; pthread_create(&t1,NULL,print_msg,(void *)"hello,"); pthread_create(&t2,NULL,print_msg,(void *)"world!\n"); pthread_join(t1,NULL); pthread_join(t2,NULL); } void *print_msg(void* m) { char *cp=(char*)m; int i; for(i=0;i<NUM;i++) { printf("%s",m); fflush(stdout); sleep(1); } }
2.linux 上编译
正确编译 gcc -lpthread -o pthread pthread.c
错误编译 gcc -o pthread pthread.c
报错 undefined reference to `pthread_create’
转载参考 http://www.cnblogs.com/renyuan/archive/2012/11/26/2789139.html