Advertisement

Thread questions

Started by June 10, 2000 06:07 PM
8 comments, last by SHilbert 24 years, 6 months ago
Just some questions about making different threads in Windows apps... 1. After the thread function returns, must you still call CloseHandle() ? 2. Does starting off another thread take away half of the primary thread''s time, or is it balanced out somehow? 3. Can you run two threads at once that use the same function? 4. If so, do the static variables in these functions work independantly or do they both access the same one??? I would assume that the answers are: 1. No 2. ??? 3. Yes 4. Yes ...but I''m away from a good reference and need some help. lntakitopi@aol.com | http://geocities.com/guanajam/
1.) No.

2.) No.
Ever check your system info? There are usually 50+ threads running at once. However, it does cost CPU cycles of course, so if it''s not speed critical:

-> SetThreadPriority(ThreadHandle,THREAD_PRIORITY_BELOW_NORMAL);

3.) Yes, because the code is never changed so no possible access violation.

4.) Yes, independently (I assume you mean the static variables within the function''s scope only). ;P




http://www.ill-lusion.com
laxdigital.com
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
Advertisement
Thanks, but I just tested #4 and it turns out they DON'T work independently!!! Here's the code:
    DWORD WINAPI ThreadFunc( LPVOID param ){static int b = 0;b++;char buffer[256];sprintf(buffer,"%d",b);MessageBox(NULL, buffer, NULL, MB_OK);return 1;}//....// In WinMain()HANDLE hThread1, hThread2;DWORD idThread1, idThread2;// Create the threadshThread1 = CreateThread( NULL, 0, ThreadFunc, 0, 0, &idThread1 );hThread1 = CreateThread( NULL, 0, ThreadFunc, 0, 0, &idThread1 );        

The first message box says "1", and the second box says "2". Is there something else I should be doing?

lntakitopi@aol.com / http://geocities.com/guanajam/

Edited by - SHilbert on June 10, 2000 10:58:46 PM
Local static variables are just syntactic sugar for global variables with visibility rules. Just like there is only one copy of global variables for all threads, there is only one copy of the static variables for all threads.
okay, so you would want to create a dynamic class/struct( with new) to hold data for each instance of a code and pass a pointer to it as the LPVOID parameter...

I stand corrected...

http://www.ill-lusion.com
laxdigital.com
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
one more thing,

of course you could just take the static keyword out, and then no problem...

http://www.ill-lusion.com
laxdigital.com
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
Advertisement
quote: Original post by Ziggy5000

one more thing,

of course you could just take the static keyword out, and then no problem...


But then you lose the persistent qualities, which is the main benefit of making static variables local to functions. (The other I can think of is if allocation/deallocation is expensive, for example on a large object.)

Static vars are global vars. (with name private to function)
Local vars are handled in registers and/or on the stack.
Every thread has it''s own stack, so local vars are not shared between threads. If calling a function from 2 threads - they both have their ow stack -> so they both have different local vars.

Threads will slow down your computer bigtime. Unless there''s no other option - don''t use em!

So, you don't need to close handles after the thread executes, eh? Why don't you all try this program at home:
#include &ltwindows.h>#include &ltstdio.h>DWORD WINAPI threadproc (LPVOID pVoid){  return 0; // thread doesn't do anything, just exits}void main (){    DWORD id;    while (CreateThread (NULL, 0, threadproc, NULL, 0, &id) != NULL);    printf("There is such a thing as a handle leak!!\n");}  


The "while" line should execute indefinitely, correct? Hmmm, looks like not.

Hate to say "shame on you" to people who answered incorrectly, but...

Edited by - Stoffel on June 12, 2000 7:04:35 PM
Whoah! that was stupid. I din''t mean to leave that static in there. Must have had too many Pepsis. Don''t worry, I know what static means.

lntakitopi@aol.com | http://geocities.com/guanajam/

This topic is closed to new replies.

Advertisement