Problem with thread priorities


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Problem with thread priorities

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    4

    Problem with thread priorities

    In my C++ console application running under Windows XP, a thread with THREAD_PRIORITY_HIGHEST (Thread B) is preempting a thread with THREAD_PRIORITY_TIME_CRITICAL (Thread A).

    Thread B is blocking on a WaitForSingleObject() call. Thread A will wake Thread B with a SetEvent() call. But since Thread A has higher priority, I would expect Thread A to complete running before Thread B will run. But thatís not the case. As soon as Thread A issues the SetEvent() call, Thread B starts running even though it has a lower priority than Thread A.

    I disabled priority boost, thinking that might be the culprit, but I still see this happening. Does anyone know what else could be causing this?

    Thanks for your help.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    Do you possibly have a dual core processor? Or a sleep or wait state in the higher thread?

  3. #3
    Join Date
    Oct 2007
    Posts
    4
    I don't have a sleep or wait state in the higher priority thread (at least not one that affects this particular portion of the code). But I do have a dual core processor and I think I see what you're getting at. You think when the lower priority thread is woken, it might be running in parallel with the higher priority thread? Is there a way to stop this from happening (other than using more semaphores, etc.)?

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    A dual core will run one thread on each processor; if that is undesirable you will need a mutex or other thread safety locks etc to prevent it.

    This is what you *want* to happen on a parallel algorithm ... I have messed with it and found that dual core machines really do run twice as fast when both threads are in time critical mode (which keeps the OS honest). On an algorithm where the two tasks are dependant, its a problem and you will have to delay them / use semaphores etc.
    Last edited by jonnin; 10-04-2007 at 06:13 PM.

  5. #5
    Join Date
    Oct 2007
    Posts
    4
    Thanks jonin, I think running on a dual core machine was my problem. I used "xcpu" (www.appliedvisual.com/xcpu.htm) to restrict my application to running on only one core and things work now. Is there any way to programmatically tell my application to use only one core when it first executes?

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    I dont know, but probably there is. You could make the app single threaded, why are you multi threading if you do not want the performance boost?

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    It really sounds like you need a single threaded design. Why bother so much with threads when you have to run each thread separately?
    Danny Kalev

  8. #8
    Join Date
    Oct 2007
    Posts
    4
    Well, the context is that I'm taking code written for an embedded device and writing a simulation of the device in Windows using the actual code. The device uses a commercial RTOS, has a single core CPU and its software is multi-threaded. My computer is running Windows on a dual core CPU. But I found the function SetProcessAffinityMask() that lets me designate a single core to execute on and that has been working well for me.

    Thanks for the help, jonnin.

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    I suspect that testing MT code written for a single core machine on a dual core machine isn't valid. You should force a single core execution to test the original code.
    Danny Kalev

Similar Threads

  1. Managing swing thread priorities
    By AndreRSISE in forum Java
    Replies: 0
    Last Post: 02-26-2007, 09:31 PM
  2. Replies: 1
    Last Post: 10-07-2006, 10:21 AM
  3. JProgressBar and Thread problem
    By somnio85 in forum Java
    Replies: 2
    Last Post: 03-01-2006, 03:37 PM
  4. Josephus Problem, problem.
    By aesoprock00 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 07:18 PM
  5. Replies: 1
    Last Post: 10-29-2000, 05:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center