views:

480

answers:

4

OK, I'm a bit confused here. The following code works:

HANDLE CreateSideThread()
{
    DWORD dwGenericThread;
    HANDLE hThread1 = CreateThread(NULL, 0, CallBackFunc, NULL, 0, &dwGenericThread);

    return hThread1;
}

int main()
{
    HANDLE Thread1;

    Thread1 = CreateSideThread();
    WaitForSingleObject(hThread1, INFINITE);

    SomeOtherFunction();

    return 0;
}

The program does other things but you get the idea. It basically creates a new thread and executes whatever it is in CallBackFunc (which is an endless loop that check db status). Now, if I remove WaitForSingleObject() then the program will not even try CallBackFunc once and execute SomeOtherFunction(). What's the point then of a thread? I mean, i'm confused here.

What I am trying to do is call that thread with the check for the database status and keep that thread going while I continue with my program, calling other functions.

What am I doing wrong? Please post a sample snippet.

Thanks

+1  A: 

Threads are typically used for doing background work and freeing up the calling thread to do other things.

Normally, the behavior you describe (calling SomeOtherFunction()) is exactly what you'd want: I am going to kick of a background 'job' and go about my life.

It would appear that your example is just fine - though if you merely return from main() your thread will of course terminate (as it's owned by the parent process).

Maybe some more detail on why what you're doing isn't what you expect to happen?

DarkSquid
Thanks. I'm not going to post all the code, but SomeOtherFunction() has an endless loop that copies data around. it continues doing this until that other thread writes to a specific file. That causes the loop to break and exit.
wonderer
+4  A: 

Without the WaitForSingleObject, your call to SomeOtherFunction() probably returns quickly enough for the program to exit before the new thread even gets a chance to run once.

When a C program returns from its main() function, the runtime system calls exit() for you. This forcibly exits your program even if other threads are trying to run at the same time. This is in contrast to other languages like Java for example, where exiting the main thread will not exit the process until all other (non-daemon) threads have finished running too.

Greg Hewgill
SomeOtherFunction() has an endless loop that does not break unless the other thread writes a file
wonderer
Ah, you hadn't mentioned that. When you say "endless", does `SomeOtherFunction()` ever sleep? Or is it a hard loop that takes up 100% of one CPU? If the latter, then your side thread may not be getting enough time slices to run very quickly.
Greg Hewgill
Idd smells like thread starvation.
Yannick M.
it sleeps(1000) so it's ok.
wonderer
Ok, i found that the main thread was exiting before predicted. that's all. Thanks for help
wonderer
A: 

CallBackFunc will of course be called, but there is no guarantee, when your stared threads will be up and running. They will be working once, but its unpredictable when the start doing so. Thats the job and property of the systems scheduler. In your case they do not anything when the second function is already called.

RED SOFT ADAIR
+1  A: 

What you're finding is that the main thread completes before you notice CallbackFunc is called. When you have the Wait call in, the main thread is blocked until the new thread finishes and so you see the thread func being executed.

Threads are not as cheap as you think, if you replace the SomeOtherFunction with something that takes a long enough time to run, you'll see your thread function being called even without the Wait call.

gbjbaanb