Steward
分享是一種喜悅、更是一種幸福
程式語言 - GNU - C/C++ - Thread
參考資訊:
https://www.geeksforgeeks.org/multithreading-in-c/
https://www.cntofu.com/book/46/gcc/gcc4e2d-_pthread_548c-_lpthread_de_qu_bie.md
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> static void * mythread ( void *param) { printf ( "mythread++\n" ); sleep (1); printf ( "mythread--\n" ); return NULL ; } int main ( int argc, char **argv) { pthread_t thread_id = 0; pthread_create (&thread_id, NULL , mythread , NULL ); pthread_join (thread_id, NULL ); return 0; } |
執行
$ gcc main.c -o main -pthread $ ./main mythread++ mythread--