0

I have this code so far :

#include <iostream>
#include <string>
#include <windows.h>
#include <iomanip>
#include <thread>
#include <chrono>
#include <atomic>

class test {

public:
    thread t1;
    test() : t1(&test::work, this) {
        
    }

    void work() {
        while (true) {
            cout << "in thread";
            Sleep(1000);
        }
    }


};

int main()
{
    test t;
    while (true) {
        cout << "in main" << endl;
        Sleep(1000);
    }

    return 0;
}

Question 1: Is this how I'm suppose to create a thread in a class object for one function only after I initialize the object?

Question 2: I've seen people write atomic_bool and run(), is that necessary? and what is it for?

Question 3: do I need to delete or join the thread or do any kind of memory management?

Problem How can I create the thread somewhere else in the function of the objects other than the constructor? I have did this and it didn't work

void work() : t1(&test::work, this) {
        while (true) {
            cout << "in thread";
            Sleep(1000);
        }
    }
John Sall
  • 1,027
  • 1
  • 12
  • 25
  • 1
    We can't tell, if this is the correct way for your needs, or if you need to delay the thread start. Also you'll need somehow to join your thread, maybe in `test`s destructor. – πάντα ῥεῖ Jan 03 '21 at 12:44
  • 1
    If you want the thread to end, then it might be a good idea to have some kind of status variable that the thread could check if it should exit or not. And to avoid data-races it makes sense to make it an atomic variable. And if the thread ends, you need to "reap" it, clean up the resources the underlying system (including the OS) might have created, which is done by joining it. – Some programmer dude Jan 03 '21 at 12:47
  • @Someprogrammerdude can I place the thread somewhere else other than in the constructor of the class? it's causing some problems – John Sall Jan 03 '21 at 12:53
  • Perhaps you should ask about the *actual* problems you have instead? And yes you can create threads in any function body. – Some programmer dude Jan 03 '21 at 12:55
  • 1
    This is a bit like asking how to use an `int` - it depends on what you want to do with it. – Galik Jan 03 '21 at 13:06

1 Answers1

1

Is this how I'm supposed to create a thread in a class object for one function only after I initialize the object?

It's one option. Another is t1([this]{ work(); }).

Relevant question: Start thread with member function

Be aware that, generally, the current instance (*this) may not be fully constructed when the thread starts executing.

I've seen people write atomic_bool and run(), is that necessary? and what is it for?

Don't know what run() is, but atomic variables are used for communication between threads via shared memory.

Do I need to delete or join the thread or do any kind of memory management?

No, you don't need any of these since your program never ends (at least normally).


How can I create the thread somewhere else in the function of the objects other than the constructor?

You can assign the thread member variable t1 a new value.

I would suggest reading some good book to learn such basics.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • Yes, I realized I have this problem now, I'm having issues because the current instance hasn't been fully constructed. So that's why I want to know how can I delay the initiation of the thread till I need it and not put it in the constructor – John Sall Jan 03 '21 at 13:14
  • I did this : t1 = [this] { work(); }; but it didn't work – John Sall Jan 03 '21 at 13:20
  • @JohnSall "_how can I delay the initiation of the thread till I need it_" - One option is to create a factory function that creates your object, calls a function that starts the thread and then returns the object. – Ted Lyngmo Jan 03 '21 at 13:30
  • @TedLyngmo can't I just create the thread inside one of the methods of the object and when I call that method, the thread starts? – John Sall Jan 03 '21 at 13:37
  • 1
    @JohnSall You need `t1 = std::thread( [this]{ work(); } );`. But you cannot do this inside `work` of course. – Daniel Langr Jan 03 '21 at 13:40