How common/necessary is multithreading, and is there a viable alternative? I was looking at the SQLite FAQ and came across http://www.sqlite.org/faq.html#q6 and http://www.sqlite.org/faq.html#q5 , which state “Threads are evil. Avoid them.” I’ve been meaning to learn about threading as a way to carry out processes in the background while still taking input that may end up terminating or affecting said background processes in some way. I guess I (incorrectly) figured that something akin to threading would be necessary in order for standard program functionality to be possible; after all, programs, or, more generally, the OS doesn’t stop taking input while doing work or stop doing work if there’s a lack of input.
I hate to bump my own post, but, my own bias aside, this seems like a reasonable question. After all, concurrent operations are utilized in almost all major and significant applications. Furthermore, threads have been suggested to me before on this board, and a search on C++ concurrency ends up leading to explicit threads or automatically generated ones every time, pretty much. It would be to hear something more detailed from those highly experienced in coding with C++.
Lots of things use threading, to not use it because some FAQ says they are evil, is silly.
Sure, you need to be careful, but you also need to be careful with dynamic memory management, over/under flows, pointers, and a whole slew of other 'unsafe' stuff, it's why you're programming in C++ and not some other, higher level language with safer features in exchange for performance.
I don't intend to avoid threads just because some FAQ or paper says they are evil; listening to a single source without considering the other side is always silly.
I'm more curious about what the alternative to threads is. Do all programs that require concurrent processes use them? Is threading how OSes allow multiple applications to run simultaneously?
For example, if I wanted two (longer) functions to run simultaneously without threads, I don't think it would be feasible without breaking up each function into small chunks that frequently return to some central control mechanism, which gives each path an equal chance to execute.
even single-core PC's use threads, think about it, if everything was running top-to-bottom how could you run a web browser and some music at the same time? the way OS's handle it is by switching to-and-from every thread quickly (which is what humans do, we can't multi task, +1 to the AI), I believe every program runs on its own unique thread.
You could simulate that with your single-threaded program I suppose.