7 ms·
An Improved Thread with C++20
- dataflow 6y agoWhy an entirely new class? Why not just pass a constructor argument to specify joining behavior? It's not like there's any reason to believe this is the only thing we'll ever want to customize for a thread.
- gpderetta 6y agoI don't think making blocking on destruction conditional is a good thing. For example futures returned by std::async block on destruciton as opposed to other futures (even if they have the same type). This is generally considered a mistake as the future behaves very differently depending on how it was created and it can cause issues (i.e. deadlocks) for code that did not expect the blocking behaviour. As an aside I believe that all kind of blocking in a destructor (and thus jthread) is generally a mistake. In order of preference I believe that: - enforce explicit join, abort or detach via the type system (hard to do in C++) - abort the async computation - abort the whole program (i.e. assert) - detach the async computation are all preferable to blocking. I think I'm in the minority though.
- dataflow 6y agoCould you elaborate on why blocking in a thread's destructor is bad? You mention it is but don't really give any reason. Every time I use std::thread I find the need to wait on it in the destructor. In fact any non-joining thread is a code smell to me. If you just detach (or abort...) a thread, then there's simply no way to ensure that main() will return after the thread has finished—and you always want to make sure all threads finish their tasks and clean up properly before the process terminates. This requires you to wait on threads during app shutdown, which generally corresponds with destruction, which means you need to join on destruction. I actually don't understand why you would want it another way by default (if at all).
- gpderetta 6y agoI just think that blocking is an important enough effect that that needs to be accounted for and it should be explicit in code. Deadlocking is the obvious reason, but also the possibly large pauses are problematic. I'm a big believer in crash-only software and I think that "clanup" at shutdown is code smell and a symptom of unreliable software [1]. Just call _Exit when the application is done. [1] Cleaning up to decrease the noise for the benefit of a leak detector is useful though, but by experience, I'm not yet convinced that the effort is worth it.
- OskarS 6y agoJust last week I was debugging an issue with a library that used SQLite internally, and I noticed that whenever I shut down, it would leave all these "something.db-shm" and "something.db-wal" files around in addition to the "something.db" file. My hunch was the library wasn't properly deinitializing SQLite, and indeed that was the case, if you call sqlite3_close() properly, these files are deleted (they're essentially journal files for SQLite). The library was essentially doing what you're suggesting, ending without doing the proper cleanup. Destruction, in general, is one of the most complex areas of software development in my opinion. You're right, blocking or throwing errors during destruction is terrible, but you really do have to genuinely clean things up when exiting, you can't leave messes behind like this. However, in addition to that: you can't ever count on destruction occuring, because it's entirely possible that your program crashes, the user kills the process, the computer runs out of battery (or someone janks the cable out) or some other random thing causes your process to exit prematurely. You really need a lot of nuance in this area, and I don't agree with "cleanup at shutdown is a code smell". But you're certainly right that you can go too far in the other direction as well: destruction has to be fast, non-blocking, error-free, and never required.
- gpderetta 6y ago> you can't ever count on destruction occuring, because it's entirely possible that your program crashes, the user kills the process, the computer runs out of battery (or someone janks the cable out) or some other random thing causes your process to exit prematurely. Well yes, that's why you can't wait for shutdown time to cleanup your mess. In this case, either the files need to survive process exit or they can be unlinked as soon as they are created. Another option is to have very simple supervisor process whose only job is to cleanup. For a DB like sqlite it does makes sense to have some amount of shutdown cleanup to close transactions and mark the log as stale and avoid any recovery on startup, but it should be considered an optimization and not be a requirement. To make this short, I do in fact agree with your nuanced view. Pragmatism always trumps dogmatism in software engineering.
- jrimbault 6y agoIn my opinion, when you have an internal boolean flag for two behaviors, the interface you should probably expose isn't the flag in one function, but two different functions.
- dataflow 6y agoSo like std::ios_base::fmtflags should actually be 19+ different functions and/or classes?
- MaxBarraclough 6y agoedit I see I'm late, dataflow already made this point. I'll leave this here anyway. This rule of thumb is often useful but there are many exceptions. As an example, the internal behaviour of this OpenCL function [0] is presumably quite different depending on which bits of the second parameter are set, but it's still a good design as all possible behaviours of the function are still closely related as far as the user is concerned. [0] https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clCreateBuffer.html https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xht...
- jrimbault 6y ago
- throwaway912598 6y agoThe main reason is so that the type system knows which thread is which and can handle it differently. They may still inherit from the same implementation and do what you suggest though.
- dataflow 6y ago> The main reason is so that the type system knows which thread is which and can handle it differently. Why is this useful in this case, is my question. You can try to put lots of things into the type system that aren't, but that doesn't automatically make them better. In fact I'd argue it makes things worse here, e.g., now every container will be instantiated twice depending on whether you use jthreads or threads, almost doubling the amount of work the compiler has to do for them.
- throwaway912598 6y agoIt's useful because you may be able to make compile-time decisions on the basis of the type of thread you're dealing with. What those are, I don't know. As for instantiations, an astute library developer will be able to do the right thing. It's not that hard.
- gpderetta 6y agosay you have function that returns a thread object (which is perfectly fine as std::thread is moveable). The caller cannot assume that the thread will join on destruction and needs to conservatively join or detach explicitly anyway. Re container, they will only be instantiated twice if you actually use both std::jthread and std::thread of course. The idea (which I disagree) is that jthread is the better solution and std::thread is sort of deprecated.
- dataflow 6y ago> The caller cannot assume that the thread will join on destruction and needs to conservatively join or detach explicitly anyway. This seems a little backwards to me? If the thread should join on destruction then that flag should be set. If not then it should be clear. I'd argue that flag should probably be set in general (and it seems you might feel the same way), but if you have a reason to clear it, then you're just fighting your own program by ignoring it. > Re container, they will only be instantiated twice if you actually use both std::jthread and std::thread of course. The idea (which I disagree) is that jthread is the better solution and std::thread is sort of deprecated. Good point!
- fpoling 6y agoJoinable thread and non-joinable one are very different in usage. It is rather appropriate to use a different class.
- masklinn 6y agostd::thread are already joinable (and that's their default state), and jthreads can be detached. The one difference between std::thread and jthread is that std::thread terminates on destruction, while jthread joins. That could easily be a ctor parameter (as well as "detach on destruction" as a third option).
- dataflow 6y ago> Joinable thread and non-joinable one are very different in usage. What is the usage of a "non-joinable" thread in the first place? I mentioned in a comment that that concept itself seems like a code smell to me, and certainly not something that should be the default. See here to read why: https://news.ycombinator.com/item?id=26142754 https://news.ycombinator.com/item?id=26142754
- usefulcat 6y agoBecause std::jthread adds new methods as well. If you don't use a different class, then you end up with a weird situation where you still have to decide what get_stop_source() and get_stop_token() should do when the new constructor argument is omitted. That's definitely less clean, IMO.
- dataflow 6y agoI mean, you can just throw an exception or leave it undefined when the state isn't what you expect... isn't that a normal thing with any nontrivial class? e.g., what should unique_lock do if you call lock() when you passed adopt_lock_t to the constructor? What should unique_ptr do if you call operator->() when you passed null to the constructor? What should fstream do if you call open() when you already passed a file name to the constructor? etc.
- lrossi 6y agoA little disappointing. I was hoping to see lightweight thread behavior or other lessons learned from Go. The ability to pause a thread from outside is interesting, but it doesn’t seem very useful in general. It also comes with risks. What if it’s holding a lock? That could cause a performance/latency disaster.
- jeffbee 6y agoAny function that returns while holding a lock may be a disaster. That's not specific to threads. This is why you should prefer RAII locked mutexes like std::scoped_lock whenever possible.
- AnimalMuppet 6y agoWhat you say is true, but (as far as I can tell) unrelated to the issue at hand. Yes, if you return holding a lock, it's a potential disaster. And if you pause a thread from outside while the thread is holding a lock, it's a similar disaster. But RAII and scoped_lock won't save you if a thread is paused from outside, since the paused thread never makes it out of the scope that took the lock.
- jeffbee 6y agoYou are talking about some theoretical thing that doesn't apply here. There is no facility in std::jthread that allows it to become "paused".
- AnimalMuppet 6y agoThen what was lrossi referring to when he (?) said: >>> The ability to pause a thread from outside is interesting, but it doesn’t seem very useful in general. >>> It also comes with risks. What if it’s holding a lock? That could cause a performance/latency disaster. Which you replied to, not claiming that it couldn't be paused from outside, but instead that the lock risk was just the same as returning from a function.
- superkuh 6y agoJust remember: when you use C++$latest features in your code, you won't be able to distribute, compille and run it on an distro older than a handful of years. That is to say: almost all of them.
- missblit 6y agoWhy not? Of course you need an up-to-date compiler, but beyond that I've never heard of any fundamental issues in compiling binaries that work on older systems (well besides installing newer compilers or cross-compiling being a real PITA to set up on Linux sometimes, but that has nothing to do with language features).
- superkuh 6y agoReplacing the compiler on a distro is not a simple task and it has system wide consequences that make it very undesirable for continued system stability. Yes, you can try compiling everything static and then distributing the fat binary. It might work on most systems. But if it's dynamically linked then it'll fail with libc++ errors locally (local libc++ not having c++$latest support).
- brown9-2 6y agoWhy can’t you install multiple versions of the compiler on the system? This seems like a poor design choice that could be easily solvable if you move outside the “only run what the distribution blesses” idea.
- DrBazza 6y agoThis is a considerably better discussion of std::thread and std::jthread: https://www.youtube.com/watch?v=elFil2VhlH8 https://www.youtube.com/watch?v=elFil2VhlH8
- _ZeD_ 6y agoI think a relevant link is https://trio.discourse.group/t/structured-concurrency-proposal-for-c-20/125 https://trio.discourse.group/t/structured-concurrency-propos...