|
Threads-Devel : |
From: Michael Gopshtein (mgopshtein_at_[hidden])
Date: 2008-01-07 09:47:33
I agree that the concept of "thread storage" is more closely assosiated with
thread's private data. The situation where we use such iterator is the
following: assume that your data structure supports simultaneously 1 reader
and 1 writer with no locking, but does not support 2 or more writers. The
most trivial example: int counter (say, for statistics). You can increment
it in one thread, and read the value in the other, but you can't increment
it in 2 threads without a lock.
So continuing this example, assume that each thread maintains a counter, and
you want to get a sum of all counters for all threads - in this case such
utility would be useful.
Adding external map of such local objects is possible, but this introduces
additional level of complexity, as you have to make sure to remove those
objects when the thread exits.
"Anthony Williams" <anthony_at_[hidden]> wrote in message
news:20080107121030.41cfg50o68cgck4w_at_vilya.visn.co.uk...
> Quoting Michael Gopshtein <mgopshtein_at_[hidden]>:
> ...
> 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
>
>