views:

32

answers:

3

I'm writing some program which should perform calculations concurrently according to inputs which reach the system all the time. I'm considering 2 approaches to allocate "calculation" processes:

  1. Allocating processes in the system initialization, insert the ids to Processes table, and each time I want to perform calculation, I will check in the table which process is free. The questions: can I be sure that those processes are only for my use and that the operating system doesn't use them?
  2. Not allocating processes in advance. Each time when calculation should be done ask the operating system for free process.

I need to know the following inputs from a "calculation" process:

  1. When calculation is finished and also if it succeeded or failed
  2. If a processes has failed I need to assign the calculation to another process

Thanks in advance. Any help would be appreciated.

+4  A: 

I think you want threads, not processes. .NET has a ThreadPool class which does pretty much what you need, I think.

Dean Harding
Agreed. Processes seem unnecessary... UNLESS the calculations are vastly different each time, and a complete .NET reset is really wanted. Even then, same process / ApplicationPools should be considered first.
TomTom
And .NET 4 (currently RC, due for RTM in April) adds PFX with a far more powerful model for packaging up and managing multiple concurrent operations that return results.
Richard
A: 

I'm not sure what you mean by 'read the system' so I'll just make some assumptions that you can correct:

  • Processing inputs are pulled from a database or file system
  • You need to track who is processing which item
  • Failure/Success of a calculation is determined simply by the fact that the process finished, rather than some condition that can be checked.

It sounds like there are two things here. First, you want an effective queueing mechanism for all jobs, and jobs that fail need to be re-enqueued. Second, you probably actually just want multiple threads, rather than processes.

ThreadPool is a good place to start. You can queue a new thread with a new work item, and if that thread fails, re-enqueue the work item. The queue can be as simple as teh Queue of T in .net. But check the thread safety - you may need to implement your own locking on the queue to ensure no two threads dequeue an item at the same time.

Without more information about the tasks and a terminology clear up, I can't help you much more.

DarkwingDuck
A: 

Thanks you very much for the detailed replies.

I think I'll go the the ThreadPool solution. Also in .Net 4.0 there is TPL which should simplify the dealing with ThreadPool.

Thanks again.

mayap