Using thread-related APIs

You can call system APIs to start threads and wait for threads to complete, and to synchronize access to shared resources.

The following example creates several threads, and uses two different synchronization techniques to control access to some shared variables. To fully understand the examples, you should refer to the Multithreaded Applications at: http://www.ibm.com/systems/infocenter/. The examples are similar to the C examples that show how to use semaphores and mutexes.

The semaphore example shows how to pass a parameter to a thread-start procedure threadSem. Normally a thread-start procedure parameter is a data structure, whose subfields take the place of the parameters and return value that a normal procedure would use. The example has two subfields; the "val" subfield is input to the thread-start procedure, and the "result" subfield is output by the procedure. In the example, the thread-start procedure sets the result to the input value multiplied by two.



[ Top of Page | Previous Page | Next Page | Contents | Index ]