Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2003-07-01 08:15:11


On Monday, June 30, 2003, at 06:04 PM, Philippe A. Bouchard wrote:

> Suppose you have:
>
> struct functor1
> {
> list<void *> m_list;
>
> void operator () ()
> {
> ...
> }
> };
>
> struct functor2
> {
> list<void *> m_list;
>
> void operator () ()
> {
> ...
> }
> };
>
> int main()
> {
> thread f1(functor1());
> thread f2(functor2());
>
> ...
>
> // I would like to access m_list of the current thread's
> functor:
>
> lock()...
> if (thread() == f1 || thread() == f2)
> {
> thread()..(whatever casts)...m_list;
> }
> unlock()...
>
> // I think the only way to do this is by mapping the thread's
> id
> // with the object's address (map<key, functor1 *>) but there
> is
> // no standard key publicly accessible and map<> creates
> overhead.
> }

The tss facility *is* a map from the thread's id to any data you want.
Perhaps your example could look like:

thread_specific_ptr<list<void*> > m_list;

void foo()
{
     // no lock necessary!
     // uses thread()'s private copy of list<void*>
     m_list->push_back(0);
     // ...
}

struct functor1
{
     void operator()()
     {
         m_list.reset(new list<void*>);
         // ...
         foo();
     }
};

struct functor2
{
     void operator()()
     {
         m_list.reset(new list<void*>);
         // ...
         foo();
     }
};

int main()
{
         thread f1(functor1());
         thread f2(functor2());
         // ...
}

Or were you wanting one thread to be able to access another thread's
list<void*>? To be able to do that, I think you would have to create
your own map.

-Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk