Thread Creation and Termination: Show Example: pthread1.c #include <stdio.h> Compile:
Run: ./a.out Results: Thread 1 Details:
The threads library provides three synchronization mechanisms:
Mutexes:Mutexes are used to prevent data inconsistencies due to race conditions. A race condition often occurs when two or more threads need to perform operations on the same memory area, but the results of computations depends on the order in which these operations are performed. Mutexes are used for serializing shared resources. Anytime a global resource is accessed by more than one thread the resource should have a Mutex associated with it. One can apply a mutex to protect a segment of memory ("critical region") from other threads. Mutexes can be applied only to threads in a single process and do not work between processes as do semaphores.Example threaded function:
If register load and store operations for the incrementing of variable counter occurs with unfortunate timing, it is theoretically possible to have each thread increment and overwrite the same variable with the same value. Another possibility is that thread two would first increment counter locking out thread one until complete and then thread one would increment it to 2.
Code listing: mutex1.c #include <stdio.h> Compile: cc -lpthread mutex1.c Counter value: 1 When a mutex lock is attempted against a mutex which is held by another thread, the thread is blocked until the mutex is unlocked. When a thread terminates, the mutex does not unless explicitly unlocked. Nothing happens by default. Joins:A join is performed when one wants to wait for a thread to finish. A thread calling routine may launch multiple threads then wait for them to finish to get the results. One wait for the completion of the threads with a join.Sample code: join1.c #include <stdio.h> Compile: cc -lpthread join1.c Thread number 1026 Condition Variables:A condition variable is a variable of type pthread_cond_t and is used with the appropriate functions for waiting and later, process continuation. The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true. A condition variable must always be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock. The thread will be perpetually waiting for a signal that is never sent. Any mutex can be used, there is no explicit link between the mutex and the condition variable. Functions used in conjunction with the condition variable:
Example code: cond1.c #include <stdio.h> Compile: cc -lpthread
cond1.c Counter value functionCount1: 1 Note that functionCount1() was halted while count was between the values COUNT_HALT1 and COUNT_HALT2. The only thing that has been ensures is that functionCount2 will increment the count between the values COUNT_HALT1 and COUNT_HALT2. Everything else is random. The logic conditions (the "if" and "while" statements) must be chosen to insure that the "signal" is executed if the "wait" is ever processed. Poor software logic can also lead to a deadlock condition. Note: Race conditions abound with this example because count is used as the condition and can't be locked in the while statement without causing deadlock. I'll work on a cleaner example but it is an example of a condition variable. When this option is enabled, each thread may have its own scheduling properties. Scheduling attributes may be specified:
News Groups:
Books:
What values of first and second as a result of executing the code segment?What are the values of first and second as a result of executing the code segment? The value of first is true, and the value of second is true.
Which of the following would be the best choice for a variable which holds my age?The variable age is to be used to represent a person's age, in years.
|