c++ - pthread 创建后立即发生段错误

标签 c++ multithreading pthreads

我正在处理一个生产者/消费者并发问题。问题是我在尝试创建我的第一个线程后立即抛出段错误。

相关代码:

customer 是一个声明为:

struct pr2_customer
{
    pthread_t customer_id;
};
typedef struct pr2_customer customer;

customers 是一个 c++ vector ,声明如下:

vector<customer> customers;

创建线程:

for(int i = 0; i < ncustomers; i++)
{
    cout<<"creating a customer\n";
    pthread_create(&customers[i].customer_id, &attr, customerAction, (void*)i);
}

输出:

创建客户
段错误

customerAction 有一个 cout 语句,因为它是永远不会执行的第一行,让我相信线程永远不会被创建。

非常感谢任何帮助。

最佳答案

在我看来,您没有在customers 中保留任何空间。我认为这就是您所需要的:

vector<customer> customers(ncustomers);

关于c++ - pthread 创建后立即发生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3909210/

相关文章:

c - Linux C : segfault error 4 in libmysqlclient. so.18.0.0

java - NotifyAll 不工作

windows - 当程序被命令行中的 Edit>Mark 暂停时,究竟发生了什么?

linux - 为什么获取pthread_mutex_lock后sleep()会阻塞整个程序?

c - C 中用于排序 time_t 对象的动态数据结构?

通过 void * 转换枚举

c++ - 如何在javascript(emscripten)中覆盖c++ malloc/free?

c++ - 线程安全和 `const`

c++ - 继承函数的重载解决方案

c++ - const 变量是否将值存储在变量中?