Asynchronised Behaviour in C using Threading
Note: This program uses POSIX threads.
The file async.c
contains an example to implement async behaviour in C. Both synchronous and asynchronous executions are shown in this piece of code.
By default, synchronous execution is enabled. To emulate the execution of a heavy task, the program sleeps for two seconds.
- Compile the program with POSIX thread flag.
gcc async.c -lpthread
- Run the program. The order of execution of functions will be printed as the output.
./a.out
- Use the
time
command to check the speed of the execution in terms of seconds.
time ./a.out
- Comment the lines
90
and91
. - Uncomment the lines
94
and95
. - Follow the steps in the above section (Synchronous Behavior).