Does one need to call srand() C function per thread or per process to seed the randomizer?

后端 未结 4 1586
借酒劲吻你
借酒劲吻你 2021-01-11 16:46

The caption pretty much says it.

PS. This is for C++ Windows program.

4条回答
  •  长发绾君心
    2021-01-11 16:48

    No, as per the Standard, because once you call srand() it affects rand() calls from all threads.

    C library function srand() is single-threaded just like all other functions are, which means if you call srand() from one thread, it affects the number-seqeunce generated from rand() in other threads as well.

    But Microsoft provides non-standard srand() which does require you to call it from all threads.

提交回复
热议问题