Doing some multi-threading - Tasks

Started by
17 comments, last by Narf the Mouse 12 years, 3 months ago
Language is C#

After some testing, I've come to a probable format for my multi-threading - A MultiThread class that feeds tasks to TaskThreads. Provided I've gone through the logic correctly -

- The MultiThread class will only feed tasks into empty (null) Task slots.
- The TaskThread will only null a Task slot once it's gotten the task from it and is going to execute it.
- The TaskThread will use a CompareAndExchange to set the Task slot to null, so a new task will never be over-ridden (a new task should never be written before it's set to null in the first place, but extra security on that is extra security).
- Thus, no collision.
- The TaskThread will store three Task slots.
- The TaskThread will store a TaskIndex variable referencing the current task.

Now, I need a way to have the Tasks be executed in order. The TaskThread is considered a part of the MultiThread for design purposes.

Thanks.
Advertisement
This seems overly complex and limiting. Just put your tasks into a ConcurrentQueue (assumes .net 4) and let the workers pull from it as they become ready.

As for execution in order... that defeats the purpose of having a bunch of worker threads.

[edit: plus I'm pretty sure there's a general purpose threadpool class in .net somewhere... haven't used it so can't talk to it]

[edit: plus I'm pretty sure there's a general purpose threadpool class in .net somewhere... haven't used it so can't talk to it]


The TPL.

This seems overly complex and limiting. Just put your tasks into a ConcurrentQueue (assumes .net 4) and let the workers pull from it as they become ready.

As for execution in order... that defeats the purpose of having a bunch of worker threads.

[edit: plus I'm pretty sure there's a general purpose threadpool class in .net somewhere... haven't used it so can't talk to it]

I tested both of those ideas; they are both slower.

I tested both of those ideas; they are both slower.


Consider me skeptical. The overhead of a concurrent queue should be negligible unless the tasks you're testing are almost nothing.

[quote name='Narf the Mouse' timestamp='1326141530' post='4901060']
I tested both of those ideas; they are both slower.


Consider me skeptical. The overhead of a concurrent queue should be negligible unless the tasks you're testing are almost nothing.
[/quote]
The tasks contained just a return command, so as to test the speed of the framework. The time shown was more than negligible.

A ConcurrentQueue, after all, has to account for multiple readers and writers. MultiThread and TaskThread, however, only have to account for one reader and one writer, and always the same writer and reader.
Here's some questions:

How do you create Task?

What if there are more than three Tasks?

Are MultiThread and TaskThread running on different threads?

If you are only executing one task at a time, then it's not multi-threading, only a task poll.

Here's some questions:

How do you create Task?

A task is just an Action<object> delegate. Since the functions I want to pass in are void(object) functions, this works nicely.


What if there are more than three Tasks?

If all TaskThreads are full, the MultiThread starts executing tasks until a TaskThread has room for another Task.

Are MultiThread and TaskThread running on different threads?

MultiThread checks Environment.Processors and makes that - 1 many TaskThreads.

I suppose what I really need is a way to sort the tasks based on which tasks need other tasks done first.
It sounds like you have a task dependency tree. If you know the detail in advance, could you not just submit the first task in each dependency tree, and have it submit additional tasks once it has completed, and so on.

What do you know about the task interdependencies?

It sounds like you have a task dependency tree. If you know the detail in advance, could you not just submit the first task in each dependency tree, and have it submit additional tasks once it has completed, and so on.

What do you know about the task interdependencies?

Pretty much, the definition. One task can't properly (for whatever definition of 'properly') do its work until another task has done its work.

This topic is closed to new replies.

Advertisement