Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should threads update global data in the main program?

I am revisiting an old thread of mine.

I want to launch a bunch of threads, each performing the same task, and know in main() when each finishs and it if it was successful or failed.

The solution offered was to use a ConcurrentQueue, but other posts have recommended using a BackgroundWorker Class, or a thread pool.

Is there a definitive answer?

Again, all threads perform the same code and have a pass/fail result. I want to run more than there are available threads so as soon as one thread finishes I will launch another asap - I want tehm to stress a remote systems as much as possible (reather than stressing my local PC with too many threads, so I will need to experiment to determine the optimal number of threads).

VB .NET for specific answer, but general threading advice is also welcome.

like image 709
Mawg says reinstate Monica Avatar asked Feb 24 '23 18:02

Mawg says reinstate Monica


2 Answers

BackgroundWorker is a very easy way to manage your running threads. It allows you to report progress back to the UI easily. I don't think there is a definitive answer but BackgroundWorker is designed for this purpose - running background tasks which update the UI as they progress. Here is an example of how to is it.

like image 105
Josh M. Avatar answered Mar 03 '23 17:03

Josh M.


Josh's is the way I'd go.

Create as many new backgroundworkers as you need, hook up to intercept their "finished" event (I forget the exact name), and when those events fire back on the main app thread, store the results in a collection/list/array of somesort.

Things can get interesting if you really need to pool threads so you don't just create a ton of threads which can cause a lot of context thrashing. Instead, you want to have a pool of, say 2x(the number of processors in the machine), and as each backgroundworker finishes, queue up another task in it's place.

like image 33
DarinH Avatar answered Mar 03 '23 16:03

DarinH