-1

I want to execute DisplayWriters in another thread. Unfortunelly I am getting erorr: Invalid use of non static member function This is the code:

 void Chat::Init() {
    //some code

    thread writersThread(Chat::DisplayWriters);
}

void Chat::DisplayWriters() {
    for (auto &v : writers) {
        // do stuff
    }
}

I cant really do DisplayWriters static, because it is using writers list which is in Chat class. So what can I do with that?

David Konn
  • 29
  • 4
  • 3
    Non-static member functions needs to be called on a specific object. Try passing e.g. `this` as the second argument to the `std::thread` constructor. – Some programmer dude Jan 16 '18 at 13:35
  • 2
    A non-static member function needs to be invoked on an object of `Chat` type. Where is the thread supposed to get this object from? Also, a pointer to member **must** be formed with `&` – StoryTeller - Unslander Monica Jan 16 '18 at 13:36
  • 2
    Also note that you should [`join`](http://en.cppreference.com/w/cpp/thread/thread/join) threads you create (unless you [detach](http://en.cppreference.com/w/cpp/thread/thread/detach) them) to avoid a resource leak. – Some programmer dude Jan 16 '18 at 13:37
  • try `thread writersThread(&Chat::DisplayWriters, this);` (for using the same object) – JFMR Jan 16 '18 at 13:38
  • 1
    Possible duplicate of [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – jtimmons Jan 16 '18 at 13:38
  • 1
    @Someprogrammerdude or even std::terminate being called in the case of std::thread! – UKMonkey Jan 16 '18 at 13:38
  • @水飲み鳥 Is the `&` needed? – Gaurav Sehgal Jan 16 '18 at 13:42
  • @GauravSehgal it is if the member function is non-`static`. – JFMR Jan 16 '18 at 13:44
  • If you create a thread and after that, the scope of it is going to be finished, the destructor of it is called in which the join function is called. That is, this function does not run async-only on another thread-. – mahdi_12167 Jan 16 '18 at 14:23
  • @mahdi_12167 that's `std::future`. `std::thread` [terminates](http://en.cppreference.com/w/cpp/thread/thread/%7Ethread) if you let it die before joining. – Quentin Jan 16 '18 at 15:41

1 Answers1

-1

If really you want to run the DisplayFunction asynchrony, you should do something such this.

class Chat {
    thread* writersThread = nullptr;
void Init() {
    //some code
    writersThread = new thread(&Chat::DisplayWriters, *this);
}

void DisplayWriters() {
    for (auto &v : writers) {
        // do stuff
    }
}
~Chat() {
    if(writersThread) {
        writersThread->join();
        delete writersThread;
    }
}
};
mahdi_12167
  • 491
  • 4
  • 9
  • @Quentin: thread is not copyable and must be instantiated once. besides, after losing the scope-Init function- its join function would be called. – mahdi_12167 Jan 16 '18 at 15:13
  • But your class still isn't copyable (in fact, it is, which will lead to a nasty double-delete). `writersThread = std::thread{&Chat::DisplayWriters, *this}` would work just fine after a default initialization. I'm not sure what you mean with that `join()` part -- you could still `join()` inside the destructor just as well. – Quentin Jan 16 '18 at 15:16
  • `writersThread = std::thread{&Chat::DisplayWriters, *this}` could be run in function which is not constructor? – mahdi_12167 Jan 16 '18 at 15:19
  • Yes, of course. `std::sthread` is not copyable, but it is movable :) – Quentin Jan 16 '18 at 15:21
  • I would not have copyable class, I would set the not copyable thread which is not local. – mahdi_12167 Jan 16 '18 at 15:22