Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.org

thread

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”.

Condition Signal

Complicated example that shows how to use condition signalling between threads.

Mutex (C)

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;
}

Mutex (C++)

This more complicated example shows how to use the C++ standard threads library’s mutual exclusion.

mutex.cpp

  • Usage: ./mutex2 [number of threads]
  • Example: ./mutex2 10

Threads

Simple threads example that prints each thread’s ID.