Skip to main content
added 32 characters in body
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180
template<typename T> T atomic_fetch_max(std::atomic<T>* obj, typename std::atomic<T>::value_type arg) noexcept { auto prev = *obj;obj->load(std::memory_order_relaxed); while (prev < arg && !obj->compare_exchange_weak(prev, arg)) {} return prev; } 
template<typename T> T atomic_fetch_max(std::atomic<T>* obj, typename std::atomic<T>::value_type arg) noexcept { auto prev = *obj; while (prev < arg && !obj->compare_exchange_weak(prev, arg)) {} return prev; } 
template<typename T> T atomic_fetch_max(std::atomic<T>* obj, typename std::atomic<T>::value_type arg) noexcept { auto prev = obj->load(std::memory_order_relaxed); while (prev < arg && !obj->compare_exchange_weak(prev, arg)) {} return prev; } 
added 682 characters in body
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180
  • Use "\n" instead of std::endl; the latter is equivalent to the former, but also forces the output to be flushed, which is usually unnecessary and has a negative impact on performance.
  • You use int in some places to hold sizes, like the loop iterator i and as the value type of the vector index. If the text is larger than can be represented by an int, your code will no longer work correctly. Always prefer to use std::size_t for sizes, counts and indices.
  • You can avoid the atomic variable by changing the for_each() into a transform_reduce(). However, it probably doesn't improve performance in a significant way.
  • Use "\n" instead of std::endl; the latter is equivalent to the former, but also forces the output to be flushed, which is usually unnecessary and has a negative impact on performance.
  • You use int in some places to hold sizes, like the loop iterator i and as the value type of the vector index. If the text is larger than can be represented by an int, your code will no longer work correctly. Always prefer to use std::size_t for sizes, counts and indices.
  • Use "\n" instead of std::endl; the latter is equivalent to the former, but also forces the output to be flushed, which is usually unnecessary and has a negative impact on performance.
  • You use int in some places to hold sizes, like the loop iterator i and as the value type of the vector index. If the text is larger than can be represented by an int, your code will no longer work correctly. Always prefer to use std::size_t for sizes, counts and indices.
  • You can avoid the atomic variable by changing the for_each() into a transform_reduce(). However, it probably doesn't improve performance in a significant way.
added 682 characters in body
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180

Other issues

  • Use "\n" instead of std::endl; the latter is equivalent to the former, but also forces the output to be flushed, which is usually unnecessary and has a negative impact on performance.
  • You use int in some places to hold sizes, like the loop iterator i and as the value type of the vector index. If the text is larger than can be represented by an int, your code will no longer work correctly. Always prefer to use std::size_t for sizes, counts and indices.

Other issues

  • Use "\n" instead of std::endl; the latter is equivalent to the former, but also forces the output to be flushed, which is usually unnecessary and has a negative impact on performance.
  • You use int in some places to hold sizes, like the loop iterator i and as the value type of the vector index. If the text is larger than can be represented by an int, your code will no longer work correctly. Always prefer to use std::size_t for sizes, counts and indices.
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180
Loading