Boost logo

Boost :

Subject: Re: [boost] [flat_set] When to sort?
From: Oswin Krause (Oswin.Krause_at_[hidden])
Date: 2017-03-27 15:32:09


On 2017-03-27 07:40, Vladimir Batov via Boost wrote:
> On 2017-03-27 10:54, Andrey Semashev wrote:
>>
>>>> How does sort_() handle thread safety? Are you proposing to add a
>>>> mutex to the container?
>>>
>>> Yes, I am/was... Is it a problem? I can't see it being a bottle neck
>>> as it
>>> all will be optimized inside sort_() (no unnecessary locks, etc.).
>>
>> I'm sorry, but that is a no-go. Too often containers are used with
>> external locking, and internal locking would just waste performance,
>> especially in such a hot spot as begin(). BTW, I don't believe any
>> compiler is able to optimize away unnecessary locking (thank god!)
>
> 1. There does not have to be "internal locking would just waste
> performance":
>
> void sort_()
> {
> if (sorted) return;
> lock
> if (sorted) return;
> ...do actual sorting
> }

Double Checked Locking is notorious for failing randomly. sorted must at
least be atomic.

One of many articles about it:
http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk