Mutual Exclusion (Mutex)
Simple implementation of the mutex from pthread library:
#include <pthread.h>
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *pushButtonThread(void *value)
{
pthread_mutex_lock(&lock);
// do something...
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
pthread_t button[2];
pthread_mutex_init(&lock, NULL);
// Create as many thread as you need
pthread_create(&button[0], NULL, &pushButtonThread, NULL);
pthread_create(&button[1], NULL, &pushButtonThread, NULL);
pthread_mutex_destroy(&lock);
return 0;
}Last updated