I create a thread:

std::thread *thr = new std::thread(&MyClass::threadFunc, this); 

How now in the function MyClass::threadFunc() to get a pointer to the thread that caused it?

  • one
    Why do you need this? There is no standard way. - ixSci
  • to clean it from the main object list ... - sitev_ru
  • 3
    You were offered one option, but, in my opinion, something is wrong with you. Can you elaborate in more detail with the code what you want to achieve? - ixSci

1 answer 1

If the stream could be started stopped, then

 struct Args { MyClass* cls; std::thread* thread; }; auto args = std::make_shared<Args>(); args->cls = this; args->thread = new suspened_thread(&MyClass::threadFunc, args); resume_thread(args->thread); 

But there is no such thing in the standard library, so you need to use some kind of synchronization object, for example std::future :

 struct Args { MyClass* cls; std::future<std::thread*> thread; }; std::promise<std::thread*> promise; auto args = std::make_shared<Args>(); args->cls = this; args->thread = promise.get_future(); promise.set_value(new std::thread(&MyClass::threadFunc, args)); void threadFunc(std::shared_ptr<Args> args) { auto thread = args->thread.get(); ...