I was contemplating the idea of using a single looping thread function to utilize multithreaded programming. However, it’s plain theory so I wonder what are your ideas since it’s probably wrong.
An example might be that a single function is called multiple times in scattered places around an application. While it would be hard to make parallel programming simply on it, one could have an extra thread that loops with a boolean that checks “do we need this function”, and if yes, then to execute it.
Is that even remote possible to have applications?
I suspect it will be slow due to some “lag” between the “call” for it via the boolean/var about it and the actual execution of the function in need.
EDIT: I had in mind a fast function, I suppose it might be doable if the function called is very slow (to complete).
Then again, we’d need to deal with thread safety and plain concurrency, so this is all probably wrong.
You could fire a thread to execute a function, but the caller would probably still want to wait for the result. Threading is by definition useful for processes that can be done in an isolated form. Not just any call is suitable to be threaded.
If you define such a process, it will probably be just fine to execute multiple threads at once. I wouldn’t see any use of having one separate thread that is just calling functions from a queue.
It could have some use in a reporting tool for example. To minimize database load, you could choose to execute only one or a small number of reports at a time. After a report is done, a thread could pick up a new report from a queue.
To implement something like this, I would build (and actually have built) a connection pool and a tread pool. If you’d like to build something like this too, I’d encourage you to search for mutexes (atoms/critical sections) and semaphores.