Win32 Thread Interleaving  
Author Message
andrew_zep





PostPosted: Visual C++ Language, Win32 Thread Interleaving Top

Hi,

I read some post for win32 thread application and thought if you guys can hep us.

we are developing trading platform for different exchanges and facing a strange problem. Our application creates two threads both threads are running for indefinate times (means there is no sleep in thread loop function). Thread 1 will always check the queue for possible work to be done and Thread 2 always listens the network broadcast.

The code is something similar to

void *ThreadAFunct(void *arg){
While(true){

int workAvailable = CheckQueue();
//....
if(workAvailable){
LockMutex(sharedMutex);
//do some work
UnLockMutex(sharedMutex);
}
//.....
}
}

void *ThreadBFunct(void *arg){
while(true){
//.....
LockMutex(sharedMutex);
//ListenForBroadcast...
UnLockMutex(sharedMutex);
//........................
}
}

Both threads uses same mutex. and for acquiring and releasing mutex we are using EnterCriticalSection and LeaveCriticalSection functions.

Now problem we are facing is, in windows XP hyper threaded processor, threads get the mutexes in just a few milliseconds and never had any problem with thread interleaving, threads releases after few milliseconds and other thread acquires mutex in very little time. But we are seeing different behaviour in windows 2003 server (SP1) hyper threaded machine. Thread doesn't interleave perfectly like XP, sometimes a thread runs for long time compare to other thread (I used timers and found out that Thread B gets most of the processor time compare to thread A.)

And often (most consistantly and probably root of all these problems) thread A waits for very long time just for acquring mutex (sometimes more than a min some time just a second).


I am not getting why it works perfectly fine in windows XP. The same application is running for over 3 months (on XP) but on windows 2003 it has these problems. One possible solution would be to put Sleep() in Thread B after reading certain number of broadcast. And it works but still can't get bottom of the problem.

I wrote same program using WaitForSingleObject() and it works fine with that, so wondering it might be something to do with the CriticalSection functions.

I even tried setting different values for Win32PrioritySepration but didn't worked.

So any suggestion would be helpful.

Thanks in advance



Visual C++15  
 
 
Mike Danes





PostPosted: Visual C++ Language, Win32 Thread Interleaving Top

It seems to me that your code is a victim of a change made in Win2k3 SP1 to avoid lock convoys (see http://www.bluebytesoftware.com/blog/CommentView,guid,e40c2675-43a3-410f-8f85-616ef7b031aa.aspx for one of many discussions about this).

Since this is not related in anyway to the C++ language I suggest you to use the MSDN Newsgroups since you have a better change to get a complete answer:

http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.win32.programmer.kernel&lang=en&cr=US