Quantcast
Channel: Windows Critical Section strange behaviour - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Windows Critical Section strange behaviour

$
0
0

I have two shared global variables

int a = 0;int b = 0;

and two threads

// thread 1for (int i = 0; i < 10; ++i) {    EnterCriticalSection(&sect);    a++;    b++;    std::cout << a ""<< b << std::endl;    LeaveCriticalSection(&sect);}// thread2for (int i = 0; i < 10; ++i) {    EnterCriticalSection(&sect);    a--;    b--;    std::cout << a ""<< b << std::endl;    LeaveCriticalSection(&sect);}

The code always prints the following output

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

10 10

9 9

8 8

7 7

6 6

5 5

4 4

3 3

2 2

1 1

0 0

That is quite strange, looks like threads are working sequentally.. What's the problem with that?

Thanks.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images