Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last thread of a multithreaded application

I have a c# console application which has some threads to do some work (download a file). each thread may exit the application at any time any where in application, but I'll show a proper message on console. It's possible to track them but it doesn't make sense to me. I want simply check thread count or something like that to find out which one is the last thread and do something when it is exiting. What's the best practice to do so ?

pseudo code:

if (lastThread)
{
   cleanUp();
   Console.ReadLine();
}

Thanks

like image 420
Xaqron Avatar asked Oct 04 '10 23:10

Xaqron


People also ask

What is a thread Give an example of a multithreaded application?

For example, a desktop application providing functionality like editing, printing, etc. is a multithreaded application. In this application, as printing is a background process, we can perform editing documents and printing documents concurrently by assigning these functions to two different threads.

What is multithreaded application?

Multithreaded applications have multiple threads executing in a shared address space. Threads are "lightweight" subprocesses that execute within a process. They share code and data segments, but have their own program counters, machine registers and stack.

What is multithreaded execution?

Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core.

How can you tell if a program is multithreaded?

In taskmanager, right-click the game process and set the affinity to one core. Play a little ingame and check your fps. Then change affinity to two cores, if your fps increases then the game is (properly) multithreaded.


3 Answers

This is one place where using the new Task Parallel Library can make life much easier. Instead of creating threads, and spinning work up on the thread, you can use multiple tasks:

var task1 = Task.Factory.StartNew( () => DoTaskOneWork() );
var task2 = Task.Factory.StartNew( () => DoTaskTwoWork() );
var task3 = Task.Factory.StartNew( () => DoTaskThreeWork() );

// Block until all tasks are done

Task.WaitAll(new[] {task1, task2, task3} );
cleanUp(); // Do your cleanup

If the "tasks" are just downloading a bunch of individual files, you could even make this simpler using PLINQ:

var fileUrls = GetListOfUrlsToDownload();

fileUrls.AsParallel().ForAll( fileUrl => DownloadAndProcessFile(fileUrl) );

cleanUp(); // Do your cleanup
like image 138
Reed Copsey Avatar answered Oct 05 '22 04:10

Reed Copsey


A design where you lose track of your threads is not ideal.

Depending on how you spawn them it ought to be possible to track the status of each by associating some per-thread signalable object, then WaitAll on those signalable objects.

Each signalable object in turn should get signaled as its thread exits. When they are all signaled, you know the threads are all dead and you close down clean. You have to make sure that abnormal conditions in your threads do not result in that thread's associated signalable object remaining unset, or your WaitAll will never return. This means exceptions typically - could use try...finally to ensure the objects get signaled.

Your new pseudocode is

foreach (workitem in list of work)
  start up thread associated with a ManualResetEvent or similar

WaitAll for all events to be signalled
cleanup
like image 38
Steve Townsend Avatar answered Oct 05 '22 04:10

Steve Townsend


Your main thread should join with all your worker threads and block while they are running. Then when all threads are complete it performs the cleanup code and then quits.

Alternatively you can use a WaitHandle such as as a ManualResetEvent per thread and wait for all of them to be signalled.

like image 23
Mark Byers Avatar answered Oct 05 '22 05:10

Mark Byers