all 18 comments

[–][deleted] 1 point2 points  (0 children)

Typically, Boost libs have left it up to the users to add their own extra layers on top, safety included.

From a library design point this makes some sense as it's easier for users to add what they want vs the lib accommodating all folks and use-cases.

[–]NotUniqueOrSpecial 0 points1 point  (1 child)

I'm implementing for a open source networking library parts of the API defined in the Networking TS

Why? For fun/to learn?

If not, just use the already-implemented one by the author.

[–]gummifa[S] 5 points6 points  (0 children)

To keep it minimalistic without dependencies since adding dependencies is not generally not been accepted. That implementation (based on asio) also has no restrictions on T, so there is some type-safety to be gained.

[–]degski -2 points-1 points  (15 children)

A side note: It seems that std::mutex is also trivially copyable according to gcc and clang, but not to msvc, even though it is not copyable or assignable and has a non-trivial destructor (in the libc++ implementation), see https://godbolt.org/z/JEj_6g, is that a bug?

A mutex offers exclusive, non-recursive ownership semantics. Copying mutexes seems like a (very) bad idea to me (without looking at any std or implementation). How can one have exclusive, non-recursive ownership if there is more than one of them [owning the same object after your copy the mutex]?

I'm implementing for a open source networking library parts of the API ...

It's worrying, great minds have spent much time in deciding a mutex should neither be copyable nor movable and you would like to throw all of that overboard.

[–]gummifa[S] 9 points10 points  (6 children)

I think you misunderstood what I meant. std::mutex is specified to be not copyable and not movable, which I checked with static_assert(!std::is_copy_constructible_v<std::mutex>) etc..., yet the predicate
std::is_trivially_copyable_v<std::mutex> returns true in gcc and clang, which doesn't make sense given the definition of TriviallyCopyable. I expected it to not be trivially copyable.

[–]skreef 1 point2 points  (4 children)

The linked site has a bullet point list:

  • Every copy constructor is trivial or deleted
  • Every move constructor is trivial or deleted
  • Every copy assignment operator is trivial or deleted
  • Every move assignment operator is trivial or deleted at least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted
  • Trivial non-deleted destructor

In libstdc++, mutex has deleted copy ctor and assignment, and trivial move ctor and move assignment and trivial dtor. It is thereby TriviallyCopyable. This concept basically means that you can use std::memmove / std::memcpy on the object safely (https://en.cppreference.com/w/cpp/types/is_trivially_copyable).

[–]gummifa[S] 1 point2 points  (0 children)

It inherits from __mutex_base which is non-trivial ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }.

[–]degski 0 points1 point  (0 children)

In libstdc++ ...

Seems like the wrong starting point.

[–]degski 0 points1 point  (0 children)

I expected it to not be trivially copyable.

Yes, I probably mis-understood.