Announcement

Collapse
No announcement yet.

Thread Question - Understanding Use of Sleep

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

  • David Roberts
    replied
    'Dialog DoEevnts 0' in MainThread doesn't help if MainThread is executed before Thread Create - NewThread doesn't get a look in until MainThread completes in this case either.

    Leave a comment:


  • David Roberts
    replied
    > ooh, ooh, one of my favorite topics.

    I knew that and was hoping you'd come in.

    However, the fact that the secondary thread does not start, even though it exists according to Task Manager, until the function MainThread has completed still remains unanswered.

    Leave a comment:


  • Michael Mattias
    replied
    >I'm playing with threads today,

    ooh, ooh, one of my favorite topics.

    >Control Set Text hDlg, 200, "Main" + Str$(iCount&)

    "Assuming" CONTROL SET TEXT uses SetWindowText, a thread switch is forced when executed, becase SetWindowText (window owned by calling process) uses WM_SETTEXT message, and the window procedure always executes in the context of the window-owning thread.

    So you are constantly switching threads.

    In addition, the thread context in which the active window executes gets preferential treatment when it comes to getting CPU time.

    The bottom line is, "empty loops" are no way to see what's going on in a multi-threaded program.


    If you are looking at doing a "GUI with a background task in a separate thread of execution" you will want to look at and study this demo:
    GUI + Worker Thread + Abort Demo 11-24-07

    I have a bunch of other multi-threaded demos in the source code forum. However, most do not have "thread" in the title since I used additional threads because it seemed like the right thing to do, not because "THREAD CREATE" was particularly fascinating.

    MCM

    Leave a comment:


  • David Roberts
    replied
    > the main thread executes almost immediately, then the second thread starts and runs really slowly (by comparison to the main thread)

    In fact, the second thread does not start until the main thread has completed. Try 20000 instead of 2000.

    After the main thread has completed left click and hold the title bar and watch the second thread run flat out. Release the button and the second thread slows down again.

    If we put 'Dialog DoEevents 0' in MainThreads loop then MainThread still starts before NewThread but both functions now run flat out.

    I did think I knew the reason for this behaviour but I have since changed my mind and am now scratching my head again.
    Last edited by David Roberts; 9 Oct 2009, 04:22 PM.

    Leave a comment:


  • Gary Beene
    started a topic Thread Question - Understanding Use of Sleep

    Thread Question - Understanding Use of Sleep

    I'm playing with threads today, so I'm sure this is only the first of several questions I will ask.

    I tried the simple code below, expecting both threads to run simultaneously. On my PC, the main thread executes almost immediately, then the second thread starts and runs really slowly (by comparison to the main thread). Regardless of the values I use in sleep (0, 1, 5, 50) , I get the same results - thread NewThread executes sequentially after MainThread.

    From this Help comment on SLEEP, I expected NewThread to run in parallel with the main thread.

    Pause the current thread of the application for a specified number of milliseconds (mSec), allowing other processes (or threads) to continue.
    Can someone clarify for me what I'm misunderstanding?

    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 0
         Loop Until iCount& > 2000
    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 0
         Loop Until iCount& > 2000
    End Function
Working...
X