HPlogo HP C/HP-UX Programmer's Guide: HP 9000 Computers > Chapter 8 Threads and Parallel Processing

Synchronization Functions

» 

Technical documentation

Complete book in PDF

 » Table of Contents

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:

gate_t namelist

declares variables to use in a critical section, ordered section, or passed as arguments to the synchronization functions

barrier_t namelist

declares a list of synchronization variables for the barrier routines

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.

Allocate Functions

These functions allocate memory for a gate or barrier. When memory is first allocated, gate variables are unlocked.

int alloc_gate(gate_t *gate_p);

int alloc_barrier(barrier_t *barrier_p);

gate_p and barrier_p are pointers of the indicated type, which have been previously declared as described above.

Deallocate Functions

These functions free the memory assigned to the specified gate or barrier variable.

These functions have the following declarations:

int free_gate(gate_t *gate_p);

int free_barrier(barrier_t, *barrier_p);

where gate_p and barrier_p are pointers of the indicated type. Always free gates and barriers when you are done using them.

Locking Functions

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:

int lock_gate(gate_t *gate_p);

int cond_lock_gate(gate_t *gate_p);

where gate_p is a pointer of the indicated type.

Unlocking Function

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:

int unlock_gate(gate_t *gate_p);


where gate_p is a pointer of the indicated type.

Wait Function

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:

int wait_barrier(barrier_t *barrier_p, const int *nthr);

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.

© Hewlett-Packard Development Company, L.P.