[try Beta version]
Not logged in

 
race condition

Aug 7, 2020 at 10:44pm
Hi! how do I make the following program print numbers in the correct order like this: 0 1 2 ... 100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <thread>

int x = 0;

void foo() {
    for (int i = 0; i < 100; i++) {
        x++;
    }
}

void bar() {
    for (int i = 0; i < 100; i++) {
        std::cout << x << std::endl;
    }
}

int main() {
    std::thread first(foo);
    std::thread second(bar);

    first.join();
    second.join();
}


UPDATE:
the following code worked for me but I'm sure there has to be a better way to do it
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
#include <iostream>
#include <thread>

int x = 0;

bool can_print = true;
bool can_increment = false;

void foo() {
    for (int i = 0; i < 100; i++) {
        while (!can_increment);
        x++;
        can_increment = false;
        can_print = true;
    }
}

void bar() {
    for (int i = 0; i < 100; i++) {
        while (!can_print);
        std::cout << x << std::endl;
        can_print = false;
        can_increment = true;
    }
}

int main() {
    std::thread first(foo);
    std::thread second(bar);

    first.join();
    second.join();
}
Last edited on Aug 7, 2020 at 11:34pm
Aug 8, 2020 at 1:23am
Apart from printing even numbers, it's not clear what you're trying to achieve as both versions of your code use threads.

Perhaps if you explain what your concern is, someone might be able to offer something helpful.
Aug 8, 2020 at 3:14am
idk what you're talking about, what I want to do is pretty obvious, however, if you don't understand it somehow compile and run the second program and ye shall see what I want.
Last edited on Aug 8, 2020 at 3:15am
Aug 8, 2020 at 4:31am
https://www.cplusplus.com/reference/condition_variable/condition_variable/
After each x++, you do a set-and-notify.
Before each print (in the other thread), you do a test-and-wait.
Aug 8, 2020 at 4:47am
I believe that last code has undefined behavior. In particular, the behavior is unpredictable in NUMA architectures. If should work fine if the bools are changed to std::atomic<bool>.
Aug 8, 2020 at 9:35am
idk what you're talking about

You can't post buggy code and expect someone to understand what your trying to do, if it was correct you might not have questions.

It's better to communicate what you want to do, then someone will be able to fix what you've posted.

The 2nd version of your code is wrong in principle. There are race conditions around the use of those global variables. You're lucky if you get the same results twice in a row.
Aug 8, 2020 at 11:38am
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
#include <iostream>
#include <thread>
#include <mutex>

int x = 0;
std::mutex fooLock, barLock;

void foo() {
    for (int i = 0; i < 100; i++) {
	fooLock.lock();
        x++;
	barLock.unlock();
    }
}

void bar() {
    for (int i = 0; i < 100; i++) {
	barLock.lock();
        std::cout << x << std::endl;
	fooLock.unlock();
    }
}

int main() {
    fooLock.lock();		// bar() goes first
    std::thread first(foo);
    std::thread second(bar);

    first.join();
    second.join();
}

Topic archived. No new replies allowed.