http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/samples/hello.c
================================
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid) {
printf("\n%d: Hello World!\n", threadid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
pthread_t threads[NUM_THREADS];
int rc, t;
for(t=0;t < NUM_THREADS;t++){
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
================================
Output :
Creating thread 0
Creating thread 1
0: Hello World!
1: Hello World!
Creating thread 2
Creating thread 3
2: Hello World!
3: Hello World!
Creating thread 4
4: Hello World!
เพิ่มเติม 2549-12-13 โครงสร้างพื้นฐานการซิงโครไนซ์ (Synchronization) (sync. การทำให้งาน 2 งาน ทำร่วมกันอย่างราบรื่น แต่ async. คือการทำงานของงานที่ไม่เกี่ยวเนื่องกัน) 1. Race Condition สภาพที่โปรเซสสามารถเข้าถึงทรัพยากรได้พร้อมกัน 2. Mutual Exclusion (MuTex) and Critical Region ซึ่งกันและกัน กันไม่ให้เข้า วิกฤต อนาเขต 3. วิธีแก้ปัญหา Mutual Exclusion with busi waiting 5 วิธี
![]() |
+ ผู้สนับสนุน + รับผู้สนับสนุน |