Hello Victor,

Victor Haefner wrote:
> ..I am trying to use threads but it is driving me crazy..
> 
> In another post here I found an example how to use member function with 
> osgthreads..
> 
> ..it works fine if the function is in that class.. but if I try to give 
> a member function pointer nothig works anymore..

hm, I'm not sure I understand this, sorry. What do you mean by "function 
is in that class" ?
What are you trying to achieve ?

> so here is my code :
> 
> OSG_BEGIN_NAMESPACE;
> using namespace std;
> 
> template <typename T>
> class MyThreadWrapper
> {
>     public:
>         class MyThread : public Thread
>         {
[SNIP]
>         };
> };
> 
> template <typename T>
> MPThreadType MyThreadWrapper<T>::MyThread::_type("MyThread", "Thread", 
> (CreateThreadF) MyThread::create, NULL);
> //MPThreadType MyThreadWrapper::MyThread::_type("MyThread", "Thread", 
> (CreateThreadF) MyThread::create, NULL);

part of the problem with this approach is that you are generating a 
family of thread types (one for each value of T) and they would all be 
registered with type name "MyThread".

It seems that you want to be able to call a member function of an 
arbitrary type as the "thread function" (I usually think of it as the 
thread's equivalent of main).
If you do this:

class ThreadFuncPlaceholder
{
public:
     virtual void call(void) = 0;
};

template <class T>
class ThreadFuncHolder : public ThreadFuncPlaceholder
{
public:
     T *_obj;
     void (T::*_memFunc)(void);

    virtual void call(void)
    {
        _obj->*_memFunc();
    }
};

and a helper to create the ThreadFuncHolder<T>:

template <class T>
ThreadFuncPlaceholder *makeTFHolder(T *obj, void (T::*memFunc)(void))
{
     ThreadFuncHolder<T> *newHolder = new ThreadFuncHolder<T>;
     newHolder._obj     = obj;
     newHodler._memFunc = memFunc;

     return newHolder;
}

you can create a thread class that contains a ThreadFuncPlaceholder * 
and calls that from its workProc.
In a sense that is a very specialized form of what 
boost::function/boost::bind do. So storing a
boost::function<void (void)> in your thread class would be another 
alternative. To make a member function match that signature you use:

OSGPeriphery *osgPeriph = new OSGPeriphery();
boost::bind(&OSGPeriphery::update, osgPeriph);

Off the top of my head I can not say if your thread type implements all 
functions that are necessary, so if one of the above approaches does not 
help, let us know so we can take another look.

        Cheers,
                Carsten

------------------------------------------------------------------------------
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to