[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] Random number generators and Pthreads

From: Martin Jansche
Subject: Re: [Help-gsl] Random number generators and Pthreads
Date: Sun, 12 Nov 2006 17:43:19 -0500

On 11/6/06, Joseph Wakeling <address@hidden> wrote:

The second question is, what if I want to use Pthreads or something
similar (which I'm just trying to teach myself)---do I have to do
anything to restrict access to the rng?  The GSL documentation claims
that gsl is "thread-safe", but I don't know really what this means,
whether GSL will sort everything out with respect to different threads'
access of the rng, or whether I do have to take my own precautions.

For C libraries in general, and GSL in particular, "thread safe"
simply means that there are no static variables in the library code
that keep global state. If you look at e.g. strtok() from <string.h>,
that's not thread safe because the library keeps state. The
alternative design, which GSL follows quite strictly, is to leave it
to client code to allocate workspace for any functions that need to
keep state (think object-oriented programming in C, or strtok_r()).
It's then up to the client code to make sure any workspace gets
updated by one thread at a time. If you want to access a single random
number generator from multiple threads, every call to produce a random
number is a critical section, and you'd have to use your favorite
synchronization mechanism to ensure atomic access. The better (IMO)
alternative is to allocate one thread-local rng object per thread.
Since there is no need for the different rngs to talk to each other,
your code does not need to synchronize rng access, provided each
thread can only see its own thread-local rng.

--  mj

reply via email to

[Prev in Thread] Current Thread [Next in Thread]