News for hackers. Stuff that matters.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by Blackthorn on Thursday April 06, 2023 @02:18AM (#35464828)
Neat idea. Though I think having to pass a lambda for anything you want to do with the fields is awful ergonomics.

Maybe instead combine the two ideas. MutexProtected<T> but can be locked with MutexProtected<T>::lock() which returns a MutexLocked<T> object. That object then cleans up the lock when it goes out of scope, and also provides direct access to the enclosed type.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by akling on Thursday April 06, 2023 @02:33AM (#35464934)
Yes, that's a great way to improve the ergonomics of MutexProtected in simple cases! We should totally add that as a complement to the lambda API. :)
Re:MutexProtected: A C++ Pattern for Easier Concurrency by akling on Thursday April 06, 2023 @04:37AM (#35465817)
After sketching this out, it's nice that this:

    auto x = state.with([](auto& state) { return state.x; });
Becomes this:

    auto x = state.locked()->x;
But it also creates this very accessible footgun:

    auto& x = state.locked()->x;
Where it's way too easy to bind a reference to something that should be protected by the lock, but now isn't. So I'm not sure this is a great idea anymore.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by gpderetta on Thursday April 06, 2023 @04:42AM (#35465842)
At the end of the day, this is C++, there are no compiler tracked lifetimes and it is easy to leak references out of the lambda as well.

At least the synchronized pattern makes it easy to document which state is protected by which mutex.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by kajaktum on Thursday April 06, 2023 @08:26AM (#35467417)
Wait why is this a footgun?
Re:MutexProtected: A C++ Pattern for Easier Concurrency by akling on Thursday April 06, 2023 @08:56AM (#35467736)
Because now you're holding a reference to `x` which is supposed to be protected by a mutex, even after the mutex is unlocked.

With the lambda-only API, it's much harder to make this mistake, since a temporary reference like this will still go out of scope at the end of the lambda expression.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by kajaktum on Thursday April 06, 2023 @01:15PM (#35471425)
You specifically mentioned that this is a footgun:

> auto& x = state.locked()->x;

But I don't see how the reference here is gonna make a difference unless i am reading the lifetime of the lock here incorrectly. For example, this is perfectly fine right?

```

{

auto& x = state.locked()->x;

}

```

This will only be a problem if you have an outside struct that holds a reference

```

auto &a = "something";

{

auto& x = state.locked()->x;

a = x;

}

```

Which can still happen even if you use a lambda.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by MontagFTB on Thursday April 06, 2023 @09:03AM (#35467805)
Holding the reference to a field that is protected by a mutex implies there is another thread out there that will race with your reference in either reading or writing it.

Even just a read is racy, as there is no “atomic read” of any size value if it is not already wrapped as atomic.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by Blackthorn on Thursday April 06, 2023 @10:31AM (#35468770)
> But it also creates this very accessible footgun:

Well, you said it yourself in the article though. There's always ways around the locking; C++ doesn't really give you the full ability to guarantee a field is locked when access. You'll need to either trust the users to some extent or use a style guide to disallow the pattern (I'd suggest only allowing use of auto x = state.locked() to avoid lifetime questions around state.locked()->x). You'd need to use compiler annotations to get any better.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by secondcoming on Thursday April 06, 2023 @02:50AM (#35465049)
That looks even worse than a simple callback.

Does it preserve the correct ‘constness’ of the field in the case of shared mutexes?

Re:MutexProtected: A C++ Pattern for Easier Concurrency by ot on Thursday April 06, 2023 @02:50AM (#35465050)
That's how folly::Synchronized works, it supports both the callback interface as MutexProtected, and an interface that returns a RAII lock holder that gives access to the underlying object. It is generally the preferred synchronization utility at Meta and thus it is widely used.

https://github.com/facebook/folly/blob/main/folly/docs/Synch...

Re:MutexProtected: A C++ Pattern for Easier Concurrency by biesnecker on Thursday April 06, 2023 @10:08AM (#35468506)
As someone who had to occasionally write C++ in FBCode but not enough to really feel comfortable with it, `folly::Synchronized` and its straightforward semantics were really nice.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by mchicken on Thursday April 06, 2023 @02:54AM (#35465069)
Re:MutexProtected: A C++ Pattern for Easier Concurrency by FpUser on Thursday April 06, 2023 @06:09AM (#35466396)
No, but Boost is one giant bear of dependency with lots of duplication with STL. Personally I prefer to use tiny includable file / lib for specific functionality that I need rather than keep that bear on my real estate.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by planede on Thursday April 06, 2023 @07:12AM (#35466791)
Boost is multiple libraries, you don't have to use all of it. Many of it are header-only as well.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by FpUser on Thursday April 06, 2023 @07:25AM (#35466897)
Seriously I have no desire installing this giant thing and deciphering what can be extracted and how.

This is not to diminish value of Boost but it is just simply not for me.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by misnome on Thursday April 06, 2023 @07:35AM (#35466988)
not to mention that the documentation is generally utterly awful, with mostly only reference-style function documentation without exposition but manually distributed across lots of small 1-page paragraphs.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by lenkite on Thursday April 06, 2023 @12:55PM (#35471139)
This is in general is a problem with C++ libraries because doc generation is not standardized unlike Rust and other languages.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by misnome on Thursday April 06, 2023 @07:36PM (#35476021)
Doxygen was a great step forwards when it came out, but C++ documentation doesn’t seem to have evolved since, and it only fulfills the “reference” pillar of documentation.

Rust’s books (mdbook?) are amazing. Lots of libraries have good, clear documentation explaining how to use the library, on top of the automatic docs.rs output (which I still sometimes find difficult to navigate, but think is just my incomplete understanding). I have no idea how the community has managed to consistently achieve this.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by electroly on Thursday April 06, 2023 @10:05AM (#35468465)
Using only the header-only stuff is super easy; there's no deciphering or extracting at all. You just include the headers and _don't_ link the Boost library and see if it builds. If it doesn't, then I guess that wasn't a header-only library. You're not building or installing Boost at all; you're just downloading a copy of the headers. Only the template instantiations you actually use will end up in your binary.

(This sounds flippant but it's literally how I use boost in real life. Why decipher when the compiler can just tell you?)

Re:MutexProtected: A C++ Pattern for Easier Concurrency by dkersten on Thursday April 06, 2023 @09:31AM (#35468085)
I once tried to use a library that used boost and since I didn’t want to require my code to need the entirety of boost (which is gigabytes in size!) I tried to extract just the parts of boost that were needed. There are so many interdependencies between sublibraries of boost that after about two hours I decided I will never again use any library that relied on boost.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by jcelerier on Thursday April 06, 2023 @09:40AM (#35468169)
Boost isn't gigabytes in size wat are you talking about. All the headers are here in this 14mb archive: https://github.com/ossia/sdk/releases/download/sdk25/boost_1... and that is enough to use 90% of the boost libs as they are mostly header only
Re:MutexProtected: A C++ Pattern for Easier Concurrency by dkersten on Sunday April 16, 2023 @05:07PM (#35594283)
I don’t know. I’m just telling my experience. Whatever release I thought I needed at the time was huge.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by zbentley on Thursday April 06, 2023 @08:46AM (#35467628)
This common criticism conflates two things.

Boost is not monolithic, nor are any of the sub-parts especially huge (like ... asio is big because an asynchrony system is a big project; regex is small because it can be). Nor, as other commenters have pointed out, is including those dependencies particularly onerous; many are single-file or just a handful of files.

I suspect your criticism has more to do with "one giant bear of dependency". Which: fair enough. Getting Boost set up is a pain; the pain does not reduce if you only want to use one sub-part of it, because you still have to use b2, their custom build language and associated cmake-ish system (yuck!) to prepare it (there are ways to avoid this, but they are not well-supported). Even CMake's (pretty good!) Boost discovery/inclusion support can only conceal that weirdness to a point, and often breaks in unexpected environments--well, breaks more often than the average CMake setup, which breaks a lot...

Similarly, getting your IDE to understand boost's presence can be tricky since many IDEs (looking at you VS) have overly-opinionated ways of incorporating it that don't jive well with how a CLI/portable project build system pulls it in.

But the first and second paragraphs aren't the same thing. Initial setup pains are real and suck, but the initial setup pains and the overhead/monolithic-ness of actually using boost in your projects once it's set up are another. Initial setup pain is largely a one-time or infrequent cost, and the infrequency of that cost should be weighed against the not-inconsiderable convenience of Boost as a tool.

Not saying it's universally worth it; for some projects (especially ones that need to be built on a wide variety of environments, though this is rarer than most people, even some project authors, think) it's not appropriate. But many of the standard criticisms of Boost's runtime utility are specious.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by electroly on Thursday April 06, 2023 @10:14AM (#35468563)
I'll point out that if you're using header-only portions of Boost, you don't have to build Boost. Lots (most?) of Boost is header-only. You can just download the tarball and include headers out of it. It's barely even a dependency. Of all the deps my C++ app has, Boost is the easiest; I just download and extract and it's done, no build step. I don't need the actual compiled library; only the headers.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by dietr1ch on Thursday April 06, 2023 @04:03AM (#35465610)
When using locks I'm more paranoid about misusing the lock and don't mind typing a bit more to make the code obvious. Also, it seems that good auto-completion for that pattern is simple to achieve.

Alternatives to make sure you are not grabbing the wrong lock include this much uglier GUARDED_BY macro, - http://clang.llvm.org/docs/ThreadSafetyAnalysis.html

I'd say the extra lambda is a fair price to pay

Re:MutexProtected: A C++ Pattern for Easier Concurrency by Someone on Thursday April 06, 2023 @07:29AM (#35466939)
It’s a fair price to pay, but I think the suggested change is better because it makes you pay less for the same benefit. It still would be obvious that there’s a lock, but the code would IMO be simpler:

  thing->field.with([&](Field& field) {
    use(field);
  });
would become

  {
     auto field = Mutex.locker(thing.field); // or thing.field.lock();
     use(field);
  }
or

  use(Mutex.locker(thing.field));
Yes, that requires you to understand destructors exist, but if you’re programming C++, that’s a given.

I find that easier to understand (partly because the first doesn’t mention ‘lock’. I don’t think ‘with’ is the best name there)

Re:MutexProtected: A C++ Pattern for Easier Concurrency by simplotek on Thursday April 06, 2023 @08:38AM (#35467536)
> because it makes you pay less for the same benefit.

Requiring a lambda capturing stuff and be passed around is not what I would call "pay less", when the alternative is just adding a block and instantiate a std::lock_guard

Re:MutexProtected: A C++ Pattern for Easier Concurrency by Someone on Thursday April 06, 2023 @08:55AM (#35467722)
Fuller quote: “I think the suggested change is better because it makes you pay less for the same benefit”

The change suggested in this part of this discussion is by user “Blackthorn” in https://news.ycombinator.com/item?id=35464828. It says:

> Neat idea. Though I think having to pass a lambda for anything you want to do with the fields is awful ergonomics.

> Maybe instead combine the two ideas. MutexProtected<T> but can be locked with MutexProtected<T>::lock() which returns a MutexLocked<T> object. That object then cleans up the lock when it goes out of scope, and also provides direct access to the enclosed type.

User “dietr1ch” replied in https://news.ycombinator.com/item?id=35465610 he wanted to pay with a lambda to make it clear a lock is taken.

I tried to clarify that Blackthorn‘s suggestion would make that clear without requiring that lambda.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by somerando7 on Thursday April 06, 2023 @06:23AM (#35466471)
IMO passing a lambda for synchronized code makes it much easier to read (going off of working with folly::Synchronized)
Re:MutexProtected: A C++ Pattern for Easier Concurrency by selimnairb on Thursday April 06, 2023 @07:42AM (#35467040)
Yeah, the syntax/ergonomics is tortured. This is my problem with modern C++ in general. As someone who isn’t a full-time C++ programmer (but who maintains a library written in C++), I have to shy away from anything too fancy as the syntax is too baroque and hard to recall.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by simplotek on Thursday April 06, 2023 @08:42AM (#35467577)
> Yeah, the syntax/ergonomics is tortured. This is my problem with modern C++ in general.

I disagree. I mean, the ergonomics in this case are indeed awful, but this has nothing to do with C++, modern or not. This is an API design problem, possibly made worse by people trying to be too clever for their own good.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by simplotek on Thursday April 06, 2023 @08:32AM (#35467479)
> Neat idea.

I don't agree. It sounds like a higher level path to deadlocks.

> Maybe instead combine the two ideas. MutexProtected<T> but can be locked with MutexProtected<T>::lock() which returns a MutexLocked<T> object. That object then cleans up the lock when it goes out of scope, and also provides direct access to the enclosed type.

It sounds like you're trying to invent std::lock_guard with extra steps.

https://en.cppreference.com/w/cpp/thread/lock_guard

Re:MutexProtected: A C++ Pattern for Easier Concurrency by jmull on Thursday April 06, 2023 @09:01AM (#35467780)
> It sounds like ... std::lock_guard with extra steps.

I'm not the previous poster, but I think they are still suggesting that the value not be accessible until the lock is acquired.

Something like this (Apologies, I haven't done C++ is a long time and this is just off the top of my head. It's based on the example from the lock_guard example for the sake of comparison):

    guardthing<int> guarded_value;

    void safe_increment()
    {
        auto lock = guarded_value.lock();
        lock.value += 1;

        std::cout << "value: " << value << "; in thread #"
                  << std::this_thread::get_id() << '\n';
     
        // the mutex in guarded_value is automatically released when lock
        // goes out of scope
    }
Re:MutexProtected: A C++ Pattern for Easier Concurrency by Blackthorn on Thursday April 06, 2023 @10:28AM (#35468722)
> It sounds like you're trying to invent std::lock_guard with extra steps.

No, because lock_guard doesn't guard fields. lock_guard is simply the RAII option they were talking about in the article.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by loeg on Thursday April 06, 2023 @11:41AM (#35469928)
> MutexProtected<T> but can be locked with MutexProtected<T>::lock() which returns a MutexLocked<T> object. That object then cleans up the lock when it goes out of scope, and also provides direct access to the enclosed type.

In fact, MutexProtected provides ::lock_exclusive() and ::lock_shared() methods which do exactly that. The article just fails to mention them. https://github.com/SerenityOS/serenity/blob/master/Kernel/Lo...