home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************/
- /* SAMPLE PROGRAM 02: SAMPLE02.C */
- /* */
- /* COPYRIGHT: */
- /* ---------- */
- /* Copyright (C) International Business Machines Corp., 1991,1995. */
- /* */
- /* DISCLAIMER OF WARRANTIES: */
- /* ------------------------- */
- /* The following [enclosed] code is sample code created by IBM */
- /* Corporation. This sample code is not part of any standard IBM product */
- /* and is provided to you solely for the purpose of assisting you in the */
- /* development of your applications. The code is provided "AS IS", */
- /* without warranty of any kind. IBM shall not be liable for any damages */
- /* arising out of your use of the sample code, even if they have been */
- /* advised of the possibility of such damages. */
- /* */
- /******************************************************************************/
- /* */
- /* Sample to demonstrate a multithread program */
- /* */
- /* This program demonstrates the multithread programming capability */
- /* of the IBM VisualAge C++ compiler. */
- /* */
- /* This sample program creates one thread for each argument. Each */
- /* thread prints "Hello world from thread n!" the number of times */
- /* specified in the corresponding argument. For example */
- /* */
- /* SAMPLE02 2 4 6 */
- /* */
- /* creates three threads; the first thread displays "Hello" */
- /* two times, the second thread displays "Hello" four times, */
- /* and the third thread displays "Hello" six times. */
- /* */
- /* In operation, the program works in the following order: */
- /* */
- /* 1. Creates the requested number of threads */
- /* 2. Waits until all threads have been created */
- /* 3. Begins multithread execution and waits for completion */
- /* */
- /******************************************************************************/
-
- #include <windows.h>
- #include <malloc.h>
- #include <process.h>
- #include <stdio.h>
- #include <stdlib.h>
-
-
- int main( int argc, char * argv[] );
- void childcode( void * arg );
-
- typedef struct /* the thread information structure */
- { /* */
- unsigned count; /* the number of times to display the text */
- HANDLE hev ; /* the individual thread's event semaphore */
- } THREAD_INFO ; /* handle */
-
- HANDLE hev ; /* the main thread's event semaphore handle */
-
- int main( int argc, char *argv[])
- {
- int i, rc;
- int NumThreads;
- THREAD_INFO thread_info[9];
-
- int MaxThread = 0;
-
- if ( argc == 1 ) {
- printf( "*** The syntax is sample02 <arg1> <arg2> ... <up to 9 args>\n");
- return (-1);
- }
-
- if ( argc > 10 ) {
- printf( "*** Error: Too many arguments (maximum 9 arguments)\n");
- return (-1);
- }
-
-
- /* Create the main thread's event semaphore */
-
- hev = CreateEvent(NULL, /* */
- TRUE, /* */
- FALSE, /* */
- NULL); /* */
- /* */
-
- /* Create one thread for each argument */
-
- NumThreads = 1; /* the main thread counts as one */
-
- /* Create a new thread and pass the corresponding argument */
-
- for (i=1; i < argc ; i++ ) {
-
- /* Create an event semaphore for a child thread */
-
- thread_info[i-1].count = (unsigned) atol( argv[i] ) ;
-
- thread_info[i-1].hev = CreateEvent(NULL,
- FALSE,
- FALSE,
- NULL);
-
- rc = _beginthread( childcode,
- NULL,
- 8192,
- (void *) &thread_info[i-1]);
-
- /* Check for error and keep track of how many threads were created */
-
- if (rc == -1) {
- printf("*** Error: Could not create %d-th Thread ***\n",
- NumThreads + 1);
- } else NumThreads++;
-
- if (rc > MaxThread)
- MaxThread = rc;
-
- }
-
- /* Display how many threads were created */
-
- printf("Number of threads = %d, Maximum thread ID = %d\n\n",
- NumThreads, MaxThread);
-
- --NumThreads;
-
- /* Let the threads begin execution and wait for them to complete */
-
- if (FALSE == SetEvent( hev ))
- printf("Last error : %d\n", GetLastError());
-
- for ( i=0; i<NumThreads; ++i) {
- WaitForSingleObject ( thread_info[i].hev, INFINITE);
- CloseHandle ( thread_info[i].hev );
- } /* endfor */
-
- /* Close the semaphores */
-
- CloseHandle (hev);
- printf("\nDone!\n");
- return 0;
- }
-
- /* "Hello world!" per thread routine. */
-
- void childcode( void * arg)
- {
- DWORD tid ; /* thread id */
- unsigned int i; /* index for loop */
- THREAD_INFO * thread_info;
-
- thread_info = ( THREAD_INFO * ) arg;
-
- /* Get the thread id. */
-
-
- tid = GetCurrentThreadId( ) ;
-
- /* wait for the main routine to say "Go!" */
-
- WaitForSingleObject ( hev, INFINITE );
-
- /* print the message <arg> times */
- /* using Sleep to delay thread execution */
-
- for (i = 1; i <= thread_info->count ; i++ ) {
- printf("Hello world from thread %i!\n",tid);
- Sleep(0L);
- }
-
- /* Let the main program know everything is done */
-
- if (FALSE == SetEvent( thread_info->hev ))
- printf("Last error = %d\n", GetLastError());
-
- }
-