I have a producer/consumer concurrency problem that I’m working on. The problem is that I’m getting a segfault thrown immediately after trying to create my first thread.
Relevant code:
customer is a struct declared as:
struct pr2_customer
{
pthread_t customer_id;
};
typedef struct pr2_customer customer;
customers is a c++ vector, declared like:
vector<customer> customers;
Create the thread:
for(int i = 0; i < ncustomers; i++)
{
cout<<"creating a customer\n";
pthread_create(&customers[i].customer_id, &attr, customerAction, (void*)i);
}
Output:
creating a customer
segfault
customerAction has a cout statement as it’s first line which never gets executed, leading me to believe the thread is never created.
Any help is greatly appreciated.
What appears to me is that you haven’t reserved any space in
customers. I think this is what you need: