based solely on the code you posted i have a few ideas that may be giving you grief (also a few suggestions below):
I don't really see a problem with your call to CreateThread(), aside from possibly 'I)' below, but a compiler should catch that. Multithreaded apps are notorious for being difficult to debug, but hopefully an idea below may help you as I don't see an obvious problem with your CreateThread() call.
NOTE: i would just like to say, i have no idea how experienced a programmer you are, so if anything below seems 'duh' or redundant, i do very much apologize.
1) as soon as get_request returns a NULL value, your loop will exit, and the function will subsequently exit, this may or may not be the behavior you want (i.e. - you may want to continue listening for messages even though you don't have any to process in the mean time). Also, just make sure that get_request is allocating memory for your request_t struct, since 'request' is only a pointer to a structure, and must have memory allocated for it to actually store valid data, otherwise, you may be overwriting other memory.
2) which brings me to the next possibility: you are passing a
pointer to the CreateThread() function that points to the 'request' structure. however, each pass through the while loop re-initializes that variable, thus depending on how long it takes each thread to process that information, or the thread scheduling on a certain machine or processor, and whether or not the thread copies that information to local storage, it may, or may not be using valid information. (see #3 just below also).
3) finally, related to #2 above: since I don't know the specific implementations of some of your functions... you may need thread synchronization. meaning, if two threads try to write to the file you have open, or change variables or memory, you may be getting problems there. above, i used the 'request' variable as an example. you need to lock out all but one thread as it accesses that memory, then release that memory, store new information in it, and allow the next thread to access it. even though it is just a pointer, if that pointer is changed before a thread/function is done with it, the result would be uncertain. a simple google search for "thread synchronization" should give you info if you need some, there are articles from MSDN, codeguru, and codeproject in the first several results.
here are a few other things that may help you:
I) probably just a typo, since a compiler should catch it... in your call to CreateThread(), the third parameter is a
pointer to a thread function, not just the name of the function. You could use either
&process_request_thr
in the call, or even
LPTHREAD_START_ROUTINE lpThreadFunctionName = &process_request_thr;
before, then pass
lpThreadFunctionName
as the third parameter in the function call.
II) you should test 'newThread' to make sure that CreateThread succeeded and actually created a new thread. msdn doc on CreateThread: <
http://msdn.microsoft.com/en-us/library/ms682453.aspx> see 'return value' section. You can even use GetLastError() to find out what happened if no new thread was created.
III) it may also be beneficial to check 'j' for some error to make sure process_request() did not fail.
IV) i see that you are trying to be good about memory, by doing
free(request);
but if you look carefully at your conditional statements, i believe this can get executed even when request == NULL! this can be taken care of by just taking it out of the else and putting it in an
if( request != NULL ){free(request);}
after.
ps - again, i'm sorry if any of this is obvious to you. take care, and good luck!