-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can Weave spawn a thread that sends a Channel message to the main channel without blocking the main thread? #198
Comments
Weave provides That said Weave internals use "work-requesting" instead of "work-stealing" so load balancing require cooperation between threads. If you offload something that blocks, for example that does IO like writing to a file or stdout, you may block the whole runtime. Also I've added an experimental API called I do not think it's the best way forward, ideally we have a threadpool built for IO, but it at least exist. |
If I understand approaches using isReady correctly, that would require me to manage any task I create myself, throughout the multiple iterations of the while-loop that may occur on Thread A between a task being spawned on Thread A and it finishing on Thread B. That's doable, but sounds like considerable overhead. By having a task simply send messages back to the main-thread via a channel I circumvent a significant chunk of that. |
Can't you do the following: proc foo(ctx: ptr Context, arg0: T, arg1: U, arg2: V): bool =
# ... heavy processing
ctx.signalReady() # The context has the taskID Otherwise your problem is quite similar to the design constraint I had for implementing dataflow parallelism with events or many events that trigger dependent spawns: weave/weave/parallel_tasks.nim Lines 367 to 397 in b6255af
You can read the implementation here: https://github.com/mratsim/weave/blob/master/weave/cross_thread_com/flow_events.nim it's multithreading runtime agnostic and only need threadsafe MPSC queues. |
Heyho, I am trying to get an example to work where I spawn a thread to perform a task in weave that eventually sends a Channel message, while the proc itself returns nothing.
I want to go for a Channel message as I want a setup with a thread running an event-loop of a GUI that regularly reads messages send to it from possibly multiple other "Task"-threads.
It should not ever block waiting for any of the "Task"-threads to finish as it should run its own event-loop and stay responsive to user-input.
The code would look roughly like this (if spawn only spawned a single thread for that one proc call):
Now obviously the above does not work.
I'm not entirely sure what it does, but I'm very sure it spawns more than 1 thread as it maxes out my 16 core CPU on top of never even starting to execute
doThing
.Is there a way to express that with weave?
I initially assumed that this fell under "task parallelism" since I would want to do 2 tasks in parallel (run gui thread, execute "doThing"), but I'm really not that knowledgeable with the terminology thrown around.
The text was updated successfully, but these errors were encountered: