Pthread_create 调用 class 中的函数
Pthread_create call function in class
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);
}
}
我的问题是,当我尝试使用 pthread_create 调用 class 客户中的函数时,弹出关于 'undefined reference to Customer::~A()' 的错误
和 'undefined reference to `Customer::A()''.
我想创建一个 class Customer 对象的数组,并使用 multi_thread 执行 class Customer 中的 运行 函数,我不知道如何来处理这些错误。谢谢
我在 Xcode 中使用 C++,在 linux 中编译。
----------------更新--------------------
现在我仍然遇到错误 'undefined reference to `Customer::id''。
不知道为什么。
我建议你使用 stl 容器而不是 C 数组。
Customer::run 是静态函数,所以你不需要像这样传递这个函数:
status = pthread_create (..., Customers[index].run, ...);
要将静态函数传递给 pthread,您需要传递指向静态函数的指针:
status = pthread_create(..., &Customers::run, ...);
好的,我们传递函数,但我猜您希望将具体的 Customer 对象传递给线程
status = pthread_create(..., &Customers::run, (void *)Customers[index]);
代码的最终版本看起来像
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
}
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);
}
}
我的问题是,当我尝试使用 pthread_create 调用 class 客户中的函数时,弹出关于 'undefined reference to Customer::~A()' 的错误 和 'undefined reference to `Customer::A()''.
我想创建一个 class Customer 对象的数组,并使用 multi_thread 执行 class Customer 中的 运行 函数,我不知道如何来处理这些错误。谢谢
我在 Xcode 中使用 C++,在 linux 中编译。
----------------更新--------------------
现在我仍然遇到错误 'undefined reference to `Customer::id''。
不知道为什么。
我建议你使用 stl 容器而不是 C 数组。
Customer::run 是静态函数,所以你不需要像这样传递这个函数:
status = pthread_create (..., Customers[index].run, ...);
要将静态函数传递给 pthread,您需要传递指向静态函数的指针:
status = pthread_create(..., &Customers::run, ...);
好的,我们传递函数,但我猜您希望将具体的 Customer 对象传递给线程
status = pthread_create(..., &Customers::run, (void *)Customers[index]);
代码的最终版本看起来像
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
}