|
|
HP C/HP-UX Programmer's Guide: HP 9000 Computers > Chapter 8 Threads and Parallel
ProcessingSynchronization Functions |
|
HP C provides functions that can be used with pragmas to achieve synchronization. Gates allow you to restrict execution of a block of code to a single thread. They can be allocated, locked, unlocked or deallocated. Or, they can be used with the ordered or critical section pragmas, which automate the locking and unlocking functions. Barrriers block further execution until all executing threads reach the barrier. You declare gates and barriers by using the following type definitions:
namelist is a comma-separated list of one or more gate or barrier names. Gates and barriers should only appear in definition and declaration statements, and as formal and actual arguments. These functions allocate memory for a gate or barrier. When memory is first allocated, gate variables are unlocked.
gate_p and barrier_p are pointers of the indicated type, which have been previously declared as described above. These functions free the memory assigned to the specified gate or barrier variable. These functions have the following declarations:
where gate_p and barrier_p are pointers of the indicated type. Always free gates and barriers when you are done using them. These functions acquire a gate for exclusive access. If the gate cannot be immediately acquired, the calling thread waits for it. The conditional locking functions, which are prefixed with COND_ or cond_, acquire a gate if doing so does not require a wait. If the gate is acquired, the functions return 0; if not, they return -1. The functions have the following declarations:
where gate_p is a pointer of the indicated type. This function releases a gate from exclusive access. Gates are typically released by the thread that locks them, unless a gate was locked by thread 0 in serial code. In that case it might be unlocked by a single different thread in a parallel construct. The function has the following declaration:
where gate_p is a pointer of the indicated type. This function uses a barrier to cause the calling thread to wait until the specified number of threads call the function, at which point all threads are released from the function simultaneously. The function has the following declaration:
where barrier_p is a pointer of the indicated type and nthr is a pointer referencing the number of threads calling the routine. You can use a barrier variable in multiple calls to the wait() function, as long as you ensure that two barriers are not active at the same time. Also, check that nthr reflects the correct number of threads. |
|