This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: cyg_thread_create()


>>>>> "Fernando" == Fernando Herrera <fherrera@teisa.unican.es> writes:

    Fernando> Hi!
    Fernando>    I'd wanted to call cyg_thread_create() funcion passing to it
    Fernando> a function member (that is, a method) of a determined class
    Fernando> object, as the 2nd argument. For instance, something like this:

    Fernando> (class declaration)

    Fernando> class myclass {
    Fernando> public:
    Fernando>    int i;
    Fernando>    int func();
    Fernando> }

    Fernando> (object declaration)
    Fernando> myclass myobject;

    Fernando> (... and this is what I want to do:)
    Fernando> cyg_thread_create(..., myobject.func,...)

    Fernando> BUT I FOUND A COMPILING TIME ERROR, because it seems
    Fernando> that cyg_thread_create() necessarily asks a function
    Fernando> pointer, instead of a function member pointer.

    Fernando> I have tried also making and explicit conversion but it
    Fernando> doesn't functions, I'm not very sure, but it seems it's
    Fernando> not posible to convert a method pointer to a function
    Fernando> pointer, at least with the compiler I'm using (g++
    Fernando> 2.95.2).

    Fernando>   So it seems that all the process functionalities must
    Fernando> be defined as functions in eCos. Is this true or there
    Fernando> is another alternative? Can't be this solved somehow?

    Fernando> (I'm new in the forum, so I don't now if this is a
    Fernando> trivial question or is simply a C++ question, (sorry in
    Fernando> advance in that case)), anyway you can tell me about.

What you see is the expected behaviour.

An C++ normal member function is different from an ordinary function
or a static member function: there is an implicit "this" argument.
Hence for any part of the system such as the thread code to invoke a
member function it needs to know two values, the function pointer and
the "this" pointer. The function argument to cyg_thread_create()
can only hold the first of these. The compiler is warning you that
what you are attempting is impossible.

One way around this is to have a utility static member function and
pass the "this" pointer as the entry_data argument to
cyg_thread_create, something along the following lines (untested):

    class myclass {
        ...
        int member_func(void);
        static void call_member(cyg_addrword_t);
    }

   myclass myobject;

   void
   myclass::call_member(cyg_addrword_t objptr)
   {
       myclass* obj = static_cast<myclass*>(objptr);
       obj->member_func();
   }


   int
   main(int argc, char** argv)
   {
       cyg_thread_create( ..., &myclass::call_member, (cyg_addrword_t) &myobject, ...)
   }


The kernel cannot do anything like this for you automatically because
when it is compiled it has no knowledge of myclass or how to invoke
its member functions.

Bart


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]