Running parralel threads?

Is it possible to run two parralel threads with seperate loops? I've tried using the thread however it only runs one thread at a time?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <thread>
using namespace std;
void go();
int main()
{
    int a = 0;
    thread start(go);
    start.join();
    while(a < 100) {
        a++;
        cout << 1;
    }
}
void go() {
    int x = 0;
    while(x < 100) {
        x++;
        cout << 2;
    }
}
This sometimes works? If you run it a couple of times it will run all 3 at the sametime displaying their numbers. However it usually doesn't?
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
// Example program
#include <iostream>
#include <string>
#include <thread>
using namespace std;
void go();
void snd();
int main()
{
    int a = 0;
    thread start(go);
    thread mang(snd);
    
    
    while(a < 100) {
        a++;
        cout << 1;
    }
}
void go() {
    int x = 0;
    while(x < 100) {
        x++;
        cout << 2;
    }
}
void snd() {
    int b = 0;
    while(b < 100) {
        b++;
        cout << 3;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int a = 0;
    thread start(go);
    thread mang(snd);
    
    // ...    

    // wait for the threads to finish
    start.join() ;
    mang.join() ;

    std::cout << "\ndone!\n" ;
}

http://coliru.stacked-crooked.com/a/eb7236d94a13ee2f
Why are my results mixed? For instance, itll sometimes mix the numbers together but sometime the numbers will be spread out like this, 111111,22222,33333, when they should be like 12323112312231?
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
// Example program
#include <iostream>
#include <string>
#include <thread>
using namespace std;
void go();
void snd();
int main()
{
    int a = 0;
    thread start(go);
    thread mang(snd);
    
    
    while(a < 100) {
        a++;
        cout << 1;
    }
    start.join();
    mang.join();
}
void go() {
    int x = 0;
    while(x < 100) {
        x++;
        cout << 2;
    }
}
void snd() {
    int b = 0;
    while(b < 100) {
        b++;
        cout << 3;
    }
}
A thread could stop execution at any time, at any point during execution, and another thread could begin execution and print some of its letters, which would make the output look jumbled. There is no guarantee as to when threads will be executed, you're at the mercy of the CPU scheduler.

One potential solution is to use a mutex: http://en.cppreference.com/w/cpp/thread/mutex

If you use a mutex, consider using lock_guard: http://en.cppreference.com/w/cpp/thread/lock_guard

That is A solution, not the only solution. I am sure there are better solutions. You can find more solutions with a Google search on the topic.
> Why are my results mixed? For instance, itll sometimes mix the numbers together
> but sometime the numbers will be spread out like this, 111111,22222,33333,
> when they should be like 12323112312231?

The stream buffers the output; std::cout << 1 ; is not an operation that would block.
To get the behaviour that you expect, put in a blocking call after a number is printed. For instance:
1
2
        cout << 1 ;
        std::this_thread::sleep_for(1ns) ; // sleep for at least one nano second  

http://coliru.stacked-crooked.com/a/5596bf0fdc1a3c32
Thanks @JLBorges it works!
However i'd still like to try JawhawksZombie's solution as it intrigues me and feel like it'd be nice to learn, thanks to all!

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
// Example program
#include <iostream>
#include <string>
#include <thread>
using namespace std;
void go();
void snd();
int main()
{
    int a = 0;
    thread start(go);
    thread mang(snd);
    
    while(a < 100) {
        a++;
        cout << 1;
        std::this_thread::sleep_for(1ns) ;
    }
    start.join();
    mang.join();
}
void go() {
    int x = 0;
    while(x < 100) {
        x++;
        cout << 2;
        std::this_thread::sleep_for(1ns) ;
    }
}
void snd() {
    int b = 0;
    while(b < 100) {
        b++;
        cout << 3;
        std::this_thread::sleep_for(1ns) ;
    }
}
For something like this, just making your thread sleep is fine. It isn't as complex as using a mutex.

I use mutexes to keep my threads from both trying to write to the same data/access the same data "at the same time". Let's say you had 2 (or more) threads accessing the SAME queue. You wouldn't want one pulling from the queue and the other pushing into the queue at the same time (trust me, you really don't).
Topic archived. No new replies allowed.