0

I understand task as task in C#, but this questions is not related to C#.

Let's say I have single thread and two tasks (both CPU-bound). I would like to run them concurrently, without running one of them at the expense of starving completely the other one. How this could be implemented without assuming the code will somehow cooperate?

Is it possible to set some kind of timeout (or instruction countdown) on CPU/OS level to freeze task with its entire stack, and run the other task in the same manner, then resume the first one and so on?

This question does not go to level of multitasking with threads. It is about multitasking within single, one, thread. Task is not the same as thread.

14
  • 1
  • @Blrfl, I don't see relevance here. Think about one core, one CPU, one thread, only multiple tasks. So I am above the level of thread (and the other question asks about them). Or from other perspective -- how one could implement task scheduler in C# with task switching but without awaits (i.e. point indicators of switching). Commented Dec 24, 2017 at 15:24
  • How do you think what you're asking wouldn't be a reinvention of the same wheels described in the answer to that question?
    – Blrfl
    Commented Dec 24, 2017 at 17:42
  • 1
    I realize you've already selected an answer, but I'm curious as to why a single thread is a requirement here? ... Are you hoping to manage job unit scheduling or "task" throttling yourself in some way? ... Are you somehow trying to avoid the concurrency risks that come with threading? ... Are you working with a specific language or technology stack? Or, are you looking for a general pattern that is technology independent?
    – svidgen
    Commented Dec 24, 2017 at 20:26
  • @svidgen, pretty basic, if multithread was not excluded, I would get answer "just put one task per a thread, solved". I am asking in context of writing task scheduler by myself, not language specific, just general pattern. Commented Dec 25, 2017 at 7:08

2 Answers 2

1

No, that is not possible.

On the CPU/OS level, multitasking is executed on the level of threads. The OS gives time to various threads, but doesn't know about the (more granular) concept of tasks.

4
  • Hmm, this is a pity (I was hoping for something as elegant as multiplexing with I/O-bound tasks). So the only chance to switch tasks is when the task itself (the code) define some point of switch, correct? Commented Dec 24, 2017 at 15:10
  • 1
    @greenoldman either that or you split the tasks over multiple threads. Commented Dec 24, 2017 at 15:34
  • @CharlesE.Grant: The question was about non-cooperative multitasking. That is simply not possible on systems that don't have a pre-emptive scheduler, like early Windows. Commented Dec 24, 2017 at 17:12
  • @BartvanIngenSchenau, my apologies, poor reading comprehension on my part. Commented Dec 24, 2017 at 17:45
3

The feature you are looking for is "fibers," sometimes called "cooperative threads." A fiber has its own stack, but you switch them in-and-out of threads cooperatively. Any one fiber can call a function, typically called yield, and pass the fiber that they wish the computer to execute on this thread. Only one fiber executes at one time on a given thread, and nothing can stop it from executing involuntarily -- it needs to call a yield function to cooperatively give away control.

I'm not certain if C# has fiber support. Fibers were never very popular, and it seems like a feature which will raise a lot of challenges for a managed language. But that's the feature you're looking for.

How to do it without cooperation between the algorithms? That answer is easy: the sole entire purpose of threads is to do exactly the thing you want to do, because you need OS help to do it. Threads take advantage of a timer that fires at the OS level every few milliseconds, and their knowledge of how their stack is constructed to do the exact thing you want to do.

The reason OS libraries support threads is because you cannot do this on your own without either the cooperation of the OS (threads) or the cooperation of your other algorithms (fibers).

3
  • How are fibers going to help if the code never calls yield? The question is about multitasking with code that wasn't designed to cooperate with the scheduler. Commented Dec 24, 2017 at 17:15
  • @BartvanIngenSchenau Ahh, I misread and saw "with" when the word was "without." You are correct.
    – Cort Ammon
    Commented Dec 24, 2017 at 17:20
  • Thank you that you expanded your answer instead of deleting it, it is very informative, +1 from me :-). Commented Dec 24, 2017 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.