Re: Win32 Thread Interleaving
- From: PLS <nobody@xxxxxxxxxxx>
- Date: Mon, 22 Jan 2007 19:12:09 -0700
It's a design problem. You should not have both threads running all the
time. Not only does that make scheduling difficult for your program, it
disrupts all other programs running on the machine.
ThreadA should wait on a semaphore until there is work for it to do.
This is key, this thread should be waiting unless it's busy processing a
unit of work.
ThreadB should wait while listening for the broadcast, then queue the
work and signal the semaphore. Again, the thread should not be running
all the time.
++PLS
In article <B9FA9B77-F9E6-4F03-B4FC-0FA435BC1DD6@xxxxxxxxxxxxx>,
Andrew_zep@xxxxxxxxxxxxxxxxxxxxxxxxx says...
.
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.)
- Follow-Ups:
- Re: Win32 Thread Interleaving
- From: Pavel Lebedinsky [MSFT]
- Re: Win32 Thread Interleaving
- References:
- Win32 Thread Interleaving
- From: Andrew_zep
- Win32 Thread Interleaving
- Prev by Date: Re: Calculating Virtual Memory Fragmentation
- Next by Date: What API is _chdrive()?
- Previous by thread: Re: Win32 Thread Interleaving
- Next by thread: Re: Win32 Thread Interleaving
- Index(es):
Relevant Pages
|