Inlining a thread handler ?

Has anybody got any information on inlining a global thread function ? It's not a class member, but it's invoked from an inline class member. This in itself will stiil mean that there's just one instance of the thread function in the compiled exe. Is there a way to inline the thread handler ? e.g.

UINT MyThread(LPVOId lp)
{
// ...
}

inline CMyClass::StartThread()
{
AfxBeginThread(MyThread, lp);
// ...
}

I want the MyThread code inserted inline every time it is called.
[550 byte] By [jase jennings] at [2007-11-18 1:40:29]
# 1 Re: Inlining a thread handler ?
My gut reaction is no, so I checked MSDN which states:

pfnThreadProc
Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows:

UINT MyControllingFunction( LPVOID pParam );

Since that param is a function pointer, I highly doubt that it can be inlined. I'm not sure what you'd hope to accomplish by doing so anyway...
bytz at 2007-11-10 8:54:14 >
# 2 Re: Inlining a thread handler ?
code repetition, that's what i'm looking for.
jase jennings at 2007-11-10 8:55:12 >
# 3 Re: Inlining a thread handler ?
Originally posted by jase jennings
code repetition, that's what i'm looking for.

:confused: :confused: :confused:
Not sure, but good luck anyway... Just a thought, hmmm, no wouldn't work... was thinking that you could inline the body of the threadproc, but that would only do it once...
bytz at 2007-11-10 8:56:11 >
# 4 Re: Inlining a thread handler ?
I would agree to the given point...no.

Your 'Create()' function will be inlined but not the thread function...
Andreas Masur at 2007-11-10 8:57:16 >
# 5 Re: Inlining a thread handler ?
Just as i suspected. I just wondered if it may be possible. Perhaps with a different thread mechanism ?
jase jennings at 2007-11-10 8:58:14 >
# 6 Re: Inlining a thread handler ?
why can't you just call your thread function with out creating a thread?
mwilliamson at 2007-11-10 8:59:13 >
# 7 Re: Inlining a thread handler ?
no reason ...

just wanted to know if it was possible
jase jennings at 2007-11-10 9:00:15 >
# 8 Re: Inlining a thread handler ?
It is my understanding that it is desired to have a separate copy of the actual thread code for each instance of the thread....

If this is incorrect, just ignore the remainder of this post...

Using the compiler and inlining will not directly achieve this capability. There are however two possible methods.

1) Write your actual thread procedure as an __inline. Then create some simple wrappers that invoke the inline procedure. Each wrapper will have a unique address that can be used to instanciate a thread, each thread will be running a unique instance of the actual thread procedure.

2) If you need to have an un-known number of instances, it will take a little more work. You can first write your actual thread procedure without rquiring in-lining. You then manually allocate memory, copy the function to it, mark the allocated page as executable, and start the thread on the dynamically allocated page.

If there is a REAL reason for needing either of these techniques, I can provide additional information. If this is a mental exercise, I will leave the implementation detailed to the reader.

Hope this helps.
TheCPUWizard at 2007-11-10 9:01:19 >