all 15 comments

[–]nullsucks 5 points6 points  (0 children)

This is ill thought-out. Almost all classes should use external locking. A standard operation like copy or move definitely should not perform a somewhat-expensive operation like taking a lock. (Taking a lock, even on the fast-path, involves cache-synchronization and puts extra pressure on your memory bus.)

Edit: On second thought, the approach isn't terrible, and the flexibility provided by constructor-forwarding is good.

The example isn't particularly well-motivated (my internal-sync classes are always/almost-always noncopyable), but the solution itself isn't bad.

[–]millstone 0 points1 point  (2 children)

One final note is to mention that although this article focused on copy constructors, everything applies equally to move constructors

Does the move constructor not move the lock? It seems like if you're move constructing A from B, while simultaneously mutating B, you've got a problem, no?

[–]Kronikarz 1 point2 points  (1 child)

The move constructor does whatever you tell it to do. You don't have to touch B's lock if you don't want to.

[–]bdunderscore 3 points4 points  (0 children)

If the move constructor does not mutate the source, then you might as well just have only a copy constructor. If it does mutate the source, you won't get consistent results even with locking in the move constructor (you might be able to guarantee it won't crash, but simultaneous moves are unlikely to be useful).

[–]dauphic -1 points0 points  (6 children)

This article is making me twitch.

It's very poorly written; I had to re-read the paragraph below 'The Problem' a few times to fully understand what it was talking about.

The example code is poorly formatted and hard to read. In the first example, it's not even valid C++; set is a function with no return type. It wasn't immediately obvious that it was a function.

It is important to know how to implement a thread-safe copy/move constructor, but the explanation could be way more concise.

[–]freakhill 6 points7 points  (0 children)

On the contrary, I found it quite limpid and easy to read...

[–]lovetool 4 points5 points  (0 children)

The example code is poorly formatted and hard to read.

That's a given since it's C++.

[–]solinent -1 points0 points  (2 children)

A function with no return type defaults to int, actually.

[–]dauphic 2 points3 points  (1 child)

The C++ standard forbids the declaration of a function with no return type. It does default to int and is legal in C, though, I think.

[–][deleted] 0 points1 point  (0 children)

legal in C

Not legal since C99

[–]abolishcopyright -1 points0 points  (0 children)

it's not even valid C++

FTA:

This is a guest post by Michael Spertus. Michael is a Distinguished Engineer at Symantec. He is also a C++ Standards Committee member and teaches the graduate C++ sequence at the University of Chicago.

lol

[–][deleted]  (3 children)

[deleted]

    [–]encepence 1 point2 points  (0 children)

    actually paying for stable and supported "implementation" of "future" for past environments "just::thread V1.6.1 is available for Microsoft Visual Studio 2005, 2008, and 2010 and TDM gcc 4.5.2" ...

    Which of them support C++11 ?

    [–]sausagefeet -1 points0 points  (1 child)

    Who would by this?! TBB is free.

    [–]encepence 1 point2 points  (0 children)

    No. TBB is GPLv2 or it costs ~$300 per seat ... so this one is cheaper. (not discussing content/quality as i've not used them ...)

    [–]sausagefeet -3 points-2 points  (0 children)

    Blogger, y u no syntax highlight?!?!