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?
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?
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(); ...
Source: https://ru.stackoverflow.com/questions/417099/
All Articles