12 ms·
Hell is a multi-threaded C++ program (2006)
- fsloth 9y agoI've found message passing and thinking in transactions are pretty good architectural patterns for maintaining ones sanity. Message queues with spinlocks for performance critical code (mutexes are slow). The biggest sin generally is to disregard the overhead caused by thread management and thinking more threads makes the sofrware run faster. I've seen people try to parallellize a sequential program by just spawning mutexes everywhere and then thinking now any number of threads can do whatever they please. Of course when tested the system was quite a bit slower as when it ran on a single thread (the system was quite large so quite a lot of work was needed before reaching this state).
- kbwt 9y ago> mutexes are slow Be careful with that. First off, what people refer to as "mutex" is usually a spinlock that falls back to a kernel wait queue when the spin count is exceeded. There are even adaptive mutexes that figure out at runtime how long the lock is typically held and base their spin count limit on that. Secondly, busy-waiting is often worse than a single slow program, because you actively slow down all of the other running programs.
- ajross 9y ago> spinlocks for performance critical code (mutexes are slow). Gah, no. Userspace spinlocks are the deepest of voodoo and something to be used only by people who know exactly what they are doing and would have no difficulty writing "traditional" threaded code in a C/C++ environment. Among other problems: what happens when the thread holding the spinlock gets preempted and something else runs on the core? How can you prevent that from happening, and how does that collision probability scale with thread count and lock behavior? Traditional locking (e.g. pthread mutexes, windows CriticalSections) in a shared memory environment can be done with atomic operations only for the uncontended case, and will fall back to the kernel to provide blocking/wakeup in a clean and scalable way. Use that. Don't go further unless you're trying to do full-system optimization on known hardware and have a team full of benchmark analysis experts to support the effort.
- jcelerier 9y ago> Gah, no. Userspace spinlocks are the deepest of voodoo and something to be used only by people who know exactly what they are doing and would have no difficulty writing "traditional" threaded code in a C/C++ environment. If you ever used pthread_mutex with glibc then you use spinlocks without knowing it. The implementation spins for some time before going for a full kernel mutex.
- ajross 9y agoSure, and C library authors qualify. App developers don't.
- mannykannot 9y agoI think the warning is directed against rolling your own spinlocks without careful consideration, including a realistic evaluation of whether you know every issue involved in doing so.
- whatidonteven 9y agoThere's a bit of a terminology confusion here. When they say "spinlock", most people mean a pure spinlock that spins forever until it succeeds. "Mutex" on the other hand might have a fast-path that spins a few times before inserting the thread onto a wait list.
- noobermin 9y agoIs there a good message passing library for C++? Sounds like something for the stl to include.
- danbolt 9y agoI know coworkers that speak well of ZeroMQ: http://zeromq.org http://zeromq.org
- shaklee3 9y agoBoost has one, but not the stl
- jcelerier 9y ago> Is there a good message passing library for C++? Sounds like something for the stl to include. Qt does it with signals-slots. What I generally do is that I have a queue of std::function and just pass lambdas with the capture being copied.
- exDM69 9y agoAn uncontended mutex is just as fast as a spinlock (on modern operating systems using a futex). It takes about 25 nanoseconds to lock it. The difference is when there's contention. A spinlock will burn CPU cycles but a mutex will yield to another thread or process (with some context switch overhead). A spinlock should only be used when you know you're going to get it in the next microsecond or so. Or in kernel space when you don't have other options (e.g. interrupt handler). Anything else is just burning CPU cycles for nothing. Mutex and condition variables (emphasis on the latter) are much more useful than spinlocks and atomics for general multithreaded programming.
- oppositelock 9y agoYou have to be careful with those things - for instance, you have to special case whether you'r on a single CPU or multiple CPU system, because a spin lock will block forever in a non-preemptive context, such as the kernel. Outside of hard realtime code, there's zero reason to use spin locks.
- kbwt 9y agoIn hard realtime code, a FIFO mutex gives you bounded wait time because you have a fixed number of threads. Interesting read: http://www2.rdrop.com/~paulmck/realtime/SMPembedded.2006.10.09b.pdf http://www2.rdrop.com/~paulmck/realtime/SMPembedded.2006.10....
- gpderetta 9y ago> Outside of hard realtime code, there's zero reason to use spin locks. On some common architectures, releasing a spin lock is cheaper than releasing a mutex.
- comex 9y agoOn all architectures, releasing a mutex requires at least a branch (to see if you need to wake up sleeping threads) that you don’t need with a pure spinlock. But if you don’t have a guarantee the lock owner won’t be preempted, well, spinning for a whole timeslot is quite a bit more expensive…
- LeoNatan25 9y agoUsing spinlocks can cause livelocks under certain conditions due to priority inversion.
- hellofunk 9y agoUnless you use this excellent library: https://actor-framework.org/ https://actor-framework.org/
- lookACamel 9y agoNote that this blog post was written in 2006. The other major model for multi-threading is known as message-passing multiprocessing. Unless you're familiar with the Occam or Erlang programming languages, you might not have encountered this model for concurrency before. Two popular variants of the message-passing model are "Communicating Sequential Processes" and the "Actor model". Why would you want to learn about this alternative model, when Pthreads have clearly won the battle for the hearts and minds of the programming public? Well, besides the sheer joy of learning something new, you might develop a different way of looking at problems, that'll help you top make better use of the tools that you do use regularly. In addition, as I'll explain in Part II of this rant, there's good reason to believe that message-passing concurrency is going to be coming back in a big way in the near future.
- mbil 9y ago> While standardization and support for [threaded] APIs has come a long way, their use is still predominantly restricted to system programmers as opposed to application programmers. One of the reasons for this is that APIs such as Pthreads are considered to be low-level primitives. Conventional wisdom indicates that a large class of applications can be efficiently supported by higher level constructs (or directives) which rid the programmer of the mechanics of manipulating threads. Such directive-based languages have existed for a long time, but only recently have standardization efforts succeeded in the form of OpenMP. OpenMP is an API that can be used with FORTRAN, C, and C++ for programming shared address space machines. OpenMP directives provide support for concurrency, synchronization, and data handling while obviating the need for explicitly setting up mutexes, condition variables, data scope, and initialization. From Introduction to Parallel Computing OpenMP: http://www.openmp.org/ http://www.openmp.org/
- dom0 9y agoSince OMP Tasks it is kinda useful for non-numerical non-trivial applications as well, though still kinda... odd. Odd but neat.
- xaa 9y agoYes, every time I hear people talking about how evil or difficult threads are, I just think back to my frequent use of OpenMP. It is really quite easy to take a single-threaded program and make it multithreaded with OMP -- so long as the jobs only read, and do not write shared state. Usually I will write a program that does the following: 1. Initialize global read-only data structures 2. Parallelize jobs across STDIN lines (or whatever) 3. Output results within a #pragma omp critical block It works wonders, and is literally 5 additional lines of code to make a single-threaded program multithreaded with OMP. But only for some types of program. The concept is very similar to what GNU parallel does. In short, threads are not the problem per se, they are just the wrong tool for the job if you have a multiple-readers multiple-writers situation. Message passing, databases, or something else are more appropriate in those cases. But you will pry my read only shared memory space out of my cold, dead hands.
- 9y ago
- deleted 9y ago[deleted]
- chris_wot 9y agoI think one thing to learn is how monitors work.
- andreasgonewild 9y agoThere is no need to use raw pthreads in modern C++, it comes equipped with a much more convenient and capable standard library module. I prefer working with channels, which are still missing from the library; but they are trivial to add: https://github.com/andreas-gone-wild/blog/blob/master/diy_cpp_chan.md https://github.com/andreas-gone-wild/blog/blob/master/diy_cp...
- to3m 9y agoThis is good advice and should never have attracted a downvote! I'm not actually sure that the C++ library does anything that pthreads doesn't - maybe std::lock, perhaps? - but it's more convenient to use (and it's not like pthreads is bad to start with...), and the pieces work well and fit together quite nicely. Some good use of move semantics. unique_lock is neat and you can do stuff like vector<thread>. The C++ thread library works on Windows, too...
- comex 9y ago> I'm not actually sure that the C++ library does anything that pthreads doesn't There’s std::atomic, which didn’t have a standardized equivalent in pthreads. In lieu of that you’d see some ad-hoc combination of GCC builtins, OS-specific functions, and `volatile`, with the result often not guaranteed to work on architectures other than x86 (if at all). Compared to that, std::atomic is both more flexible and easier to get right. The C ‘backport’, stdatomic.h, is also available on most platforms these days...
- andreasgonewild 9y agoI was thinking about mostly about atomics, which aren't covered by pthreads; and I can't remember shared mutexes being supported either.
- ridiculous_fish 9y agoUnfortunately std::thread is not as capable as pthreads. For example, std::thread has no facilities for controlling the stack size or signal mask. Whenever I use channels, I find I need them to synchronize with each other. For example, I have a Go data structure with a write channel and a separate read channel; how do I avoid a read-after-write hazard? The simplest solution is to make them the same channel that ships closures, to ensure requests are processed in-order. But then why not just make all channels ship closures, like libdispatch...
- noobermin 9y agoSo to check, I googled C++11 threads memory model, and it[0] looks like his main complaint isn't helped by C++ 11 threads, as they essentially allow shared state too. They are also a thin veneer over the OS threads, for unix types, pthreads I imagine. [0] http://en.cppreference.com/w/cpp/language/memory_model http://en.cppreference.com/w/cpp/language/memory_model
- duneroadrunner 9y agoYeah, I don't know if there's ever going to be a portable way to enforceably "disallow" the sharing of state between threads in C++. For straightforward cases, the SaferCPlusPlus library provides an easy way to "wrap" any object so that access from different threads is safely controlled[1]. [1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus#asynchronously-shared-objects https://github.com/duneroadrunner/SaferCPlusPlus#asynchronou...
- jcelerier 9y ago> Yeah, I don't know if there's ever going to be a portable way to enforceably "disallow" the sharing of state between threads in C++. Not sharing state at all means that you cannot even do message passing other than through sockets or something like this. At some point you have to share at least a message queue to be able to go from thread A to thread B.
- fnj 9y agoHonest question: If you aren't going to share data between/among threads, why use threads at all? Why not just fork-abd-exec?
- danieltillett 9y agoBecause on windows it is much slower.
- to3m 9y agoIf it's the same, it's the same, so there's no reason to do one rather than the other. Why not just create a thread?
- andrewguenther 9y agoWhen I think about hard problems, I like to look at projects I feel handle these problems well. Having worked in the Chromium code base for some time now, I really appreciate their approach to threading. So much so, I really wish they would distribute their base/threading module as its own project. https://chromium.googlesource.com/chromium/src/+/lkcr/docs/threading_and_tasks.md https://chromium.googlesource.com/chromium/src/+/lkcr/docs/t...
- oppositelock 9y agoI've been writing massively threaded C++ code since the 1990's, so I can comment with some serious experience here. My code used to run on 256CPU SGI's and had high cpu utilization across all of them. When designing C++ code for threading, you have to keep a few things in mind, but it's not particularly harder than on other languages with thread support. You generally want to create re-entrant functions which pass all of their state as input, and return all results as output, without modifying ant kind of global state. You can run any number of these in parallel without thread safety issues, or locks. When using multiple threads with shared data, you have to protect it with locks, but I generally use a single thread to access data of this sort which produces work for other threads to do, which can be queued or distributed any other way. Producer/consumer queues show up a lot in good designs, and your single-threaded choke points are usually a producer or a consumer, which allows thread fan-in or fan-out. (As a side note, Go really got this right with channels). What leads to problems in threaded C++ programs is unprotected, shared state. Some C++ design patterns make life particularly hard, such as singletons, which are just a funny name for global variables. C++ gives you lots of ways to make something global accidentally - static member variables, arguments by reference, for instance. True C++ hell is taking a single threaded C++ program and making it multi-threaded after the fact, but that's hell in any language which allows you to have globals of any sort and doesn't perform function calls within some kind of closure. The article in this post is being a little reductionist. You don't need to pick a threading model - some of your threads can pass messages, others can use shared data, all in the same program. The lack of thread local storage is not an issue, because you partition the data so that no two threads are working on the same thing at the same time.
- nemoniac 9y agoSo, functional programming then?
- oppositelock 9y agoYup, basically.
- 9y ago
- paulddraper 9y agoBut...like so many other abstractions created by people with puny hardware, shared memory parallelism is very fast.
- moderation 9y agoGood article by the creator [0] on how Lyft's relatively new L4-7 proxy called Envoy [1] handles multi-threading at https://medium.com/@mattklein123/envoy-threading-model-a8d44b922310 https://medium.com/@mattklein123/envoy-threading-model-a8d44.... There are now more people at Google contributing to Envoy than Lyft. 0. https://twitter.com/mattklein123/ https://twitter.com/mattklein123/ 1. https://lyft.github.io/envoy/ https://lyft.github.io/envoy/
- beached_whale 9y agoGenerally, do not use raw threads. They are a building block for higher level abstractions like task queues, pipelines, futures... which lend themselves to higher level algorithms like your sorts, filters, find, map, reduce, map_reduce... C++17 gets a lot of this with much of the algorithms header getting parallel/vectorized versions. But it doesn't have a task stealing work queue or something like then to pass the value when it completes. Boost has a version now that does though and this lets you have the next function called when done instead of waiting and blocking. But if you spend your time in the thread zone you are probably putting too much thought into threading and not the problem you are solving. Plus, using a task queue can yield really good results for lower level algorithms.
- kuwze 9y agoThe FoundationDB people invented Flow to easily create multithreaded distributed systems[0]. [0]https://gist.github.com/Preetam/98e80cd17ecb8748c72b https://gist.github.com/Preetam/98e80cd17ecb8748c72b
- iainmerrick 9y agoPthreads seems to be getting a bad name here, but compared to other low-level threading APIs it's excellent. It has a proper join operation, which is weirdly lacking in some other libraries I've used, and it has a good choice of key synchronisation primitive: condition variables. Other choices are possible, like semaphores, but those are fiddly to use for some applications. Building semaphores on top of CVs is pretty easy, building CVs on top of semaphores is very hard. Likewise Windows events. Higher-level systems have primitives like message queues, thread pools, and channels. Those are all great but they can't always be used to build the exact system you need without a lot of overhead. C and C++ are all about emphasising speed and flexibility over safety. Whether you think that's a good or bad approach, it is what it is, and pthreads is the right design for that approach. Once you come up with the high-level multithreading abstraction of your dreams, what else would you rather implement it in than pthreads? The one thing that was missing from pthreads was atomic operations for modern lockless data structures, so it's great that those have finally been added to the standard library in both C and C++.
- mbessey 9y agoI really need to do an update of that article, with C++ std::thread, libDispatch, and some of the other stuff that's come along in the intervening 11 years. I still spend a lot of time tracking down other people's threading bugs (in an entirely-different codebase, these days) though.