who created me?

Started by
2 comments, last by Xai 17 years, 8 months ago
i'm currently working on a win32 app (asset mng). to make app "livly" we decided to transfer all the heavy work into queued tasks, which can run in multiple worker threads. one problem i currenlty have, that once the tasks are created and stuffed into a queue, i have no idea who created them. the first solution i tried was to use the built-in macros __file__, __function__, __line__, from which i created a string, and passed this string via a macro to a overloaded task creating function. so, now when task is being executed, i just take a look of this string, and know who created it. but this seems so ugly to me... is there a nicer way? are the __X__ macros only way to find out the "callers id"? what bout going back on the stack? ie, if u create a wrapper function, is it posible to go back (unwind)?
Abnormal behaviour of abnormal brain makes me normal...www.zootfly.com
Advertisement
Do you want to know who created them, or where they were created?

If it's the former, good design would involve passing the creator object to the constructor of each task.
hmm, in this case who almost equals where... but, function id would be quite nice :)
Abnormal behaviour of abnormal brain makes me normal...www.zootfly.com
Since C has no normal metadata (beyond the compile time macros) there is no native support for knowing anything about a "callstack" or the location or execution at any time. There is not even any standard access to simply outputing the values of things like the instruction or stack pointers, because there is no such thing in C's abstract machine - these are platform details, and their are platform specific ways to access them (In your case x86 assembly and / or Win32 debugging API).

But, if you are putting items into a queue and the recipient wants to know WHO/WHAT/WHERE put the item in the queue, then you SHOULD define the source as an element of the message in the queue. Part of the explicit interface of using the queue (much as threads have names, because the OS wants them to). It is NOT a hack. The only part that is hacky is using the macros to construct your function's source string ... which could be done a dozen different ways depending on how many such function you use and how volatile they are. If they are fairly stable you could simply use a custom formatted string - handcoded or built using a more advanced C+ preprocessor than the one provided by the langauge (something like "Class::Method").

Of course you realize that while using method identification might be what you want today, you might want something else tommorow (something that includes the source thread or user id ... etc ... so your best bet is to have something like: GetSourceString(x, y, z) that you use religeously when you put items into the queue - so that in the future when you want to change the rules you can use compile time errors and/or find-replace-in-files to help you migrate all of your client code to your new system.

This topic is closed to new replies.

Advertisement