ตัวอย่างถัดไปแสดงการทำงานแยกกันของ Thread กับ main Process
Thread 1 ตัว
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>
void * thread_function(void * arg);int i, j;int main() { pthread_t a_thread; //thread declaration pthread_create( & a_thread, NULL, thread_function, NULL); //thread is created pthread_join(a_thread, NULL); //process waits for thread to finish . //Comment this line to see the difference printf("Inside Main Program\n"); for (j = 20; j < 25; j++) { printf("%d\n", j); sleep(1); }}void * thread_function(void * arg) { // the work to be done by the thread is defined in this function printf("Inside Thread\n"); for (i = 0; i < 5; i++) { printf("%d\n", i); sleep(1); }}OUTPUT
01234Inside Main Program2021222324ตัวอย่าง 2
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>
void * thread_function(void * arg);int i, j;int main() { pthread_t a_thread; //thread declaration pthread_create( & a_thread, NULL, thread_function, NULL); //thread is created printf("Inside Main Program\n"); for (j = 50; j < 60; j++) { printf("Main %d\n", j); sleep(1); } printf("Main thread end\n"); pthread_join(a_thread, NULL); //process waits for thread to finish . //Comment this line to see the difference printf("Thread end\n");}void * thread_function(void * arg) { // the work to be done by the thread is defined in this function printf("Inside Thread\n"); for (i = 0; i < 50; i++) { printf("Thread %d\n", i); sleep(1); }}ตัวอย่างถ้าไม่ join thread
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>
void * thread_function(void * arg);int i, j;int main() { pthread_t a_thread; //thread declaration pthread_create( & a_thread, NULL, thread_function, NULL); //thread is created printf("Inside Main Program\n"); for (j = 50; j < 60; j++) { printf("Main %d\n", j); sleep(1); } printf("Main thread end\n"); // pthread_join(a_thread, NULL); //process waits for thread to finish . //Comment this line to see the difference printf("Thread end\n");}void * thread_function(void * arg) { // the work to be done by the thread is defined in this function printf("Inside Thread\n"); for (i = 0; i < 50; i++) { printf("Thread %d\n", i); sleep(1); }}Thread 2 ตัว
#include <stdio.h>#include <stdlib.h> // for srand(), rand()#include <unistd.h> // for sleep(), usleep()#include <pthread.h> // the header file for the pthread library
void *thread_entry_func( void *arg ) {long id = (long)arg;printf( "Thread '%ld' started\n", id );// sleep for some seconds (randomized between 1..10)sleep( 1 + (rand() % 10) );printf( "Thread '%ld' finished\n", id );return NULL;}
int main( int argc, char *argv[] ) {int retval; // return value from a function callpthread_t thread1, thread2; // thread handles
// initialize the pseudo-random generator with a seedsrand( time(NULL) );
// create Thread 1retval = pthread_create(&thread1 /* used to identify thread 1 */,NULL /* default attributes */,thread_entry_func /* start routine */,(void*) 1 /* thread argument \*/ );printf( "Thread creation (1): %s\n", retval ? "FAILED" : "OK" );
// create Thread 2retval = pthread_create(&thread2 /* used to identify thread 2 */,NULL /* default attributes */,thread_entry_func /* start routine */,(void*) 2 /* thread argument */ );printf( "Thread creation (2): %s\n", retval ? "FAILED" : "OK" );// sleep for 10msecusleep( 10000 /*usec\*/ );// wait until both threads (thread 1 and 2) are finished.printf( "\nWaiting for threads to be finished...\n" );if ( thread1 ) {pthread_join( thread1, NULL ); // wait for thread 1}if ( thread2 ) {pthread_join( thread2, NULL ); // wait for thread 2}printf( "Done...\n\n" );return 0;}OUTPUT
Thread creation (1): OKThread '1' startedThread creation (2): OKThread '2' started
Waiting for threads to be finished...Thread '1' finishedThread '2' finishedDone...จะใช้การสร้าง Thread เก็บไว้ใน Array
#include <stdio.h>#include <stdlib.h> // for srand(), rand()#include <unistd.h> // for sleep(), usleep()#include <pthread.h> // the header file for the pthread lib
#define NUM_THREADS (10)
void *thread_entry_func( void *arg ) { long id = (long)arg; printf( "Thread '%ld' started (0x%08lX)\n", id, pthread_self() ); // sleep for some seconds (randomized between 1..10) sleep( 1 + (rand() % 10) ); printf( "Thread '%ld' finished\n", id ); return NULL;}
int main( int argc, char *argv[] ) { int retval; pthread_t threads[ NUM_THREADS ]; // array of thread handles
// initialize the pseudorandom generator with a seed srand( time(NULL) );
// create a number of threads for ( int i=0; i < NUM_THREADS; i++ ) { long id = (i+1); // used as thread argument retval = pthread_create( &threads[i], NULL, thread_entry_func, (void*) id ); printf( "main> thread creation (%ld): %s\n", id, retval ? "FAILED" : "OK" ); if ( retval ) { // thread creation error printf( "Program terminated...\n" ); exit(1); } } // sleep for 1msec before proceeding usleep( 1000 /*usec*/ ); // wait until all threads are finished. printf( "\nWaiting for all threads to be finished..\n" ); for ( int i=0; i < NUM_THREADS; i++ ) { pthread_join( threads[i], NULL ); // wait for thread } printf( "Done...\n\n" ); return 0;}OUTPUT
main> thread creation (1): OKThread '1' started (0x7FE7F8F41700)main> thread creation (2): OKThread '2' started (0x7FE7F8740700)main> thread creation (3): OKThread '3' started (0x7FE7F7F3F700)main> thread creation (4): OKThread '4' started (0x7FE7F773E700)main> thread creation (5): OKThread '5' started (0x7FE7F6F3D700)main> thread creation (6): OKThread '6' started (0x7FE7F673C700)main> thread creation (7): OKThread '7' started (0x7FE7F5F3B700)main> thread creation (8): OKThread '8' started (0x7FE7F573A700)main> thread creation (9): OKThread '9' started (0x7FE7F4F39700)main> thread creation (10): OKThread '10' started (0x7FE7F4738700)
Waiting for all threads to be finished..Thread '10' finishedThread '3' finishedThread '4' finishedThread '6' finishedThread '2' finishedThread '5' finishedThread '7' finishedThread '9' finishedThread '8' finishedThread '1' finishedDone...