Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2008-01-07 07:10:30


Quoting Michael Gopshtein <mgopshtein_at_[hidden]>:

> When working with "tread local storage" I find it sometimes very useful to
> get an access to instances of the class of all threads from single location.
> For example, when each thread stores some info in its own object, and you
> want to be able to collect overall data.
>
> So I think it would be helpful to add some form of iterator over the
> instances. Do you find such functionality valuable?

I think this would be a dangerous facility to provide. Firstly, the
nature of thread specific data means that it is not in general
possible to access the data for one thread from another thread: this
would require that each thread store a pointer to its thread-local
data in a global structure.

Secondly, *because* thread-local data is thread-local, it is generally
accessed without synchronization, so any such "iterator" would lead to
the potential for race conditions.

Why not just create a global map for the data, indexed by thread::id?

std::map<boost::thread::id,some_data> the_data;

Each thread can then access its own data with
the_data[boost::this_thread::get_id()], assuming appropriate
synchronization.

Anthony

-- 
Anthony Williams
Just Software Solutions Ltd - http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk