Announcement

Collapse
No announcement yet.

Thread Question - Sleep 0 vs Sleep 1

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Thread Question - Sleep 0 vs Sleep 1

    Same code as my last message - different question, just trying to understand how Sleep works.

    If I use Sleep 0, I expect the thread to give up the rest of the current time slice. I don't know what the time-slice for my PC, but if it is 20ms, then Sleep 0 would give up 0-20 ms, depending on how much of the current time slice the current thread had already used when Sleep was called.

    If I use Sleep 1, then I expect it to give up only 1 ms of the current time slice.

    If these two statements are true, then I would expect Sleep 0 would cause a thread to run more slowly than Sleep 1 (unless it just happens that Sleep 0 hits on the 19 ms mark). But that's not what I see in the code.

    Can someone explain my mis-understanding of how Sleep is supposed to work?

    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    #Include "Win32API.inc"
    Global hDlg As Dword, hThread As Dword
    Function PBMain() As Long
       Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
       Control Add Button, hDlg, 100,"Start Thread", 50,10,100,20
       Control Add Label, hDlg, 200,"<main thread count>", 50,40,100,20
       Control Add Label, hDlg, 300,"<extra thread count>", 50,70,100,20
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
         Thread Create NewThread(0) To hThread       'start the new thread, argument not used
          Thread Close hThread To hThread    'suggested by PowerBASIC Inc. as good practice
          MainThread    'in the main thread, keep doing something
       End If
    End Function
    
    Function MainThread () As Long
         Local iCount&
         Do
            Incr iCount& : Control Set Text hDlg, 200, "Main" + Str$(iCount&)
            Sleep 1
         Loop Until iCount& > 500
    End Function
    
    Thread Function NewThread (ByVal x As Long) As Long
         Local iCount&
         Do
            Incr iCount& : Control Set Text hDlg, 300, "Thread" + Str$(iCount&)
            Sleep 1
         Loop Until iCount& > 500
    End Function

  • #2
    > If I use Sleep 1, then I expect it to give up only 1 ms of the current time slice.

    The current time slice is given up on Sleep no matter what its parameter is. When we return if Sleep is still active then the time slice is given up again and we belt out. With Sleep 0 when we return Sleep is no longer active and we continue where we left off.

    Comment


    • #3
      SLEEP is not something you "always use" just because a program is multi-threaded.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        SLEEP tells the operating system that you're not doing something vitally important right now, and it can take the time to catch up on its to-do list-- process a few messages or whatever. The amount of time you specify for a SLEEP is a hint, not a command, because the SLEEP statement already says "I don't need your attention right now, take five." How much time is actually used depends on what other things the operating system wants to do, and on how accurate the clock is. If you specify a unit of 1 tick, and the clock is only accurate to within 10 ticks, well...

        Comment

        Working...
        X