Please enable JavaScript.
Coggle requires JavaScript to display documents.
POSIX Threads (Thread Attributes (Detachedstate (Allows to avoid the need…
POSIX Threads
Thread Attributes
Detachedstate
Allows to avoid the need for the threads to rejoin. The two possible values for the flag is pthread_create_joinable and pthread_create_detached. When state is detached you cannot use pthread_join to recover exit state of another thread.
Schedpolicy
Controls how threads are scheduled. The options are pthread_other, pthread_RR, and pthread_fifo. By default the attribute is set to other. The other two types are only available for processes running under superuser permissions.
Schedparam
Partner to schedpolicy and allows control over scheduling of threads running with schedule policy Sched_Other.
Inheritshed
This attribute can take two values pthread_explicit_sched and pthread_inherit_sched. By default the value is the explicit scheduling that sets the scheduling explicitly with the attributes. The inherit option sets the scheduling based on the one its creator was using.
-
Stacksize
Controls the thread creation stack size, set in bytes. This is part of the optional section of the specification. Linux implements threads with large amount of stack by default, so the feature is redundant.
Advantages of Threads
Very useful to make a program appear to do two things at once. For example real time word count were one thread counts the words while the other is editing the text.
Performance of an application that mixes input, calculation, and output may be improved by running these as separate threads.
-
Switching between threads is less work for the OS than switching processes. Also multiple threads are less demanding than multiple processes on resources.
Disadvantages of Threads
Writing multithreaded programs requires very careful design. The implementation of threads is also known as ¨How to shoot yourself in both feet at once¨.
Debugging a multithreaded program is much harder than debugging a single-threaded program, because interactions between threads are hard to control.
Splitting calculations into threads will not necessarily run more quickly on a single processor machine, unless the machine has multiple cores and allows multiprocessing truly.
Canceling a Thread
Sometimes you want that a thread is able to terminate another thread. Given a thread identifier you can request for it to be canceled. On the receiving of the cancel signal things are more complicated.
A thread can set is cancel state. First is pthread_cancel_enable which receives cancel requests or pthread_cancel_disable that ignores them.
If a thread is accepting cancel signals then it has another level of control called cancel type. This types can be asynchronous or deferred. When asynchronous the thread acts immediately after receiving the cancel signal. When in deferred the thread waits until the thread executes a cancellation point.
Thread
Multiple strands of execution in a single program are called threads. All processes have at least one thread of execution.
When a process executes a fork, a new copy of the process is created with its own variables and PID, the new process is scheduled independently. In contrast, when we create a new thread it gets its own stack but shares global variables, file descriptors and signal handlers.
Synchronization
Semaphores
A semaphore is a special type of variable that can be incremented or decremented, but crucial access to the variable is guaranteed to be atomic. This means if two threads attempt to change the value of a semaphore, the system guarantees that operation will take place in sequence.
Semaphores are used to protect a piece of code so that only one thread can run it at a time. A binary semaphore is used for this. If you want to permit a limited number of threads to execute the piece of code then a counting semaphore is used.
A semaphore is created by the sem_init function. The function initializes a semaphore object pointed to by sem. The sem_post function increases atomically the value of the semaphore by one. The sem_wait function decreases atomically the value of the semaphore by 1. The last semaphore function is sem_destroy this function is used when you have finished using the semaphore.
Mutex
They act by allowing the programmer to lock an object so that only one thread can access it. To control access to a critical section you lock a mutex before entering the code section and unlock it after leaving.
The function used for mutex locks are vey similar to those used on semaphores. They are pthread_mutex_init used to initialize a mutex lock. Also pthread_mutex_lock to lock a mutex and pthread_mutex_unlock to unlock a mutex. Last one is pthread_mutex_destroy used to disable a mutex lock.