简体   繁体   中英

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

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 eg 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.

There are a bunch of solutions.

For example, keep track of all the Process and Thread objects created. Store these each in a set<void*> , and check for the presence of that void* in the ProcessSet or ThreadSet . This solution just requires that you know where the objects are allocated.

Other approaches require some ability to deference.

Most obviously, if you have defined the types Process and Thread , give them a common base class and pass that around instead of a void* . This is basic OOP. You can then use RTTI to find the derived type. But most likely in this situation, a refactor/ redesign would obviate the need for this in the first place.

If you cannot add a base type, you could add a wrapper, and pass that around. This works even if all you ever see is a void*. This is similar to the set<> solution in that you require to know the type when it is allocated.

struct ProcessOrThread
{
    bool isProcess_;
    void* handle_;
};

All this really boils down to: If you know the type to start with, avoid throwing that information away in the first place.

What system are you talking about? On Linux, I would say your question does not make any sense, because processes don't have addresses (a pid_t as returned by fork or getpid is an integer).

You could use libraries which wrap processes and threads as objects, like Qt does (and it works on Linux, Windows, MaCOSX...). (and they you could eg use dynamic_cast or Qt meta object system , if you are sure the pointer points to either an instance of QThread or an instance of QProcess ).

您唯一能做的就是将type信息附加到进程/线程结构。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM