multithreading and member-functions

Discussion in 'C++' started by Ilia Poliakov, Sep 3, 2003.

  1. Can I run a thread with _beginthread() and pass as the first parameter the
    pointer on a member function of a class?

    #include <process.h>

    class A
    {
    void _cdecl f(void*) {};
    };

    void main()
    {
    _beginthread(&A::f, 0, NULL);
    }

    I get the compilation error:
    error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
    (__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich

    PS. I work in MSVC7.0 (german version :), console application
     
    Ilia Poliakov, Sep 3, 2003
    #1
    1. Advertising

  2. "Ilia Poliakov" <> wrote in message
    news:bj4gs5$f95q9$-berlin.de...
    > Can I run a thread with _beginthread() and pass as the first parameter the
    > pointer on a member function of a class?
    >
    > #include <process.h>
    >
    > class A
    > {
    > void _cdecl f(void*) {};
    > };
    >
    > void main()
    > {
    > _beginthread(&A::f, 0, NULL);
    > }
    >
    > I get the compilation error:
    > error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
    > (__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich
    >
    > PS. I work in MSVC7.0 (german version :), console application


    1. It would be better if you ask this on a Windows NG.
    2. Please translate the errors to English whenever posting.
    Then may be someone here can help you.
    --
    With best wishes,
    J.Schafer
     
    Josephine Schafer, Sep 3, 2003
    #2
    1. Advertising

  3. Ilia Poliakov

    Attila Feher Guest

    Ilia Poliakov wrote:
    > Can I run a thread with _beginthread() and pass as the first
    > parameter the pointer on a member function of a class?


    Nope. AFAIK _beginthread is a C function, taking a void * argument.
    Pointers and pointers to members are very different beasts. A pointer to a
    member (function or variable) can have a very different implementation than
    a normal pointer. The standard (let me not look it up) clearly states that
    such conversion (from and to) is not supported. IMHO the best you can do is
    to make a little class containing that pointer to member, create it (either
    static/global, dynamicly allocated or some other way that ensures that it is
    still there when the new thread tries to access it) and pass a pointer to
    this very "argument list" to the new thread. Of course if you need more
    arguments you can add them into this class (or struct).

    --
    Attila aka WW
     
    Attila Feher, Sep 3, 2003
    #3

  4. > 1. It would be better if you ask this on a Windows NG.


    _beginthread() is a C run-time function and has no relationship to any OS.

    > 2. Please translate the errors to English whenever posting.
    > Then may be someone here can help you.


    error C2664: '_beginthread' : convertion of parameter 1 from 'void (__cdecl
    A::* )(void *)' in 'void (__cdecl *)(void *)' not possible
     
    Ilia Poliakov, Sep 3, 2003
    #4
  5. Ilia Poliakov

    Attila Feher Guest

    Josephine Schafer wrote:
    [SNIP]
    > 1. It would be better if you ask this on a Windows NG.


    The problem has nothing to do with Windows, all you need to know is that
    thread starting function take a void * argument to represent "arguments" for
    the thread. That is clearly visible from the error message - even though it
    is in German.

    > 2. Please translate the errors to English whenever posting.
    > Then may be someone here can help you.


    I speak no German but Konvertierung is definitely Conversion. What it wants
    to convert is void (A::*)(void *) (pointer to a member function of a class A
    taking a void * argument and returns nothing) to void (*)(void *): pointer
    to a function bla bla. Now it is clear that *if* this conversion is
    possible you get no error message. It is also clear from the rules of C++
    that this conversion is not possible So even if you did not know that
    "nicht möglich" is not possible you could deduce what is the problem.

    IMHO if you do not take the time to answer a post... well, just do not take
    the time to answer it. No offense meant.

    --
    Attila aka WW
     
    Attila Feher, Sep 3, 2003
    #5
  6. Ilia Poliakov

    Attila Feher Guest

    Ilia Poliakov wrote:
    >> 1. It would be better if you ask this on a Windows NG.

    >
    > _beginthread() is a C run-time function and has no relationship to
    > any OS.


    _beginthread is *not* a C runtime function. Please refer to the ISO
    standard of the C language! Or to this place and look for the C runtime
    library documentation: http://www.dinkumware.com

    --
    Attila aka WW
     
    Attila Feher, Sep 3, 2003
    #6
  7. "Ilia Poliakov" <> wrote in message
    news:bj4hef$fd0iq$-berlin.de...
    >
    > > 1. It would be better if you ask this on a Windows NG.

    >
    > _beginthread() is a C run-time function and has no relationship to any OS.


    Well then it's a C++ NG.

    > > 2. Please translate the errors to English whenever posting.
    > > Then may be someone here can help you.

    >
    > error C2664: '_beginthread' : convertion of parameter 1 from 'void

    (__cdecl
    > A::* )(void *)' in 'void (__cdecl *)(void *)' not possible


    Your error message gives the answer.
    You cannot convert a pointer to a member function to a normal pointer
    to a function.
     
    Josephine Schafer, Sep 3, 2003
    #7
  8. "Ilia Poliakov" <> wrote in message
    news:bj4hef$fd0iq$-berlin.de...
    >
    > > 1. It would be better if you ask this on a Windows NG.

    >
    > _beginthread() is a C run-time function and has no relationship to any OS.
    >


    That is not true. _beginthread is non-standard and specific to Microsoft
    Windows.
     
    Simon Saunders, Sep 3, 2003
    #8
  9. "Attila Feher" <> wrote in message
    news:bj4hkh$ro7$...
    > Josephine Schafer wrote:
    > [SNIP]
    > > 1. It would be better if you ask this on a Windows NG.

    >
    > The problem has nothing to do with Windows, all you need to know is that
    > thread starting function take a void * argument to represent "arguments"

    for
    > the thread. That is clearly visible from the error message - even though

    it
    > is in German.
    >
    > > 2. Please translate the errors to English whenever posting.
    > > Then may be someone here can help you.

    >
    > I speak no German but Konvertierung is definitely Conversion. What it

    wants
    > to convert is void (A::*)(void *) (pointer to a member function of a class

    A
    > taking a void * argument and returns nothing) to void (*)(void *): pointer
    > to a function bla bla. Now it is clear that *if* this conversion is
    > possible you get no error message. It is also clear from the rules of C++
    > that this conversion is not possible So even if you did not know that
    > "nicht möglich" is not possible you could deduce what is the problem.
    >
    > IMHO if you do not take the time to answer a post... well, just do not

    take
    > the time to answer it. No offense meant.


    OK may be I just looked at _beginthread() and started taking it OT.
    Neither do I understand German nor did I made make much attempts to
    understand it.
    But when the OP translated the error message, I answered it and yes it had
    nothing to do with
    Windows or anything as you said.

    Hope you too understand.
    J.Schafer
     
    Josephine Schafer, Sep 3, 2003
    #9
  10. Ilia Poliakov

    Attila Feher Guest

    Josephine Schafer wrote:
    > "Ilia Poliakov" <> wrote in message
    > news:bj4hef$fd0iq$-berlin.de...
    >>
    >>> 1. It would be better if you ask this on a Windows NG.

    >>
    >> _beginthread() is a C run-time function and has no relationship to
    >> any OS.

    >
    > Well then it's a C++ NG.


    The C libray is part of C++. But of course the _beginthread function is not
    part of either. :)

    > Your error message gives the answer.
    > You cannot convert a pointer to a member function to a normal pointer
    > to a function.


    I guess why the OP posted is to know what he _can_ do, not only to know that
    he cannot do it as he tried. ;-)

    --
    Attila aka WW
     
    Attila Feher, Sep 3, 2003
    #10
  11. Ilia Poliakov wrote:
    >
    > Can I run a thread with _beginthread() and ...


    Stay away from _beginthread() silliness ("ex"-stuff including).

    As for the "rest",

    http://groups.google.com/groups?selm=
    (Subject: Re: C++ and threads: Messy code?)

    regards,
    alexander.

    P.S. Stay away from the current Boost.Thread as well.
     
    Alexander Terekhov, Sep 3, 2003
    #11
  12. "Ilia Poliakov" <> wrote in message news:<bj4gs5$f95q9$-berlin.de>...
    > Can I run a thread with _beginthread() and pass as the first parameter the
    > pointer on a member function of a class?
    >
    > #include <process.h>
    >
    > class A
    > {
    > void _cdecl f(void*) {};
    > };
    >
    > void main()
    > {
    > _beginthread(&A::f, 0, NULL);
    > }
    >
    > I get the compilation error:
    > error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
    > (__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich
    >
    > PS. I work in MSVC7.0 (german version :), console application


    As has been pointed out by others, a pointer to a member function is
    not the same as a pointer to a function.

    So, what you want is a function that is compatible with begin thread
    and will forward your function correctly.


    extern "C" void _cdecl forward_function(void *arg)
    {
    A *a = reinterpret_cast<A *>(arg);

    arg->f();
    }

    samuel
    }
     
    Ron Samuel Klatchko, Sep 3, 2003
    #12
  13. Ilia Poliakov

    Jack Klein Guest

    On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
    <> wrote in comp.lang.c++:

    > Ilia Poliakov wrote:
    > > Can I run a thread with _beginthread() and pass as the first
    > > parameter the pointer on a member function of a class?

    >
    > Nope. AFAIK _beginthread is a C function, taking a void * argument.


    Nope, _beginthread is not a C function. It is not defined anywhere in
    the C standard. Nor am I aware of any Microsoft documentation that
    states that it is written in (non-standard) C.

    What you could say is that _beginthread is a function that may be
    called by C.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 4, 2003
    #13
  14. "Ilia Poliakov" <> wrote in message news:<bj4gs5$f95q9$-berlin.de>...
    > Can I run a thread with _beginthread() and pass as the first parameter the
    > pointer on a member function of a class?
    >
    > #include <process.h>
    >
    > class A
    > {
    > void _cdecl f(void*) {};
    > };
    >
    > void main()
    > {
    > _beginthread(&A::f, 0, NULL);
    > }
    >
    > I get the compilation error:
    > error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
    > (__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich
    >
    > PS. I work in MSVC7.0 (german version :), console application


    You can use the static member function

    I got it compiled with the following code with proper header included

    class proc
    {
    public:
    static void SecondThreadFunc( void* pArguments )
    {
    printf("In thread");
    }
    };

    void main()
    {
    _beginthread(proc::SecondThreadFunc, 0, NULL );
    Sleep(1000);
    }

    Hope that helps
     
    Hafiz Abid Qadeer, Sep 4, 2003
    #14
  15. Ilia Poliakov

    Attila Feher Guest

    Jack Klein wrote:
    > On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
    > <> wrote in comp.lang.c++:
    >
    >> Ilia Poliakov wrote:
    >>> Can I run a thread with _beginthread() and pass as the first
    >>> parameter the pointer on a member function of a class?

    >>
    >> Nope. AFAIK _beginthread is a C function, taking a void * argument.

    >
    > Nope, _beginthread is not a C function. It is not defined anywhere in
    > the C standard. Nor am I aware of any Microsoft documentation that
    > states that it is written in (non-standard) C.


    It is a C function. Repeat it to yourself until you understand: it *is* a C
    function. And then if you still not understand say that: on the other hand
    strcpy is a *standard C library* function.

    > What you could say is that _beginthread is a function that may be
    > called by C.


    Which we call a C function as opposed to C++, Fortran, Fifthrun etc.
    functions. If you did care to read my other posts you would have seen that
    I did post (to the OP) that _beginthread is not a standard C library
    function.

    --
    Attila aka WW
     
    Attila Feher, Sep 5, 2003
    #15
  16. Ilia Poliakov

    Attila Feher Guest

    Ron Samuel Klatchko wrote:
    > So, what you want is a function that is compatible with begin thread
    > and will forward your function correctly.


    And if you want that (forward correctly) you do nto want to use the posted
    code.

    > extern "C" void _cdecl forward_function(void *arg)
    > {
    > A *a = reinterpret_cast<A *>(arg);


    Reinterpret case is _implementation_ defined. In *all* cases. If you want
    to get back your original A pointer here you *must* use static_cast (or a C
    style cast).

    --
    Attila aka WW
     
    Attila Feher, Sep 5, 2003
    #16
  17. Ilia Poliakov

    Attila Feher Guest

    Attila Feher wrote:
    > Reinterpret case is


    Of course I meant reinterpret cast

    --
    Attila aka WW
     
    Attila Feher, Sep 5, 2003
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hamish
    Replies:
    3
    Views:
    583
    Alf P. Steinbach
    Jan 25, 2008
  2. mafiQ
    Replies:
    0
    Views:
    476
    mafiQ
    Apr 22, 2009
  3. Hicham Mouline
    Replies:
    0
    Views:
    436
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
  5. Stephen Howe
    Replies:
    2
    Views:
    292
    Stephen Howe
    Nov 6, 2012
Loading...

Share This Page