Ví dụ cơ bản với pthread trên Linux
1. Tạo và dừng thread Source code gốc lấy từ link /****************************************************************************** * FILE: hello.c * DESCRIPTION: * Chuong trinh demo thao tac tao thread, tat thread * AUTHOR: Blaise Barney * LAST REVISED: 08/09/11 * * TRANSLATE to Vietnamese by Minatu * COMPILE CMD : $gcc -o hello hello.c -lpthread ******************************************************************************/ // #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! Thread #%ld o day !!!n", tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long tId; for (tId = 0; tId < NUM_THREADS; tId++) { printf(" Ham main() : Thread Main : Tao...thread moi: %ldn", tId); rc = pthread_create(&threads[tId], NULL, PrintHello, (void *)tId); if (rc) { printf("ERROR; loi khi chay pthread_create(), ma loi: %dn", rc); exit(-1); } } /* Last thing that main() should do */ pthread_exit(NULL); } } Kết quả sau khi biên dịch và chạy: ...