2

I've got 2 class' where in the constructor of each spawns a thread which just prints hello world or goodbye universe. My goal is for the program to print out both hello world and goodbye universe at the same time. Problem is the program currently waits for the first thread to finish before starting the second. Its basically thread 1 is blocking the creation of threa2 until it finishes. What is the correct way for both threads to be executing at the same time?

My code is

#include <iostream>
#include "voltage.h"
#include <thread>

class MyClass final
{
private:
    std::thread mythread;
    void _ThreadMain()
    {
    int x = 1000;
    while(x>0)
    {
        std::cout << "hello world " << x << std::endl;
        x--;
    }
    };
public:
    MyClass()
        : mythread{} 
    {
        mythread = std::thread{&MyClass::_ThreadMain, this};
    mythread.join();
    }
};

class MyClass2 final
{
private:
    std::thread mythread;
    void _ThreadMain()
    {
    int x = 1000;
    while(x>0)
    {
        std::cout << "goodbye universe " << x << std::endl;
        x--;
    }
    };
public:
    MyClass2()
        : mythread{}
    {
    mythread = std::thread{&MyClass2::_ThreadMain, this};
    mythread.join();
    }
};


int main(int argc, char *argv[])
{
    MyClass *myClass = new MyClass();
    MyClass2 *myClass2 = new MyClass2();
    return 0;
}

My compile arguments are

g++ -g  -march=armv6 -marm -I Sources/  main.cpp -L libraries/ -lyocto-static -lm -lpthread -lusb-1.0

Most of that is for other parts of the program i'm working on

Root0x
  • 472
  • 1
  • 9
  • 28

1 Answers1

4

Start threads in constructors and call join methods in destructors of your classes:

MyClass()
    : mythread{} {
    mythread = std::thread{&MyClass::_ThreadMain, this};
}

~MyClass() {
    mythread.join();
}

MyClass2()
    : mythread{} {
     mythread = std::thread{&MyClass2::_ThreadMain, this};
}

~MyClass2() {
    mythread.join();
}

but then you need to add in main the following lines

delete myClass; // wait until thread  started in myClass ends
delete myClass2; // wait until thread started in muClass2 ends

to force destructors to be called.

rafix07
  • 20,001
  • 3
  • 20
  • 33
  • Problem is that runs hello world or goodbye universe first and then waits for it to finish until starting the other. I want them to print at the same time – Root0x Nov 06 '18 at 11:39
  • I'm running this on ARM if that makes any difference – Root0x Nov 06 '18 at 11:40
  • Have you tested this code? First thread is started when `new MyClass` is called, it runs and executes, the second thread is started when `new MyClass2`, at the end of main you call `delete myClass` and `delete myClass2` to wait both threads end. So it should work concurrently. – rafix07 Nov 06 '18 at 11:43
  • https://pastebin.com/uaMdWUiV this is an example of the output that i've got. For some it seems to of done goodbye universe once during its hello world block but that could be expected behavior from the scheduler so i'm assuming this is working. Thanks for the help – Root0x Nov 06 '18 at 11:48