How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent exe

poguypoguy Member Posts: 91 ■■□□□□□□□□
How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent execution of threadOne until threadTwo completes execution?

I though waitcallback just push the function and it will run whenever there is a thread in the pool, isn't it?

but I saw a question said that waitcallback would prevent threadOne until threadTwo complete exec.
Thank you

Comments

  • DeepCodeDeepCode Member Posts: 29 ■□□□□□□□□□
    poguy wrote: »
    How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent execution of threadOne until threadTwo completes execution?

    I though waitcallback just push the function and it will run whenever there is a thread in the pool, isn't it?

    but I saw a question said that waitcallback would prevent threadOne until threadTwo complete exec.
    Thank you

    If thread-one has to wait for thread two to finish, then you might as well just execute the operations synchronously and eliminate the overhead of creating threads
  • davidinnzdavidinnz Member Posts: 5 ■□□□□□□□□□
    The ThreadPool.QueueUserWorkItem override that takes one parameter, a WaitCallback delegate, definitely will not block that thread.

    I wonder if the question that you saw, that seemed to suggest that the thread WOULD wait, was doing something a bit more complicated.

    70-536 does discuss passing an EventWaitHandle object to the callback delegate. After queuing threads, the main thread calls WaitHandle.WaitAll (which is passed an array of those EventWaitHandle objects). At that point it will block.

    When completing, the background thread calls that object's Set method. The main thread therefore resumes only after all those objects have had their state reset by the background threads.

    But the simplistic ThreadPool.QueueUserWorkItem simply passing a delegate will definitely not block.
  • davidinnzdavidinnz Member Posts: 5 ■□□□□□□□□□
    Just to clarify, given...

    publicstaticvoid TestThreadPoolA()
    {
    // Queue the task.
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));
    Console.WriteLine("Main thread resumes.");
    }
    publicstaticvoid TestThreadPoolB()
    {
    // Queue the task.
    AutoResetEvent e1 = newAutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc),e1);
    AutoResetEvent e2 = newAutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc), e2);
    WaitHandle.WaitAll(newWaitHandle[] { e1, e2});
    Console.WriteLine("Main thread resumes.");
    }
    // This thread procedure performs the task.
    staticvoid ThreadProc(Object stateInfo)
    {
    Console.WriteLine("Starting a thread.");
    Thread.Sleep(3000);
    AutoResetEvent e = (AutoResetEvent)stateInfo;
    Console.WriteLine("Finishing a thread");
    if (e != null)
    {
    e.Set();
    }
    }

    If I ran TestThreadPoolA, I woudl see something like...

    Main thread resumes
    Starting a thread
    Starting a thread
    Finishing a thread
    Finishing a thread

    If I ran TestThreadPoolB, I woudl see something like...

    Starting a thread
    Starting a thread
    Finishing a thread
    Finishing a thread
    Main thread resumes
  • purpleflightpurpleflight Registered Users Posts: 1 ■□□□□□□□□□
    If you ask this question because it's an answer of the 70-536 exam, i think the answer could be by setting thread pool SetMaxThreads to1. So queue firstly thred2 then thread1. Plusthe pool reuse of the thread should fasten the process.
Sign In or Register to comment.