After we chose the right 'model', what remains is implementation. The following are two important structures that you need to manipulate when playing around with threads in Linux:

• pthreadJ Generally used as a handle to a thread.

• pthread_attr J Contains thread attributes, i.e., the parameters that control the behaviour of a given thread. It is not recommended that you populate them directly, but a number of APIs are provided by the 'pthread' library (or NPTL library) to manipulate them.

The following listing states a few of them:
• pthread_attr _init() and pthread_attr _destroy(): Allocates and destroys the structure.

• pthread_attr _setdetachstate() and pthread_attr _ getdetachstate(): Sets/gets the 'detachstate' attribute (whether the thread can be joined on termination or terminated independently).

• pthread_attr _setschedpolicy() and pthread_attr_ getschedpolicy(): Sets/gets the 'schedpolicy' attribute (FIFO scheduling/round-robin scheduling).
Creation and deletion: pthread_create() and pthread_exit() are the two basic APIs that could be used for this purpose. They are fairly simple to use and hardly need any explanation. Other APIs that could be used are:

• pthread-Join(): It blocks the current thread until another one terminates. It partially achieves synchronisation.

• pthread_cancel(): It cancels the execution of another thread.

Synchronisation: Being in a multi-threaded environment, there's no guarantee when a given thread would be invoked unless a proper 'synchronisation' mechanism is deployed. Synchronisation basically has two aspects. One is to make one thread wait until the other has completed a specific activity. Here I intend to highlight the scheduling aspect of the threads.

Name:  Manipulation on threads.jpg
Views: 190
Size:  31.1 KB

The second aspect of synchronisation is related to resource sharing between multiple threads. Here, for the caller it is not so important which thread grabs the resource first, but that the resource should be in a consistent state before it is taken by another thread. Yes, I am talking about 'mutual exclusion' here. Going back to implementation, the following are two different types of interfaces that are provided by the 'pthread' library (or NPTL library):

Synchronisation using condition/signal (Jar execution synchronisation)

• pthread_cond_init(): Initialises a 'condition' variable.

• pthread_cond_destroy(): Destroys a 'condition'
variable.

• pthread_cond_signal(): Signals a 'condition'.

• pthread_cond_wait(): Waits on a 'condition'.
The use of these APIs is fairly simple and can be easily understood by reading their manuals.

Synchronisation using mutex (for protection synchronisation)

• pthread_mutex_init(): Initialises the 'mutex lock' variable.

• pthread_mutex_destroy(): Destroys a 'mutex lock' variable.

• pthread_mutex_lock(): Attempts and acquires a 'mutex lock' (blocking call).

• pthread_mutex_trylock(): Attempts and acquires a 'mutex lock' (non-blocking call, one attempt).

• pthread_mutex_unlock(): Releases the 'mutex lock'. These APIs too are fairly simple and only require the man pages, to get you familiarised.

Thread local storage: It enables two threads referring to the same static or global variable that refer to different memory locations, thereby making the variable 'thread
local.