Is this true multithreading with context switching where tasks can yield and resume later, or where the runtime can preempt running tasks? At first sight, your code has only one thread of execution, and all functions run to completion. In this case, I think “cooperative multitasking” would be a more suitable name than “multithreading”.
- No, it doesn't have context switching.
- It has one thread as an example, I can put another thread with its tasks.
- Yes, it's a "cooperative multitasking", but with a feature of grouping multiple tasks to a certain thread and monitor that thread in the manager function.
Void pointers are a cancer that spread through your code base,
Yes, this was the only way I could achieve this code using void pointers because it allows me to pass whatever arguments type I want with casting everything.
I'd highly recommend designing some sort of type erasure abstraction to manage different argument types and counts.
How to do that in C ?
==========================================================
I tried to find a better way in the first time, but I found that void pointers are the only solution in C, because I think that I can do almost but not entirely anything with C.
I really want to learn as much as I can in C before using C++, but I read C++ libraries from a time to time and I know some of the methods that C++ use.
I'm not a fast learner that's why I'm taking a lot of time that can be from months to years to cover some skills in C.
C++20 has language support for coroutines, there's no need to implement this in user code, which is much harder than using the conveniences provided by the compiler.
Where to find this support for coroutines ?
If you really want to reinvent the wheel yourself, have a look at Boost::asio for inspiration.
No, I'm not trying to invent the wheel, it's not my main goal. I just want a task_manager that is easy to use for me and has the features I want.
I downloaded many multitasking libraries and I didn't like them very much, I prefer to do my own version even if it's almost identical to another library, I copy functions sometimes and also I copy methods and ideas of the library I consider to follow its main scheme.
That's where you would use co_await
rather than looping. Blocking loops do not mix with coroutines, the whole point is that coroutines can be resumed later, without actually waiting.
Contrary to what you might infer from the name, await
suspends the coroutine and schedules it to be resumed later, it doesn't actually wait.
Let me try that on thread level. I'm only using:
CO_BEGIN, co_delay(prd) & CO_END
Maybe I should find away to multitask between multiple coroutine_manager.