Re: creating thread in C++



Hi,

Sergei Organov schrieb:
void myclass::mythreadstub(void* arg)
{ ((myclass*)arg)->mythread();
}

...
myclass* m = new myclass();
_beginthread(&myclass::threadstub, NULL, 0, m);

Strictly speaking, this is not guaranteed to work as the function
pointer that you pass to pthread library should be declared 'extern "C"'
in C++, and you can't declare static member this way. I.e., static
member function could be incompatible with a function that pthread
library expects.

uh, I remember there have been something like this. IBM VCC++ 3.0 for OS/2 for instance uses a different calling convention for class members which cannot be changed.

The work around is to use a friend instead of a static class member function. If the entry point is public (or with some tricks protected) a statically linked function in the implementation file where _beginthread is called is sufficient. But I am unsure if a static function declaration within an extern "C" block is guaranteed.


Marcel
.



Relevant Pages

  • Re: const and extern...
    ... extern float bar; ... a declaration), but if you merely declare it, you do not define it. ... or the translation unit ends. ... an actual definition with an initializer of: ...
    (comp.lang.c)
  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: How do header files work?
    ... declare some constants, variables, and functions that have ... for is the "extern" linkage keyword (which nobody has mentioned ... following source files in my directory: ... That's because the compiler for your library.c file doesn't need ...
    (comp.lang.c)
  • Re: Exported function mangaled name
    ... extern "C" { ... You have otherwise defined a function unrelated to the header ... You have to declare the function the same way in your .cpp file as in your .h file ... #define LIBSPEC __declspec ...
    (microsoft.public.vc.mfc)
  • Re: keyword extern
    ... > declared multiple times using extern in all the files file2.c ... NEVER, ever, define a variable in a header file. ... declare it with the "extern" qualifier in the header file for the ... misuse. ...
    (comp.lang.c)

Loading