Do threads encapsulate functions?

Hello World!

I had hoped to create a radio scanner object which when created would create its own window, callback function, and thread in which to execute. A main control would then oversee several scanners each controlling a receiver.

Alas, there are difficulties doing that since callback functions and threads are static function and they cannot be made member of a class without being declared as 'static' which defeats the purpose of encapsulating them.

Now the question was what happens if several threads are created and each accesses the same function; the only difference is that a a different pointer is passed to the thread which then is passed to the function.

Does the thread encapsulate the function, in other words, is there a unique copy of that function made in memory for each thread?

It seems to be the case, but I am not sure. I tried a simple example where three threads are created each being passed a structure pointer containing the handles to a window, virtual window, and it device handle. The function called contained an infinite loop that does nothing but draw different colored ellipses that fill the window and continuously recycle.

If the function was "encapsulated" then I should see ellipses generated in each of the windows. And that I did. However, not all of the virtual windows seemed to get filled. Sometimes only one did, sometimes two, and sometimes all three.

Does anybody know how Windows threads really handle functions?

Thanks for any help and if you even made it through this long thread!
[1617 byte] By [Gyannea] at [2007-11-18 0:24:19]
# 1 Re: Do threads encapsulate functions?
Code segments need not be separated, since each thread gets its own register context (stack, etc.). It is perfectly acceptable to encapsulate a thread, keeping the thread function static and using the class to maintain state for each thread. The thread function can be passed a pointer to a state structure (this, or better, a separate contained state structure created each thread creation). You can use such objects to build complex threading structures, with control and branching mechanisms, etc. Encapsulation, in my opinion, is the only way to go for true multithreading professional power. However, worrying about whether each function is separate is unnecessary, since the register contexts are different for each thread, thus separating the executions. Synchronization between shared access is of course always necessary.
galathaea at 2007-11-9 13:02:24 >
# 2 Re: Do threads encapsulate functions?
1. You can make your thread functions static members of a class. To access the non-static members of that class, you'll have to send the pointer of the appropriate object in the lParam parameter of CreateThread(). You can typecast this pointer in the thread function to the class type, and access all the members of that class from the function.

2. When you create several threads running the same code, the code is not replicated. All threads execute the same code residing in the same memory. In other words their code segment remains the same. However, each thread gets a stack of its own. So all the local variables you create in your thread function have a unique copy for each thread. The data segment also remains the same for all threads, so all the global and static variables are shared resources for the threads. So is the local heap, since it resides in the data segment.
Umair Ahmad at 2007-11-9 13:03:24 >
# 3 Re: Do threads encapsulate functions?
Originally posted by Umair Ahmad
1. You can make your thread functions static members of a class. To access the non-static members of that class, you'll have to send the pointer of the appropriate object in the lParam parameter of CreateThread(). You can typecast this pointer in the thread function to the class type, and access all the members of that class from the function.

Just a small correction...there exists no 'lParam' argument of 'CreateThread()'...I think you rather meant the 'lpParameter' argument... :cool:
Andreas Masur at 2007-11-9 13:04:30 >