Threads run sequentially?

This is a discussion on Threads run sequentially? within the Windows Programming forums, part of the Platform Specific Boards category; When running a program with multiple threads of execution, does windows grant them CPU time sequentially? So for example will ...

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801

    Threads run sequentially?

    When running a program with multiple threads of execution, does windows grant them CPU time sequentially? So for example will the order of thread execution be:

    Thread A
    Thread B
    Thread C
    Thread A
    Thread B
    Thread C
    Thread A
    .......

    Or is it random?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I believe Windows uses a variation of Shortest Job First with emphasis on priority scheduling algorithm.

    Kuphryn

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The best discription I have seen in books is a "round-robin" approach at its most basic (assuming all threads are equal).....so yeah in theory your model is close. But factors like thread priority distort this quite a bit (higher the priority, the greater the quantums they have)......and also add to the that hardware interupts are able to "jump the queue" above any priority level that you are able to set......

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    It's pretty unreliable sometimes. Often you'll see even threads with the same priority execute out of whack, and almost always you cannot count on threads launched side by side to start in perfect order. I really don't know why this is the case tho, your guess is as good as mine. If it's *really* important though, you should use global flags, locks, mutex's, etc...



    ITSA
    Socket Library!

  5. #5
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Ok thanks guys.

    Another question, I wasn't aware until now that it is possible to assign a priority to a thread. Is this done by using (from msdn):

    Code:
    BOOL SetPriorityClass(
      HANDLE hProcess,
      DWORD dwPriorityClass
    );
    where:

    hProcess would be the name of the thread
    dwPriorityClass would be for example HIGH_PRIORITY_CLASS (msdn)

    Thanks again

  6. #6
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    On second thoughts, I just found this:

    Code:
    BOOL SetThreadPriority(
      HANDLE hThread,
      int nPriority
    );
    Which I assume is what I am actually after right?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Right.

    And don't forget about GetThreadPriority(), SuspendThread() and ResumeThread().

    Another useful function you'll need is GetExitCodeThread, especially for console programs. You can use it to hold the console window open while the thread is in action:

    Code:
    bool ThreadIsActive(HANDLE thread)
    {
     DWORD status = 0;
     GetExitCodeThread(thread, &status);
         if(status == STILL_ACTIVE)
       {
         return true;
       }
     CloseHandle(thread);
     return false;
    }
    
    int main() 
    {
     //...code
    
     while(ThreadIsActive(thread))
       continue; // a busy loop
    }



    ITSA
    Socket Library!

  8. #8
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Doh!! I have been doing pretty much the same thing but manually. Having a global flag that the thread sets to 1 upon execution and 0 upon termination. Your method is much tidier I feel.

    Thanks guys

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Once, I studied this, check this document:
    http://www.strandmark.com/arbeten/ai.pdf page 17

    It's in Swedish, but the two graphs show how many instructions each of two threads (with equal priority and code) execute over time.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bumping old threads.
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-21-2009, 07:53 PM
  2. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  3. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  4. Closing threads
    By VirtualAce in forum Windows Programming
    Replies: 8
    Last Post: 01-02-2006, 09:49 AM
  5. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21