1
class Customer{
public:
       Customer(){};
       Customer(int i)
       {id=i;}
       ~Customer(){...};
       static void* run(void* arg)
       {
       //code for execution
       return NULL;
       }
private:
static int id;
}

int main(void)
{
    int index;
    int status;
    //Create Customer Threads
    pthread_t Customer_Threads[50];
    Customer *Customers;
    Customers=new Customer[50];
    // create 50 Customer threads
    for (index = 0; index < 50; index++) {
        Customers[index]=*new Customer(index);
        status = pthread_create (&Customer_Threads[index], NULL, Customers[index].run, NULL);
        assert(0 ==status);
    }
}

My question is when i try to use pthread_create to call function in class Customer, the error pop up about 'undefined reference to Customer::~A()'' and 'undefined reference to `Customer::A()''.

I want to create an array of class Customer object, and use multi_thread to execute the run function in class Customer, i don't know how to deal with these errors. Thanks.

I use C++ in Xcode and compile in linux.

-----------------Update-------------------

Now i still face an error 'undefined reference to `Customer::id''.

Not sure why.

Kai_90
  • 65
  • 6
  • 1
    You declare the constructors and destructor, but where are the *definitions* (i.e. the implementations) of the constructors and destructor? – Some programmer dude Mar 24 '16 at 08:00
  • Also this expression `Customers[index]=*new Customer(index)` will give you a memory leak. Unlike Java, you don't have to use `new` to create object instances. Just doing `Customer(index)` does that, so change to `Customers[index]=Customer(index)`. – Some programmer dude Mar 24 '16 at 08:07
  • 1
    That is by no means imaginable the error received during compiling **this** code. `Customer`, if it had constructors and a destructor, would have them titled `Customer` (and `~Customer`), not `A`. you're also missing the terminating `;` after your class definition. The compiler should complain to the heavens about those things long before you reach a link phase. – WhozCraig Mar 24 '16 at 08:09
  • @WhozCraig Yes, my mistake. I think i have a lot typo in my first question. – Kai_90 Mar 24 '16 at 08:17
  • 1
    @MantraApps Then why not edit the question to contain a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve)? – Some programmer dude Mar 24 '16 at 08:21
  • @JoachimPileborg Thanks, i have update it. As a beginner, appreciate your suggestion. – Kai_90 Mar 24 '16 at 08:30
  • Your not yet done, e.g. `~Customer(){...};` does not compile. Also, can you remove the loop? And do you even need the `pthread_create()` call or could you invoke the thread-function directly? These are all things you should consider before posting here, you example is supposed to be **minimal**! That said, use C++ threads instead of POSIX threads, they allow you to compile your code on any system supporting threads. – Ulrich Eckhardt Mar 24 '16 at 09:20

1 Answers1

0

I suggest to you use stl container instead of C arrays.

The Customer::run is static function so you not need pass this function like this:

status = pthread_create (..., Customers[index].run, ...);

For passing static function to pthread you need pass pointer to static function:

status = pthread_create(..., &Customers::run, ...);

Ok we pass function but also I guess you wish to pass concrete Customer object to thread

status = pthread_create(..., &Customers::run, (void *)Customers[index]);

End-version of the code will look like

void *Customer::run(void *arg)
{
    Customer *this_ = (Customer *)arg;
    // Do something
}

std::list<pthread_t> pthreads(50);
std::list<Customer *> Customers(50);

for (size_t i = 0; i < pthreads.size(); ++i)
{
   Customers[i] = new Customer();
   status = pthread_create(&pthreads[i], &Customer::run, (void *)Customers[i]);
   ...
}

for (size_t i = 0; i < pthreads.size(); ++i)
{
    pthread_join(pthreads[i]); // block until thread end
    delete Customers[i]; // free mem
}
  • Thanks, I have tried to use your method, but i still found error as below: in function 'Customer::Customer()': undefined reference to 'Customer::id', i think may be my implementation of class isn't right, can you also kindly give me a hint about this class implementation? – Kai_90 Mar 24 '16 at 09:07
  • I guess you didn't initialize customer::id inside obj file, I.e add int customer::Id = 1; after class declaration inside cpp file – Vasiliy Soshnikov Mar 24 '16 at 09:35
  • Yes, fixed. Thanks – Kai_90 Mar 24 '16 at 20:44