Why is this C++11 code containing rand() slower with multiple threads than with one?

前端 未结 4 1871
栀梦
栀梦 2020-12-13 06:17

I\'m trying around on the new C++11 threads, but my simple test has abysmal multicore performance. As a simple example, this program adds up some squared random numbers.

4条回答
  •  醉酒成梦
    2020-12-13 06:55

    As you discovered, rand is the culprit here.

    For those who are curious, it's possible that this behavior comes from your implementation of rand using a mutex for thread safety.

    For example, eglibc defines rand in terms of __random, which is defined as:

    long int
    __random ()
    {
      int32_t retval;
    
      __libc_lock_lock (lock);
    
      (void) __random_r (&unsafe_state, &retval);
    
      __libc_lock_unlock (lock);
    
      return retval;
    }
    

    This kind of locking would force multiple threads to run serially, resulting in lower performance.

提交回复
热议问题