Linux threads and passing arguments

Posted on 2005-03-05
Medium Priority
Last Modified: 2008-02-01

I have problem here with KDevelop. I have program written in C with pthread library, when I complite it with C compiliator everything goes O.K. but now I'm trying to convert that to KDevelop project using C++ I get following errors:

xxx.cpp:129: error: argument of type `void (XXX::)(void*)' does not match `void*'
xxx.cpp:173: error: invalid conversion from ` void*' to `LINK_INFO*'

the code (launching new thread) here is the first error:
pthread_create (&tid_poll, NULL, (void*)rx_message, link_info);

the code (thread function) and here is the second error:
void XXX::rx_message(void *pv) {
    LINK_INFO *pli = pv;

How could I write that correctly?
Question by:beside
  • 2
  • 2

Author Comment

ID: 13465531
solved the problem according to http://www.csd.uwo.ca/courses/CS402b/Assign/faq.c++
LVL 39

Expert Comment

ID: 13485892

   pthread_create (&tid_poll, NULL, XXX::rx_message, link_info);

However, XXX::rx_message must be a static member of class XXX.

XXX::rx_message is of type void. So, a cast to void* neither applies to the required argument type nor to the type of function rx_message.

Regards, Alex


Author Comment

ID: 14100531
I posted a question, and answered it myself. So please do points refund.

Accepted Solution

modulo earned 0 total points
ID: 14123632
PAQed with points refunded (125)

Community Support Moderator
LVL 39

Expert Comment

ID: 14124509
>>>> solved the problem according to http://www.csd.uwo.ca/courses/CS402b/Assign/faq.c++

I found time to look at the link above.

The answer given there was that C++ member functions couldn't/shouldn't be used as start functions in pthreads. The main argument was that pthread API is C and C++ and C have a different linkage on most compilers, especially on g++;

I think the answer is wrong if the dll/program is a C++ program (what seems to be the case here as the file to compile has cpp extension). If a different C linkage of a library would be any problem, you couldn't use C runtime dlls, but you can - with any C++ compiler. The only precondition is that C function prototypes have an "extern C" directive in the header files, thus were not subject of C++ name mangling.
You can use a static class member function as startup function of a pthread if it has the needed prototype:

// xxx.h

 class XXX
         pthread_t m_thread;
         static void* threadStart(void* pParam);   // startup function for thread
         // non-static member function to invoke thread
         bool createThread();
         // non-static member function to do the thread job
         void doWhatMustBeDone();    

// in  xxx.cpp

  bool XXX::createThread()
    // start the thread and pass static function + 'this' as param  
    int rc = pthread_create( &m_thread, NULL, XXX:: threadStart, this);  
    return rc == 0;

   void* XXX::threadStart(void* pParam)
       XXX* pThis = (XXX*)pParam;          // get 'this' pointer in static function

       return NULL;

I have successfully used that construct with pthreads and Windows threads (_beginthread).

Regards, Alex

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question