[TUHS] Threads vs... not
bakul at iitbombay.org
Fri Aug 6 09:02:06 AEST 2021
On Aug 4, 2021, at 2:48 PM, Lawrence Stewart <stewart at serissa.com> wrote:
> What do folks think about event-driven programming as a substitute for threads in UI and process control settings?
The equivalence here is with coroutines (sometimes called
green-threads), not true threads. True threads can take
advantage of multiple cores. Your event-driven program won't
benefit from multiple cores.
> I wrote the service processor code for the Sicortex Machines using libevent.a and I thought it was very lightweight and fairly easy to think about. (This was a thing running on ucLinux on a tiny 16 MB coldfire that managed the consoles and power supplies and temp sensors and JTAG access and booting and so forth.)
I wrote the RealNetworks media server code using an
event-driven model. It was efficient but the control flow got
quite messy as effectively my code had to do explicit
continuation passing. Given that each media stream was
independent of other streams, a thread based model was far
simpler. That is what a colleague did to test the server
As a thought experiment consider implementing a Unix style
pipeline but without an OS that provides the illusion of
independent processes or threads and where each stage in the
pipeline is a separate function. Try implementing this in Go
which has support for threads vs C (but without pthreads).
I have used coroutines for hardware simulation a number of
times, the first time was in 1982 for simulating bus
transactions to see the impact of adding an ethernet
controller (5.6Mhz clock, memory access 4 clocks). The last
time was to simulate an ethernet switch with complex i/o
logic. One can think of each hardware module as a separate
state machine but smushing them into one giant state machine
would not been easy as the code was being developed and
debugging it would be very painful. One would then be tempted
to write a "preprocessor" language of some sort to hide all
> Tk (IIRC) has a straightforward event driven model for UI interactions.
> Meanwhile, the dropbox plugin for my Mac has 120 threads running. WTF?
Any system can be abused but the abuse-case should not be
mistaken for the normal use-case.
More information about the TUHS