Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Last active March 5, 2024 22:13
Show Gist options
  • Save gustavorv86/51582381d4aaa5efe837a0a4a9daf663 to your computer and use it in GitHub Desktop.
Save gustavorv86/51582381d4aaa5efe837a0a4a9daf663 to your computer and use it in GitHub Desktop.
Shared a global variable into multiple threads.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef struct {
pthread_t thread;
int id_thread;
long sec_usleep;
} thread_info_t;
/** Shared variable by the threads */
static int __shared_variable = 0;
/** Mutual exclusion of the shared variable */
static pthread_mutex_t __mutex_shared_variable = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
/** Encapsulation "__shared_variable" */
void set_shared_variable(int value) {
pthread_mutex_lock(&__mutex_shared_variable); // Start critical section
{
__shared_variable = value;
}
pthread_mutex_unlock(&__mutex_shared_variable); // End critical section
}
int get_shared_variable() {
int value;
pthread_mutex_lock(&__mutex_shared_variable); // Start critical section
{
value = __shared_variable;
}
pthread_mutex_unlock(&__mutex_shared_variable); // End critical section
return value;
}
/** thread_task_1: increment "__shared_variable" */
void * thread_task_1(void * args) {
thread_info_t * thread_args = args;
int local_variable;
while (1) {
local_variable = get_shared_variable();
local_variable++;
set_shared_variable(local_variable);
printf("thread %d: set shared variable to %d \n", thread_args->id_thread, local_variable);
usleep(thread_args->sec_usleep);
}
}
/** thread_task_2: decrement "__shared_variable" */
void * thread_task_2(void * args) {
thread_info_t * thread_args = args;
int local_variable;
while (1) {
local_variable = get_shared_variable();
local_variable--;
set_shared_variable(local_variable);
printf("thread %d: set shared variable to %d \n", thread_args->id_thread, local_variable);
usleep(thread_args->sec_usleep);
}
}
int main() {
thread_info_t * thread_info_1;
thread_info_t * thread_info_2;
thread_info_1 = calloc(1, sizeof (thread_info_t));
thread_info_1->id_thread = 1;
thread_info_1->sec_usleep = 1000000; // 1 second
pthread_create(&(thread_info_1->thread), NULL, thread_task_1, thread_info_1);
thread_info_2 = calloc(1, sizeof (thread_info_t));
thread_info_2->id_thread = 2;
thread_info_2->sec_usleep = 1500000; // 1.5 seconds
pthread_create(&(thread_info_2->thread), NULL, thread_task_2, thread_info_2);
pthread_join(thread_info_1->thread, NULL);
pthread_join(thread_info_2->thread, NULL);
/*
Example execution:
thread 1: set shared variable to 1
thread 2: set shared variable to 0
thread 1: set shared variable to 1
thread 2: set shared variable to 0
thread 1: set shared variable to 1
thread 2: set shared variable to 0
thread 1: set shared variable to 1
thread 1: set shared variable to 2
thread 2: set shared variable to 1
thread 1: set shared variable to 2
thread 2: set shared variable to 1
thread 1: set shared variable to 2
thread 1: set shared variable to 3
thread 2: set shared variable to 2
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment