multithreading and member-functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilia Poliakov

    multithreading and member-functions

    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


  • Josephine Schafer

    #2
    Re: multithreading and member-functions


    "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
    news:bj4gs5$f95 q9$1@ID-120622.news.uni-berlin.de...[color=blue]
    > 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[/color]

    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


    Comment

    • Ilia Poliakov

      #3
      Re: multithreading and member-functions

      [color=blue]
      > 1. It would be better if you ask this on a Windows NG.[/color]

      _beginthread() is a C run-time function and has no relationship to any OS.
      [color=blue]
      > 2. Please translate the errors to English whenever posting.
      > Then may be someone here can help you.[/color]

      error C2664: '_beginthread' : convertion of parameter 1 from 'void (__cdecl
      A::* )(void *)' in 'void (__cdecl *)(void *)' not possible



      Comment

      • Attila Feher

        #4
        Re: multithreading and member-functions

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

        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


        Comment

        • Josephine Schafer

          #5
          Re: multithreading and member-functions


          "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
          news:bj4hef$fd0 iq$1@ID-120622.news.uni-berlin.de...[color=blue]
          >[color=green]
          > > 1. It would be better if you ask this on a Windows NG.[/color]
          >
          > _beginthread() is a C run-time function and has no relationship to any OS.[/color]

          Well then it's a C++ NG.
          [color=blue][color=green]
          > > 2. Please translate the errors to English whenever posting.
          > > Then may be someone here can help you.[/color]
          >
          > error C2664: '_beginthread' : convertion of parameter 1 from 'void[/color]
          (__cdecl[color=blue]
          > A::* )(void *)' in 'void (__cdecl *)(void *)' not possible[/color]

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


          Comment

          • Simon Saunders

            #6
            Re: multithreading and member-functions

            "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
            news:bj4hef$fd0 iq$1@ID-120622.news.uni-berlin.de...[color=blue]
            >[color=green]
            > > 1. It would be better if you ask this on a Windows NG.[/color]
            >
            > _beginthread() is a C run-time function and has no relationship to any OS.
            >[/color]

            That is not true. _beginthread is non-standard and specific to Microsoft
            Windows.


            Comment

            • Attila Feher

              #7
              Re: multithreading and member-functions

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

              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.
              [color=blue]
              > 2. Please translate the errors to English whenever posting.
              > Then may be someone here can help you.[/color]

              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


              Comment

              • Attila Feher

                #8
                Re: multithreading and member-functions

                Ilia Poliakov wrote:[color=blue][color=green]
                >> 1. It would be better if you ask this on a Windows NG.[/color]
                >
                > _beginthread() is a C run-time function and has no relationship to
                > any OS.[/color]

                _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


                Comment

                • Josephine Schafer

                  #9
                  Re: multithreading and member-functions


                  "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                  news:bj4hkh$ro7 $1@newstree.wis e.edt.ericsson. se...[color=blue]
                  > Josephine Schafer wrote:
                  > [SNIP][color=green]
                  > > 1. It would be better if you ask this on a Windows NG.[/color]
                  >
                  > 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"[/color]
                  for[color=blue]
                  > the thread. That is clearly visible from the error message - even though[/color]
                  it[color=blue]
                  > is in German.
                  >[color=green]
                  > > 2. Please translate the errors to English whenever posting.
                  > > Then may be someone here can help you.[/color]
                  >
                  > I speak no German but Konvertierung is definitely Conversion. What it[/color]
                  wants[color=blue]
                  > to convert is void (A::*)(void *) (pointer to a member function of a class[/color]
                  A[color=blue]
                  > 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[/color]
                  take[color=blue]
                  > the time to answer it. No offense meant.[/color]

                  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


                  Comment

                  • Attila Feher

                    #10
                    Re: multithreading and member-functions

                    Josephine Schafer wrote:[color=blue]
                    > "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
                    > news:bj4hef$fd0 iq$1@ID-120622.news.uni-berlin.de...[color=green]
                    >>[color=darkred]
                    >>> 1. It would be better if you ask this on a Windows NG.[/color]
                    >>
                    >> _beginthread() is a C run-time function and has no relationship to
                    >> any OS.[/color]
                    >
                    > Well then it's a C++ NG.[/color]

                    The C libray is part of C++. But of course the _beginthread function is not
                    part of either. :-)
                    [color=blue]
                    > Your error message gives the answer.
                    > You cannot convert a pointer to a member function to a normal pointer
                    > to a function.[/color]

                    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


                    Comment

                    • Alexander Terekhov

                      #11
                      Re: multithreading and member-functions


                      Ilia Poliakov wrote:[color=blue]
                      >
                      > Can I run a thread with _beginthread() and ...[/color]

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

                      As for the "rest",


                      (Subject: Re: C++ and threads: Messy code?)

                      regards,
                      alexander.

                      P.S. Stay away from the current Boost.Thread as well.

                      Comment

                      • Ron Samuel Klatchko

                        #12
                        Re: multithreading and member-functions

                        "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message news:<bj4gs5$f9 5q9$1@ID-120622.news.uni-berlin.de>...[color=blue]
                        > 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[/color]

                        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_functio n(void *arg)
                        {
                        A *a = reinterpret_cas t<A *>(arg);

                        arg->f();
                        }

                        samuel
                        }

                        Comment

                        • Jack Klein

                          #13
                          Re: multithreading and member-functions

                          On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
                          <attila.feher@l mf.ericsson.se> wrote in comp.lang.c++:
                          [color=blue]
                          > Ilia Poliakov wrote:[color=green]
                          > > Can I run a thread with _beginthread() and pass as the first
                          > > parameter the pointer on a member function of a class?[/color]
                          >
                          > Nope. AFAIK _beginthread is a C function, taking a void * argument.[/color]

                          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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                          Comment

                          • Hafiz Abid Qadeer

                            #14
                            Re: multithreading and member-functions

                            "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message news:<bj4gs5$f9 5q9$1@ID-120622.news.uni-berlin.de>...[color=blue]
                            > 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[/color]

                            You can use the static member function

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

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

                            void main()
                            {
                            _beginthread(pr oc::SecondThrea dFunc, 0, NULL );
                            Sleep(1000);
                            }

                            Hope that helps

                            Comment

                            • Attila Feher

                              #15
                              Re: multithreading and member-functions

                              Jack Klein wrote:[color=blue]
                              > On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
                              > <attila.feher@l mf.ericsson.se> wrote in comp.lang.c++:
                              >[color=green]
                              >> Ilia Poliakov wrote:[color=darkred]
                              >>> Can I run a thread with _beginthread() and pass as the first
                              >>> parameter the pointer on a member function of a class?[/color]
                              >>
                              >> Nope. AFAIK _beginthread is a C function, taking a void * argument.[/color]
                              >
                              > 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.[/color]

                              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.
                              [color=blue]
                              > What you could say is that _beginthread is a function that may be
                              > called by C.[/color]

                              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


                              Comment

                              Working...