Sign in to follow this  
beebs1

Design Difficulties

Recommended Posts

Hiya,

I have a number of worker threads, all taking jobs from a queue. The jobs use several objects which must access a structure from a non thread-safe third-party library - so each thread ideally needs its own structure. Could anyone suggest a way of giving the objects access to the shared structure?

The structure is actually the state of a RNG, so I'd rather not initialise a new one in each class where it's needed as its expensive. I'm tempted to use a singleton with Boost thread-local storage, something like this:

[code]
class RandomGenerator
{
public:

static RandomGenerator* instance()
{
boost::thread_specific_ptr<RandomGenerator> threadInstance;
if(threadInstance.get() == 0)
threadInstance.reset(new RandomGenerator());

return threadInstance;
}

/* Return the actual structure. */
gmp_randstate_t& getState() const
{
return m_state;
}

private:

RandomGenerator() { /* etc. */ }

mutable gmp_randstate_t m_state;
};
[/code]

It seems messy and I'd rather not use a singleton if possible.

Any suggestions are appreciated!

Thanks.

Share this post


Link to post
Share on other sites
A simple static (or module scoped instance) with thread-local storage is pretty common. Though honestly, I can't think of any RNG implementation that is particularly heavyweight...

Share this post


Link to post
Share on other sites
[quote name='beebs1' timestamp='1318454631' post='4871990']
Hiya,

I have a number of worker threads, all taking jobs from a queue. The jobs use several objects which must access a structure from a non thread-safe third-party library - so each thread ideally needs its own structure.[/quote]

Thread-safety is a vaguely defined and often misused term.

Having one copy per thread does not make it thread safe. At least not in absence of having the source and making sure that per-thread isolation does indeed provide sufficient isolation.

Consider a simple example of a logger. Each thread could have its own instance, yet they would all be writing to same file.

[quote]Could anyone suggest a way of giving the objects access to the shared structure?[/quote]

A problem like this is solved via a queue. All threads serialize requests into a single queue, which is then handled by one single thread.

[quote]I'm tempted to use a singleton with Boost thread-local storage, something like this:[/quote]
Singletons and concurrency don't mix. If implemented properly, they block, so that only a single thread may access them at same time. Otherwise, they are no longer a singleton, since multiple copies exist.

All the previously mentioned issues exist. If source isn't available, there is no guarantee that multiple copies will behave properly. A true RNG with external data source would still be polling same resource, despite multiple copies existing inside an application.

That said, for PRNG, just about every useful implementation provides ability to specify own seed. If it doesn't, ditch it and use a library that does. All good PRNGs are available in suitable form in many forms and different licenses.

And even if it takes 30 seconds to initialize, it makes no difference. If done on single thread, it will take 30 seconds, if done on n threads it will still take 30 seconds, just each thread initializing on its own. For user it doesn't matter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this