0

I have several always running threads which are executing "tasks" and one thread which gets new "tasks" and distributes between these threads.

Thread which executes task:

public class TaskExecutor
{
    public object CurrentTask { get; set; }
    public Thread ThreadRef { get; set; }

    private void Run()
    {
        while (true)
        {
            try
            {
                if (CurrentTask != null)
                    ExecuteTask(CurrentTask);
                else
                    Thread.Sleep(Timeout.Infinite);
            }
            catch (ThreadInterruptedException)
            {

            }
            catch (Exception)
            {
                CurrentTask = null;
            }
        }
    }

    private void ExecuteTask(object task)
    {

    }
}

Thread which distributes tasks:

private void TryDistribute(TaskExecutor thread, object task)
{
    if (thread.CurrentTask == null)
    {
        thread.CurrentTask = task;
        thread.ThreadRef.Interrupt();
    }
}

How to implement the same logic (indefinite wait and waking up for processing by other thread) using TPL Task?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Dmitry
  • 59
  • 5
  • 1
    see https://stackoverflow.com/questions/13406435/is-thread-sleeptimeout-infinite-more-efficient-than-whiletrue - the question is different, but the accepted answer is applicable. – hatchet - done with SOverflow May 26 '17 at 18:14
  • 2
    I do not understand why you want to call `Interrupt` in `TryDistribute` (or at all). Can't you express the behaviour you want in terms of wait handles wrappers, such as `ManualResetEvent`?. What you are doing reminds me of my old [Work](https://github.com/theraot/Theraot/blob/b362cd58af1db7e0fc993ee388361953b1cc06ac/Core/Theraot/Threading/Work.cs) and [WorkContext](https://github.com/theraot/Theraot/blob/b362cd58af1db7e0fc993ee388361953b1cc06ac/Core/Theraot/Threading/WorkContext.cs). If you need Tasks in old .NET you can use my libraries ([nuget](https://www.nuget.org/packages/Theraot.Core/)). – Theraot May 26 '17 at 18:19
  • 3
    That's an *extremely* problematic way of implementing that behavior *without* the TPL. You should be using a proper synchronization mechanism *regardless* of whether you use the TPL or not. – Servy May 26 '17 at 18:23
  • @Servy, this is what I want - move this logic to TPL. But as other commenters pointed out, it's better to use ManualResetEvent instead of indefinite sleeping – Dmitry May 26 '17 at 18:34
  • An indefinitely waiting item that will handle tasks passed to it could be as simple as an `ActionBlock` that `TryDistribute` posts data/tasks to. Have look [here](https://blog.stephencleary.com/2012/09/introduction-to-dataflow-part-1.html) – JSteward May 26 '17 at 19:22
  • 1
    `I have several always running threads which are executing "tasks" and one thread which gets new "tasks" and distributes between these threads.` ... like ... a thread pool? Why not just use the thread pool? – Stephen Cleary May 27 '17 at 16:05

0 Answers0