Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for a background thread to complete

Sorry if this is a duplicate, but I'm not quite sure what terms I need to use to find existing answers to this question.

I'm trying to improve start-up performance of an application, the pseudo-code looks a bit like this.

LoadBigFileFromDisk();  //slow
SetupNetwork();         //even slower
UseBigFileFromDisk();

I figured that as the first step is disk-bound, and the other network-bound (and slower), I could run the first in a background thread (currently playing with ThreadPool.QueueUserWorkItem, but not sure if that's the best way) and improve the performance a bit.

It works, but what worries me is that I'm relying on the second step being slow enough for the first to complete.

I know I could set a _done boolean somewhere and while ! on that, but is there a more elegant/idiomatic solution?

(Not .Net 4.0 yet, so though I'm interested in Task-based, I need the fall-back solutions).

like image 405
Benjol Avatar asked Oct 24 '11 11:10

Benjol


2 Answers

In the "main class" do this:

ManualResetEvent mre = new ManualResetEvent(false);

in your "main" method do this:

// Launch the tasks

mre.WaitOne();

in the task when it finishes (just before the return :-) )

mre.Set();

If you have to wait for multiple events, in your "main" create multiple ManualResetEvent and put them in an array, each event "connected" to one of the tasks, and then each task Sets its event when it finishes. Then in your "main" you do:

WaitHandle.WaitAll(arrayOfManualResetEvents);

Note that in this way you can wait up to 64 events. If you need more, there is another method (and note that you have to use this last method even if you are on a STA thread, like the main thread of a WinForm app).

ManualResetEvent mre = new ManualResetEvent(false);
int remaining = xxx; // Number of "tasks" that must be executed.

// Launch tasks

mre.WaitOne();

At the end of each task

if (Interlocked.Decrement(ref remaining) == 0)
{
    mre.Set();
}

Only the last task will decrement the remaining field to 0 and mre.Set().

like image 198
xanatos Avatar answered Oct 12 '22 22:10

xanatos


You could try:

var loadTask=Task.Factory.StartNew(()=>LoadBigFileFromDisk());
var setupTask=Task.Factory.StartNew(()=>SetupNetwork());

Task.WaitAll(loadTask,setupTask);

UseBigFileFromDisk();

This uses the Task Parallel Library.

or:

var loadThread=new Thread(()=>LoadBigFileFromDisk());
var setupThread=new Thread(()=>SetupNetwork());

loadThread.Start();
setupThread.Start();

loadThread.Join();
setupThread.Join();

UseBigFileFromDisk();

When you're not using .NET 4. If these tasks take a long time to run then it's best to avoid the thread pool, as it's primarily for short lived tasks.

like image 44
Sean Avatar answered Oct 13 '22 00:10

Sean