1

Topic: Boost.random And thread safety

As it is known, boost:: random is not thread safe. Then if globally is boost:: random:: mt19937 gen (42); And from different flows simultaneously very often to cause uint32_t randVal = gen (); that that can happen? I see 2 dangers: 1) For simultaneous calls identical values will be always generated; 2) Happens undefined behavior and a call either "hangs up", or falls out with segmentation fault. My task - with the maximum productivity to receive pseudo-random numbers from gen.min () to gen.max (), quality of numbers not important. Therefore without are necessary to close  it would not be desirable.

2

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> My task - with the maximum productivity to receive pseudo-random numbers from gen.min () to gen.max (), quality of numbers not important. Therefore without are necessary to close  it would not be desirable. What hinders simply to have in each flow the generator? Simply add key a word thread_local: thread_local boost:: random:: mt19937 gen; Well and at will initialize these generators not a constant, and a combination from time and flow number.

3

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> My task - with the maximum productivity to receive pseudo-random numbers from gen.min () to gen.max () As fast as possible - to have in each flow the generator.

4

Re: Boost.random And thread safety

Hello, watchmaker, you wrote: W> Simply add key a word thread_local: It still what for? Simply to create new object not so fashionably?

5

Re: Boost.random And thread safety

Hello, MTD, you wrote: MTD> As fast as possible - to have in each flow the generator. In each flow the generator far it is not obvious how to make. Flows are boost:: thread from asynchronous http servers boost:: asio. The generator for everyone Connection it is easy, but no sense in this case has (a great number of connections, including short-lived).

6

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> In each flow the generator far it is not obvious how to make. And in what complexity? You write approximately so: auto gen = RandomGenerator (); Also you use. P> flows are boost:: thread from asynchronous http servers boost:: asio. To what then talks about as fast as possible? Do as you want, the generator of random numbers last that should be optimized. P> the generator for everyone Connection it is easy, but no sense in this case has (a great number of connections, including short-lived). Also what? If they short-lived, any   all yours of optimization. And for keep-alive connections the generator of random numbers last that should be optimized. You understand, the synch point, even implicit in a type, for example, shared_ptr transferred on value leads to that your flows and processor kernels start to stand idle. Before synchronizing flows it is necessary to think 2 times, and whether it is necessary?

7

Re: Boost.random And thread safety

Hello, MTD, you wrote: MTD> Hello, watchmaker, you wrote: W>> Simply add key a word thread_local: MTD> It still what for? Simply to create new object not so fashionably? To add one keyword is much easier. But if you want to control independently these objects, that, certainly, you can do and so. Simply, judging by the answer of the author in an adjacent branch the Author: prrt Date: 16.01 13:42, it nevertheless hardly more difficult action.

8

Re: Boost.random And thread safety

Hello, watchmaker, you wrote: W> Simply add key a word thread_local: W> thread_local boost:: random:: mt19937 gen; Yes, it, seemingly, that is necessary. Only here the compiler old, thread_local does not support.  with boost:: thread_specific_ptr, did not quit, or errors at compilation, or at a performance stage. As a result made while so: Globally std:: map <boost:: thread:: id, boost:: random:: mt19937> gens; At creation of flows: for (std:: size_t i = 0; i <thread_pool_size _; ++ i) {boost:: shared_ptr <boost:: thread> thread (new boost:: thread (boost:: bind (&boost::asio::io_service::run, io_service_ptr))); gens [thread-> get_id ()] = boost:: random:: mt19937 (i);} And generation of a random number in output agent Asio: uint rand = gens [boost:: this_thread:: get_id ()] ();

9

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> As a result made while so: P> it is global P> P> std:: map <boost:: thread:: id, boost:: random:: mt19937> gens; P> P> At creation of flows: P> P> for (std:: size_t i = 0; i <thread_pool_size _; ++ i) P> {P> boost:: shared_ptr <boost:: thread> thread (new boost:: thread (P> boost:: bind (&boost::asio::io_service::run, io_service_ptr))); P> gens [thread-> get_id ()] = boost:: random:: mt19937 (i); P>} P> P> And generation of a random number in output agent Asio: P> P> uint rand = gens [boost:: this_thread:: get_id ()] (); P> If quality of numbers not important, but thread safety is necessary, whether to use rand_s under Windows or rand_r under UNIX is easier?

10

Re: Boost.random And thread safety

Hello, AleksandrN, you wrote: AN> If quality of numbers not important, but thread safety is necessary, whether to use rand_s under Windows or rand_r under UNIX is easier? Judging by the answer to this question - https://stackoverflow.com/questions/294 … ms-in-c-c, for rand_r (at me Linux) it is necessary to do seed of each flow. I.e. all turns out a similar kitchen garden...

11

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> Hello, AleksandrN, you wrote: P> Judging by the answer to this question - https://stackoverflow.com/questions/294 … ms-in-c-c, for rand_r (at me Linux) it is necessary to do seed of each flow. I.e. all turns out a similar kitchen garden... int myrnd () {unsigned int init; init ^ = pthread_self () ^ time (NULL); return rand_r (&init);}

12

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> Judging by the answer to this question - https://stackoverflow.com/questions/294 … ms-in-c-c, for rand_r (at me Linux) it is necessary to do seed of each flow. I.e. all turns out a similar kitchen garden... rand_r quality of numbers not important is healthy faster a twister of Mersenna, if you.

13

Re: Boost.random And thread safety

Hello, prrt, you wrote: P> My task - with the maximum productivity to receive pseudo-random numbers from gen.min () to gen.max (), quality of numbers not important. Therefore without are necessary to close  it would not be desirable. You do a pool of ten generators.  each generator you initialize its number in a pool. Each generator in a pool you protect . In threads you use the generator at number thread_id % 10. So the probability of collisions will be . PS 10 it is taken for an example and can be any.