TMP Thread
TMP Thread
Race_condition
if we try to increment the mails variable in each thread 1000 times it will give the expect value 2000
but if we go further , we don`t get the expect value (2000000)
That is a race condition
//read mails
//increments
//write mails
but it can be
in this scenario, t1 arrives at 30 but t2 have stopped at 23 for some reason and this is an issue
c to assembler compilation
why it happen at 1million because if it is less the first thread finish its execution before the second
thread is created
Mutex (to prevent race condition
we can implements method like in minitalk create a lock and verify the value of it and take an
action depending its value like wait or actually increment
so to implemet the protection against some thread to read it when the thread actually use it we
surround it with
pthread_mutex_lock(&nutex);
//code to protect
pthread_mutex_unlock(&nutex);
but we must don`t use mutex whenever it is possible because it actually run only one thread so very
slow compare to multithread
when entering the loop, we expect that each thread receive 0,1,2…
but with multithread, the thread may not be create already but since we are passing pointer the value
of it may be change during the time like
like this
to correct the issue we have to store each memory of I and then free it after
here we have all single index not in order cause of the multithread but all single one
exemple of uses
Mutex trylock vs lock
for lock, it will lock one thread and then execute it one by one
for trylock it try to lock and then only one thread is lock and the other not
pthread condition
a condition variable is an identifier for a certain variable that you could either signal or wait on
signaling the condition may true
and the wait does unlock the mutex
Header Inclusions:
• <pthread.h>: Provides functions for thread creation, synchronization, and
management.
• <stdio.h>: Provides standard input/output functions like printf.
• <stdlib.h>: Provides general utility functions like sleep.
• <unistd.h>: Provides POSIX operating system related functions.
• <errno.h>: Provides error number definitions.
2. Global Variables:
• pthread_mutex_t mutexFuel: A mutex (mutual exclusion) object used to
control access to the shared fuel variable. Only one thread can acquire the mutex
lock at a time, ensuring exclusive access to fuel.
• pthread_cond_t condFuel: A condition variable used for signaling between
threads. In this case, the fuel_filling thread signals the car thread when fuel is
available.
• int fuel = 0: The shared variable representing the current fuel level (starts
empty).
3. fuel_filling Thread:
• This thread is responsible for simulating the act of filling the car's fuel tank.
• It runs in a loop 5 times:
• Acquires the mutexFuel lock to ensure exclusive access to fuel.
• Increments fuel by 15 (simulates adding fuel).
• Prints a message indicating the new fuel level.
• Releases the mutexFuel lock to allow other threads access.
• Signals the condFuel condition variable to notify any waiting threads (the
car thread) that fuel is now available.
• Sleeps for 1 second (simulates the time it takes to fill the tank).
4. car Thread:
pthread_cond_signal vs pthread_cond_broadcast
pthread_cond_signal only signals one thread to wake up and check whatever or not it has enough
fuel
multiple mutex