threads1.cpp
433 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <thread>
#include "mutex.h"
int Counter=0;
void
thread1 ()
{
for (unsigned int i = 0; i < 100000; i++)
{
Counter++;
Counter--;
}
}
void
thread2 ()
{
for (unsigned int i = 0; i < 100000; i++)
{
Counter++;
Counter--;
}
}
int
main ()
{
std::thread t1(thread1);
std::thread t2(thread2);
t1.join ();
t2.join ();
printf ("Counter=%d\n", Counter);
}