views:

647

answers:

3

How to implement posix threads in linux c++.The smme program when saved as ".c and run using c compiler is ok. but in c++ it is giving error ..

I think i made mistake when compiling is there any tag to be included like "-lpthread" for c++

Can someone pls send a valid code...?

Actually this is my code

int cooperbussman :: startlistenthread()
{
        if(pthread_create(&m_thread,0,&packetreadertask,0)<0)
        {
                cout<<"Unable to create the thread Startlistenthread\n";
                return -1;
        }
        return 1;

and the error i am getting is

cooperbussman.cpp: In member function âint cooperbussman::startlistenthread()â:
cooperbussman.cpp:76: error: invalid conversion from âvoid* (*)()â to âvoid* (*)(void*)â
cooperbussman.cpp:76: error:   initializing argument 3 of âint pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)â
+3  A: 

You might look into using Boost.Threads. It gives you some simple semantics in C++ over pthreads on platforms that support it.

But....there's no reason why you can't use pthreads in a C++ program. Your errors may be due to symbol mangling, but there's no way for us to help you more precisely without seeing your code or at least your compiler output.

Ben Collins
+1  A: 

You use -lpthreads when using g++ just as you would with gcc. As long as you are not trying to use a non-static member function pointer as a thread then pthreads should work just fine with C++.

Judge Maygarden
+4  A: 

Your packetreadertask function must be a function that takes a single void * as a parameter. This is the important error message:

cooperbussman.cpp:76: error: invalid conversion from âvoid* (*)()â to âvoid* (*)(void*)â

Your function is declared something like this:

void *packetreadertask();

where it must be:

void *packetreadertask(void *);
Greg Hewgill
thanks yaar.. it worked... thanks once again....
Chaithra