The three C files in this directory must be compiled with the -lpthread option, as in ”$ cc condsignal.c -lpthread -o condsignal”. You can read
the GitLab CI script for details.
The C++ files must be compiled with the options -pthread -std=c++11, as in
”$ g++ -pthread -std=c++11 mutex.cpp -o mutex2”.
Complicated example that shows how to use condition signalling between threads.
This example shows the mutual exclusion technique, used to allow threads to share resources.
pthread_t tid[4];
int counter;
pthread_mutex_t lock;
void *handler(void *arg) {
pthread_mutex_lock(&lock);
counter++;
printf("Job %d has started\n", counter);
sleep(2);
printf("Job %d has finished\n", counter);
pthread_mutex_unlock(&lock);
return NULL;
}This more complicated example shows how to use the C++ standard threads library’s mutual exclusion.
- Usage:
./mutex2 [number of threads] - Example:
./mutex2 10
Simple threads example that prints each thread’s ID.