Skip to main content
Repair value copy/paste error fixed
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308
  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;false;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    

Posted a rewrite using my suggestions for review here: (Optionally Concurrent) FIFO

  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    

Posted a rewrite using my suggestions for review here: (Optionally Concurrent) FIFO

  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return false;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    

Posted a rewrite using my suggestions for review here: (Optionally Concurrent) FIFO

added 139 characters in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    

Posted a rewrite using my suggestions for review here: (Optionally Concurrent) FIFO

  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    
  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    

Posted a rewrite using my suggestions for review here: (Optionally Concurrent) FIFO

added 774 characters in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
     
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
     
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    
  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
     
     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
     
     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  1. Don't abuse SFINAE to provide two classes in one.
    It doesn't work for fields, and is generally clunky.

    Instead, either invest in private inheritance and judicious use of using, or write them separately.

  2. Your operator= deadlocks on self-assignment in the multithreaded case.

  3. If you do an early return, there's no use to writing an else-block.

     if (boolean_expression)
     {
         some_code;
         return true;
     }
     else
     {
         some_other_code;
         return false;
     }
    

    Better change that to:

     if (boolean_expression) {
         some_code;
         return true;
     }
     some_other_code;
     return true;
    
  4. You can return a bool directly, no need to put it into a condition:

     if (boolean_expression)
         return true;
     else
         return false;
    

    Better change that to:

     return boolean_expression;
    
  5. buf_capacity_ has exactly two uses: Padding out your class-size and pessimizing code.
    It's always exactly CAPACITY + 1.

  6. Most of your multithreaded code is of the form:

     std::unique_lock<std::mutex> this_lock(this->mutex_);
     auto result = somecode();
     //this_lock.unlock();
     cv_.notify_one(); // or occassionally better cv_.notify_all();
     return result;
    

    Defining a simple private member-template helps clean it up:

     template<bool all = false, class F>
     auto locked(F f) noexcept(noexcept(f())) {
         auto result = (std::unique_lock<std::mutex>(mutex_), f()); // minimal lock-duration
         all ? cv_.notify_all() : cv_.notify_one();
         return result;
     }
     // Used like:
     return locked([&]{ return somecode(); });
    
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
Loading