0

i started with a simple example http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm i need to divide in the different cpps and headers i have as the following

class definition in Process_Images.h

void PrintHello(void* threadid);

in Process_Images.cpp

void ProcessImages::PrintHello(void* threadid)
{
   long tid;
   tid = (long)threadid;
   std::cout << "Hello World! Thread ID, " << tid << std::endl;
   pthread_exit(NULL);
}

in the main function

ProcessImages PI;

pthread_t threads[2];
pthread_create(&threads[0],NULL,PI.PrintHello,(void *)i);

the error is -->

/home/nvidia/Desktop/cms/tools/vibrante-vcm30t124-linux/cmsapplication_export/cmsapplication/sampleThread.cpp:333:69: error: cannot convert ���ProcessImages::PrintHello��� from type ���void (ProcessImages::)(void*)��� to type ���void* (*)(void*)���
      pthread_create(&threads[0],NULL,CarDetLEFT.PrintHello,(void *)i);
                                                                     ^

any suggestion please?

2
  • What's i? Try using &i Commented Dec 7, 2015 at 15:20
  • No, use C++11 threads instead of pthreads! Seriously, prepare a minimal example and you might find someone to solve this puzzle. Commented Dec 7, 2015 at 16:34

2 Answers 2

1

Since I see C++11 tag in the question, there is absolultey no need to go over pthread route!

std::thread thr(&ProcessImages::PrinteHello, &PI, &i);

will do you good!

Sign up to request clarification or add additional context in comments.

Comments

0

Two issues :

  • the thread function must return void*, not void
  • the thread function cannot be a (non-static) member function

More details can be found on a reference page for pthread_create.

So, change the return type of ProcessImages::PrintHello to void* and make it static :

class ProcessImages {

  //<SNIP>

  public :
    static void* PrintHello(void* threadid);

  //<SNIP>

};

Then this should work better :

pthread_create(&threads[0], NULL, &ProcessImages::PrintHello, (void*) i);

2 Comments

i have made it static, and change it void* when i compile i got a linking error undefifined reference to ProcessImages::PrintHello(void+)
@user3406305 : you need to also update the definition of ProcessImages::PrintHello accordingly (in Process_Images.cpp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.