<< Chapter < Page | Chapter >> Page > |
Multiple threads are also used in high performance database and Internet servers to improve the overall throughput of the server. With a single thread, the program can either be waiting for the next network request or reading the disk to satisfy the previous request. With multiple threads, one thread can be waiting for the next network transaction while several other threads are waiting for disk I/O to complete.
The following is an example of a simple multithreaded application. This example uses the IEEE POSIX standard interface for a thread library. If your system supports POSIX threads, this example should work. If not, there should be similar routines on your system for each of the thread functions. It begins with a single master thread that creates three additional threads. Each thread prints some messages, accesses some global and local variables, and then terminates:
#define_REENTRANT /* basic lines for threads */
#include<stdio.h>#include<pthread.h>#define THREAD_COUNT 3
void *TestFunc(void *);int globvar; /* A global variable */
int index[THREAD_COUNT]/* Local zero-based thread index */
pthread_t thread_id[THREAD_COUNT]; /* POSIX Thread IDs */
main() {int i,retval;
pthread_t tid;globvar = 0;printf("Main - globvar=%d\n",globvar);
for(i=0;i<THREAD_COUNT;i++) {
index[i]= i;
retval = pthread_create(&tid,NULL,TestFunc,(void *) index[i]);printf("Main - creating i=%d tid=%d retval=%d\n",i,tid,retval);
thread_id[i]= tid;
}printf("Main thread - threads started globvar=%d\n",globvar);
for(i=0;i<THREAD_COUNT;i++) {
printf("Main - waiting for join %d\n",thread_id[i]);
retval = pthread_join( thread_id[i], NULL ) ;
printf("Main - back from join %d retval=%d\n",i,retval);}
printf("Main thread - threads completed globvar=%d\n",globvar);}void *TestFunc(void *parm) {
int me,self;me = (int) parm; /* My own assigned thread ordinal */self = pthread_self(); /* The POSIX Thread library thread number */
printf("TestFunc me=%d - self=%d globvar=%d\n",me,self,globvar);globvar = me + 15;
printf("TestFunc me=%d - sleeping globvar=%d\n",me,globvar);sleep(2);
printf("TestFunc me=%d - done param=%d globvar=%d\n",me,self,globvar);}
The global shared areas in this case are those variables declared in the static area outside the
main( )
code. The local variables are any variables declared within a routine. When threads are added, each thread gets its own function call stack. In C, the
automatic variables that are declared at the beginning of each routine are allocated on the stack. As each thread enters a function, these variables are separately allocated on that particular thread’s stack. So these are the
thread-local variables.
Unlike the
fork( )
function, the
pthread_create( )
function creates a new thread, and then control is returned to the calling thread. One of the parameters of the
pthread_create( )
is the name of a function.
New threads begin execution in the function
TestFunc( )
and the thread finishes when it returns from this function. When this program is executed, it produces the following output:
Notification Switch
Would you like to follow the 'High performance computing' conversation and receive update notifications?