Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2003-06-30 17:04:48


William E. Kempf wrote:

>
> Philippe A. Bouchard said:
>> William E. Kempf wrote:
>>
>> [...]
>>
>>> As already pointed out, to associate data with a thread you use
>>> thread_specific_ptr. BTW, you still have to remember that the
>>> functor is copied, and data passed to/in the functor is not
>>> considered part of the thread in any event.
>>
>> Ok, how do you find out the data of the current thread? The key in
>> boost::detail::tss is not the same as the one in boost::thread.
>
> What data? The actual thread data (there's not much, beyond the thread id
> which isn't direclty accessible) is found by calling the default c-tor on
> the thread. The data passed in the functor is your responsibility.
> Either you explicitly pass it everywhere that's needed, or the thread
> stores it in a tss key.
>

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.
}

Regards,

-- 
Philippe A. Bouchard

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