?
Solved

Linux threads and passing arguments

Posted on 2005-03-05
7
Medium Priority
?
258 Views
Last Modified: 2008-02-01
Hello,

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?
0
Comment
Question by:beside
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 

Author Comment

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

Expert Comment

by:itsmeandnobodyelse
ID: 13485892
Try

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

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

FYI:
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

0
 

Author Comment

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

Accepted Solution

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

modulo
Community Support Moderator
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
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
 {
 private:
         pthread_t m_thread;
 public:
         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
       
       pThis->doWhatMustBeDone();

       return NULL;
   }

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

Regards, Alex
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

762 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