STREAMS/UX for the HP 9000 Reference Manual

118
How to Compile and Link STREAMS/UX Drivers, Modules, and Applications
Compiling and Linking TLI/XTI Applications and Threads
If using condition variables, a pthread_cond_signal() must be sent for each
thread waiting on that condition. Condition variables are useful for synchronizing
activity on a single endpoint where multiple threads may be attempting to
manipulate that endpoint. Another use is coordinating multiple endpoints that
need to arrive at a particular state before proceeding. This contrasts with mutex
usage which is better suited for critical data or code section protection.
An example would be if an application were replicating data at multiple sites,
each thread would drive its appropriate endpoint to the state before the final
commit is ready. When the controlling thread has detected that all endpoints are
currently waiting at the same condition variable, the pthread_cond_signal()
could be sent to each thread with the controlling thread waiting until the threads
are complete before releasing the shared memory buffer.
If the application is utilizing the poll() system call, the application will need to
have error handling code in each thread to avoid unnecessary processing. For
example, if multiple threads are sending data to a single endpoint and that
endpoint becomes flow-controlled, when the flow-control condition is relieved,
the poll() system call will return that the endpoint is writable. At this point one or
more threads could be scheduled to execute which may result in one thread
succeeding with the rest returning TFLOW errors.
If a thread is exiting, it is recommended that the thread call pthread_detach() is
used to release any memory that has been allocated for that thread’s usage. If the
detach is not performed, that memory can be lost and the application could
experience memory shortage problems. Once the process is terminated, all
memory should be returned to the system.