Jump to content
  • Advertisement
Sign in to follow this  
Narf the Mouse

Doing some multi-threading - Tasks

This topic is 2522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!