ref. to object functions from threads

hi all, is there a way to get a reference to the function object used by a boost::thread object?? when you create a new thread and feed it a function object it copies it and then starts a new thread of execution on this object.. well, I would like to send messages to the object in which the thread is running but I don't know how to do it with boos.threads... is there a way to tell boost:thread not to copy the function object but to use it directly?, or to ask the thread object to give a reference to the object? I am now doing a dirty hack which consists of the object putting itself (a pointer) in a static chained list during construction and remove itself in the destructor, this way I can call a static member function which returns this list to get a hold of the object. There is also a static mutex protecting this list to make sure no other objects are created / destructed while one of them is inserting or removing itself from the list. it is working, but I don't think this is a very "clean" approach.. does anyone have any ideas how it could be done better?? thank you. regards, luis.

--- In Boost-Users@y..., Luis De la Parra <lparrab@g...> wrote:
hi all, is there a way to get a reference to the function object used by a boost::thread object??
No, and it would be very difficult to do so, since the type is lost.
when you create a new thread and feed it a function object it copies it and then starts a new thread of execution on this object.. well, I would like to send messages to the object in which the thread is running but I don't know how to do it with boos.threads...
Only by wrapping it in an adapter. The boost::ref class is a very good candidate for this. Here's an example, and I'll add this to the next release: #include <boost/ref.hpp> #include <boost/thread/thread.hpp> #include <iostream> struct thread_func { thread_func() : val(0) { } void operator()() { val = 99; } int val; }; int main() { thread_func f; boost::thread thrd(boost::ref(f)); thrd.join(); std::cout << f.val << std::endl; // should be "99" } Bill Kempf

El Thu, 5 Sep 2002 23:34:39 +0200 Luis De la Parra <lparrab@gmx.net> escribió:
hi all, is there a way to get a reference to the function object used by a boost::thread object?? when you create a new thread and feed it a function object it copies it and then starts a new thread of execution on this object.. well, I would like to send messages to the object in which the thread is running but I don't know how to do it with boos.threads...
I use the pimpl idiom with a boost::shared_ptr<> doing the trick. It's not very clean, but here goes my example: --------------------------------------------------------- #include <boost/thread/thread.hpp> #include <boost/thread/xtime.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/recursive_mutex.hpp> #include <iostream> #include <boost/shared_ptr.hpp> class counter_thread { public: counter_thread(): _pimpl(new counter_thread_impl) {} void Print() { _pimpl->Print(); } void Incr() { _pimpl->Incr(); } void operator()() { _pimpl->Run(); } private: class counter_thread_impl { public: counter_thread_impl() : _counter(0) { } void Run() { for(unsigned i=0; i<10;++i) { Print(); Incr(); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; boost::thread::sleep(xt); } } void Print() { boost::recursive_mutex::scoped_lock block_now(mutex); std::cout << "Counter: " << _counter << std::endl; } void Incr() { boost::recursive_mutex::scoped_lock block_now(mutex); ++_counter; } private: unsigned _counter; boost::recursive_mutex mutex; }; boost::shared_ptr<counter_thread_impl> _pimpl; }; int main(int argc, char* argv[]) { counter_thread theCounter; boost::thread threadManager(theCounter); for(unsigned i=0; i<10;++i) { theCounter.Print(); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; boost::thread::sleep(xt); } threadManager.join(); } --------------------------------------------------------- Note that, in main(), theCounter.Print() prints the correct value. Hope it helps. -- Raúl Huertas Díaz Redes y Comunicaciones Ingeniero de Sistemas TCP Sistemas e Ingenieria Fernández Caro, 7, 3ª planta Tel.: +34 91 367 32 79 (Ext. 535) Fax: +34 91 407 71 39 rhuertas@tcpsi.es http://www.tcpsi.es
participants (3)
-
bill_kempf
-
Luis De la Parra
-
Raul Huertas Diaz