linux c多线程编程范例
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> void* func_1(void* args) { while(1){ sleep(1); printf("this is func_1!\n"); } } void* func_2(void* args) { while(1){ sleep(2); printf("this is func_2!\n"); } } int main() { pthread_t pid1, pid2; if(pthread_create(&pid1, NULL, func_1, NULL)) { return -1; } if(pthread_create(&pid2, NULL, func_2, NULL)) { return -1; } while(1){ sleep(3); } return 0; }
编译命令:gcc thread.c -o thread -lpthread