Re: Win32 Thread Interleaving



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.)

.



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... And in what way am I wrong about Windows mutexes? ... expectations would violate the *documented* mutex behavior). ... building only experimental systems (my first synchronization system was ...
    (microsoft.public.vc.mfc)
  • Re: Why does Microsoft never document threading right?
    ... which is a fast mutex (and has ... CRITICAL_SECTION structure is implemented internally on Windows. ... object does not allow only one thread at time to execute the same code. ... It protects data, not code. ...
    (comp.programming.threads)
  • Win32 Thread Interleaving
    ... Both threads uses same mutex. ... Now problem we are facing is, in windows XP hyper threaded processor, ... sometimes a thread runs for long time compare ...
    (microsoft.public.win32.programmer.kernel)
  • Re: [RFC PATCH 0/6] module, kbuild: Faster boot with custom kernel.
    ... many of them around 20 milliseconds. ... The whole massive parallel modprobe happens during udev coldplug. ... With the mutex it takes 1.8 seconds, without it, it takes 1.3 seconds. ... preparation, which is used in an error case, I get almost the same ...
    (Linux-Kernel)
  • RE: Open running processes
    ... It's clear that you're on Windows (because of the Mutex) but you ... to post, a fair amount would depend on the nature of your program, ... anti-virus service working around the clock, around the globe, visit: ...
    (comp.lang.python)