#include #include #include // Global variable and mutex int shared_variable = 0; pthread_mutex_t mutex; // Function to be executed by threads void *update_shared_variable(void *arg) { pthread_mutex_lock(&mutex); // Lock the access to the variable shared_variable++; // Update the shared variable printf("Updated value: %d\n", shared_variable); pthread_mutex_unlock(&mutex); // Unlock the access to the variable pthread_exit(NULL); } int main() { pthread_t thread1, thread2; // Initialize the mutex pthread_mutex_init(&mutex, NULL); // Create the threads pthread_create(&thread1, NULL, update_shared_variable, NULL); pthread_create(&thread2, NULL, update_shared_variable, NULL); // Wait for the threads to finish pthread_join(thread1, NULL); pthread_join(thread2, NULL); // Destroy the mutex pthread_mutex_destroy(&mutex); return 0; }