Thoughts on Threads
Posted: Fri Apr 26, 2024 12:06 pm
Consider the case where we want to run a lot of very short-lived tasks, arriving via a websocket, in order. However, we don't want to them in the 0 thread, which must remain unblocked and available for other things.
There at least two solutions for this.
First, we can spawn a new thread for every task and then use ⎕TSYNC to ensure that each task waits for the previous task to finish before starting.
Second, we can create a single new permanent thread with a loop around ⎕TGET to process tasks that are chucked in from the main thread using ⎕TPUT.
I would have thought the latter would be a more efficient solution, especially as the volume of short-lived tasks grows. But that appears not to be the case, which leads me the believe that threads are much lighter resources than I thought.
There at least two solutions for this.
First, we can spawn a new thread for every task and then use ⎕TSYNC to ensure that each task waits for the previous task to finish before starting.
Second, we can create a single new permanent thread with a loop around ⎕TGET to process tasks that are chucked in from the main thread using ⎕TPUT.
I would have thought the latter would be a more efficient solution, especially as the volume of short-lived tasks grows. But that appears not to be the case, which leads me the believe that threads are much lighter resources than I thought.