简体   繁体   中英

pthread_create error in C++ (pthread inside class)

I have this C++ code, where i try to create a pthread, and i got 4 errors:

Can anyone please help?

Thanks in advance.

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

static void* func(void*);

class Test
{
 public:
    pthread_t *threadId;
    pthread_create(threadId, NULL, func, NULL);

 };

 static void* func(void *arg)
 {
   printf("Thread function called\n");
 }

 int main()
 {
  Test();
 }

Compiling:

 # g++ simplepThread.cc -lpthread
      simplepThread.cc:11: error: ‘threadId’ is not a type
      simplepThread.cc:11: error: expected identifier before ‘__null’
      simplepThread.cc:11: error: expected ‘,’ or ‘...’ before ‘__null’
      simplepThread.cc:11: error: ISO C++ forbids declaration of ‘pthread_create’ with no type

If i use the thread function as a "C" linkage:

extern "C" void* func(void *arg)
{
  printf("Thread function called\n");
}

Error faced is:

simplepThread.cc:7: error: previous declaration of ‘void* func(void*)’ with ‘C++’ linkage
simplepThread.cc:15: error: conflicts with new declaration with ‘C’ linkage

You can't call functions in class declaration. In class declaration you can only declare (and possibly define) its members. And

pthread_create(threadId, NULL, func, NULL);

is not a valid member function definition.
The whole class Test seems to be redundant.

static void* func(void *arg)
{
   printf("Thread function called\n");
}
int main()
{
  pthread_t threadId;

  pthread_create(&threadId, NULL, func, NULL);
}

Should work fine.
I fixed also another problem - you tried to pass an uninitialized pointer ( threadId ) to a function that expects a variable address.
UPDATE
About the linkage - you have a prototype with default (C++ linkage)

void* func(void *arg);

And definition with C linkage

extern "C"
{
    void* func(void *arg)
    {
    ....
    }
}

So they conflict. change prototype to

extern "C"
{
  void* func(void *arg);
}

And it will be OK

There are a number of issues with your code. First of all, you need to declare a constructor function for the Test class, and given how you used the code, I would put the pthread_create() call inside the constructor. Secondly, while pthread_create() takes as its first argument a pthread_t* argument, this means that that argument is being used as an output parameter and the pointer should point to the actual memory/variable where the thread ID for the newly created thread will be placed.

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

static void* func(void*);

class Test
{
 public:
    pthread_t threadId;

    Test() {
       pthread_create(&threadId, NULL, func, NULL);
    }
 };

 static void* func(void *arg)
 {
   printf("Thread function called\n");
 }

 int main()
 {
  Test();
 }

In general when you are doing multi-threaded code, you also want to ensure that the threads are destroyed when you are finished with them, or keep track of when they die on their own. For example, if you create a "worker" thread that will be used to process work in the background asynchronously, then you will often want to have a mutex-protected queue for the main thread to pass work to the worker thread. You would often also want some semaphore or other "safe" signaling system to cause the worker thread to die safely when the program wants to exit, with a back-signaling mechanism so the main thread knows when the worker has died and it is now safe to clean up the shared data structures. (ie, putting a piece of work in the job queue that simply says "die", and having the worker reply "dying" before exiting)

Implementing correct multi-threaded code is non-trivial, and you have to worry about a wide range of issues, such as deadlock and race conditions, that simply do not occur with single-threaded code. I strongly recommend that you read about, and ensure that you fully understand, these and other topics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM