CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mu;
int change = 0;
void printConsecutiveNumbers(int start, int end, int consecutive){
int x = start;
while (x < end){
if(change % consecutive == start){
unique_lock<mutex> locker(mu);
cout << "Thread " << start+1
<< " " << this_thread::get_id()
<< " -> "
<< x+1 << endl;
x += consecutive;
change++;
//to counter overflow
change %= consecutive;
}
}
}
int main(){
//change num = 2 for printing odd and even, more for threads > 2
const int num = 4;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run