critical sections are generally only for blocking threads in a single process, there are other methods that span multiple processes. a spin lock may be a good idea though, i've used simple spin locks with success before, just using a 'long' variable, and windows 'interlocked' functions. in fact, you can pretty much implement a simple spin lock in three lines of code.
edit: here is one possibility for a spin lock:
1 2 3
|
long SpinLock = 0;
#define ENTER_LOCK_ while( InterlockedExchange( &SpinLock, 1 ) == 1 ){}
#define EXIT_LOCK_ InterlockedExchange( &SpinLock, 0 );
|
given the new info, I
may have a workable solution provided that you don't mind your polling thread to be inactive for short amounts of time.
but, who is cleaning up the memory allocated by the polling thread for the new data it is receiving? you said you don't have any control over the main app, so how then, do you know when it is done with the data and can delete it?
anyway, create a simple spin lock, and create two variables ( they will be used for signaling ). now, when the exported function is called, it would set the first signal variable, then spin until the second is set. Now, in your polling thread (either before or after what takes place in the loop but in the loop) you check the first signal variable, if it is set, then also set the second signal variable and spin until the first is reset ( thus allowing the function to retrieve the data it needs ). then, at the end of the function, reset the second, then first signal, and return, and your polling thread can continue on it's way. The way that i described, because you are only checking the signal once each time through the polling thread's loop, even if the exported function is called very very frequently, the polling thread would still execute it's loop once through all the way before allowing the function to run. and that way, even if the function is being called very frequently, your polling thread should also be looping frequently.
NOTE: 1) make sure all of those signal accesses are protected by either a spin lock, or possibly just use 'interlocked' functions. 2) This is what i think will work
in theory, since i am not actually writing the code and running it, I can't attest to it's functionality.