3

Decide whether it is a process or a thread in C ++

 2 years ago
source link: https://www.codesd.com/item/decide-whether-it-is-a-process-or-a-thread-in-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Decide whether it is a process or a thread in C ++

advertisements

Give a void * variable as input (can only point to a process or thread), I'd like to first determine its type and then convert it to that type.

How should I do that in C++? I know it's a dumb question, but I've never done C/C++ before and can't think C/C++ way yet.

EDIT: I need to achieve this on both Linux and Windows.


You can't. Pointers carry two pieces of information: the location in memory to where they point and the type of the pointed object. With a void * this last information is omitted, and there's no way to reconstruct what type it pointed to. So, you need to carry along this pointer another value that specifies what it actually points to (you can use e.g. an enum).

The only facility somehow related to this task in C++ is RTTI, but it works only on pointers to polymorphic classes (RTTI usually exploits the vtable of the object to store additional information about the dynamic type of the pointer, but the vtable can be accessed and correctly interpreted only if it is known that the object belongs to a particular polymorphic class hierarchy).


I'm looking for a uniform way to pass pid or tid in but will treat the ids differently. Sorry, I might not properly state my problem.

Well, this is a completely different thing... if you need to pass around your PID/TID inside a void * you could simply create a struct or something like that with a member for the ID and one to store if such ID is a PID or a TID.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK