0% found this document useful (0 votes)
17 views12 pages

TMP Thread

Uploaded by

Nathan Andri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

TMP Thread

Uploaded by

Nathan Andri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

when doing (mails ++) , it `s actually do

//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

That`s the race condition

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

we have mute in pthread to do these action


to use it we need to declare a pthread_mutex_t then initiate it then in the end destroy it

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

pthread_ create in a loop

pthread_create and pthread_join in a different for loop

get return value from a thread


pass arg to a thread

this is the main id

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:

• This thread simulates a car waiting for fuel.


• It acquires the mutexFuel lock to ensure exclusive access to fuel.
• It enters a loop that continues as long as the fuel level is less than 40 (the car needs
at least 40 units to proceed).
• Prints a message indicating that there's no fuel and the car is waiting.
• Waits on the condFuel condition variable. This releases the mutexFuel
lock (implicit in pthread_cond_wait) and waits for a signal from the
fuel_filling thread. When signaled, the car thread reacquires the
mutexFuel lock before continuing (to maintain exclusive access).
• Once there's enough fuel (fuel >= 40), the car "takes" 40 units of fuel.
• Prints a message indicating that the car has filled up and shows the remaining fuel
level.
• Releases the mutexFuel lock to allow other threads access.
5. main Function:

• Creates an array of two threads (th).


• Initializes the mutexFuel and condFuel objects.
• Creates two threads using pthread_create:
• The first thread runs the car function.
• The second thread runs the fuel_filling function (created after the car
thread to simulate the car initially having no fuel).
• Waits for both threads to finish using pthread_join.
• Destroys the mutexFuel and condFuel objects.
• Returns 0 to indicate successful program termination.
Overall, this code demonstrates:
• Thread Creation: Using pthread_create to create threads.
• Mutual Exclusion: Using a mutex (mutexFuel) to ensure only one thread can access the
shared fuel variable at a time, preventing race conditions and data corruption.
• Condition Variables: Using condFuel to synchronize the fuel_filling and car
threads. The fuel_filling thread signals when fuel is available, and the car thread
waits on this signal.
• Resource Management: Using pthread_mutex_destroy and
pthread_cond_destroy to release resources associated with the mutex and condition
variable objects when they are no longer needed.

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

You might also like