How to write a program that increasingly and every once in a second prints 1 integer variable in 2 separate threads(using multithreading)?
1
Expert's answer
2013-01-15T08:49:27-0500
#include <conio.h> #include <iostream>
#include <windows.h> #include <pthread.h>
#define sleep Sleep
using namespace std;
const int max_number = 16;
int number; /* Use mutex to prevent the simultaneous access to the number variable from different threads */ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* print_number(void* arg) { while (number < max_number) { pthread_mutex_lock(&mutex); cout << number << endl; number++;
Comments
Leave a comment