S
S
sitev_ru2015-04-16 14:25:01
C++ / C#
sitev_ru, 2015-04-16 14:25:01

How to get a pointer to the thread that called it in a function?

I create a thread (thread):
std::thread *thr = new std::thread(&MyClass::threadFunc, this);
How now in the MyClass::threadFunc() function to get a pointer to the thread that called it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2015-04-16
@vt4a2h

In the standard library, you can get the thread id. You can try to compare it with a pre-stored id.
In Qt, you can get a pointer to a thread like this:
or find out which thread an object is in:
QThread * QObject::thread() const

D
DISaccount, 2015-04-16
@DISaccount

Do you want to have a std::thread* pointer inside MyClass?
-Make this pointer global.
- Pass the pointer through the class method MyClass.
-Make a global function to access this pointer.
-Make a global singleton class to access this pointer.
-Make a class method that spawns this very thread, which is a member of the class.
- etc.

A
AxisPod, 2015-04-17
@AxisPod

Not really, but why would you even need it? For sleeping there is std::this_thread. Well, calling the methods of the thread object from the thread itself is pretty crazy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question