【发布时间】:2018-08-01 19:57:02
【问题描述】:
我是 pthread 的新手,我试图实现一个生产者/消费者问题,让用户选择缓冲区大小、生产者数量、消费者数量和项目总数。我一直在查看我认为与堆栈溢出类似的内容,但似乎无法正确处理。
我的代码有一个主类,它产生生产者和消费者。它给生产者和消费者一个指向在 main 中初始化的堆栈的指针(或者至少我正在尝试)。我认为我所做的事情是合法的,并且在 CLion 中我得到了我想要的预测文本选项,所以我认为我正确地链接了它,但是当我尝试阅读顶部元素时出现了段错误。
我使用 GDB 来确保我知道我在哪条线路上崩溃了,但我不明白我的设置出了什么问题。在调试时,我确认生产者首先通过其 push() 命令,但消费者在尝试 top() 或 pop() 时失败。我在这里看到了一些 OP 有问题的线程,因为他们没有加入他们的线程,但我有点迷茫。
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stack>
#include <cstring>
#include <semaphore.h>
#include <pthread.h>
#define N 10000
sem_t mutex;
sem_t fullCount;
sem_t emptyCount;
int iCount = 0;
typedef struct _thread_data{
int id;
int itemcount;
std::stack<char>* ptr;
}thread_data;
void *producer(void *arg){
std::cout << "spawned producer\n";
thread_data *data = (thread_data *)arg;
while(true){
char message = 'X';
sem_wait(&emptyCount);
sem_wait(&mutex);
if(iCount < data->itemcount){
data->ptr->push(message);
iCount++;
char temp [25];
sprintf(temp, "p:<%u>, item: %c, at %d\n", data->id, message, data->ptr->size());
std::cout << temp;
//std::cout << "hi I'm a producer\n";
sem_post(&mutex);
sem_post(&fullCount);
}
else{
sem_post(&fullCount);
sem_post(&mutex);
pthread_exit(nullptr);
}
}
}
void *consumer(void *arg){
std::cout << ("spawned consumer\n");
thread_data *data = (thread_data *)arg;
while(true){
char message;
sem_wait(&fullCount);
sem_wait(&mutex);
if(iCount < data->itemcount) {
message = data->ptr->top(); //SEGFAULT
char temp[25];
printf(temp, "c:<%u>, item: %c, at %d\n", data->id, message, data->ptr->size());
data->ptr->pop();
std::cout << temp;
//std::cout << "Hi I'm a consumer\n";
sem_post(&mutex);
sem_post(&emptyCount);
}
else if (iCount == data->itemcount){
message = data->ptr->top(); //SEGFAULT
char temp[25];
printf(temp, "c:<%u>, item: %c, at %d\n", data->id, message, data->ptr->size());
data->ptr->pop();
std::cout << temp;
sem_post(&emptyCount);
sem_post(&mutex);
pthread_exit(nullptr);
}
else{
sem_post(&mutex);
pthread_exit(nullptr);
}
}
}
int main(int argc, char *argv[]){
int bufferSize = N;
int pThreadCount,cThreadCount,itemCount;
for (int x = 0; x < argc; ++x) {
if(strcmp(argv[x],"-b") == 0){
bufferSize = atoi(argv[x+1]);
}
if(strcmp(argv[x],"-p") == 0){
pThreadCount = atoi(argv[x+1]);
}
if(strcmp(argv[x],"-c") == 0){
cThreadCount = atoi(argv[x+1]);
}
if(strcmp(argv[x],"-i") == 0){
itemCount = atoi(argv[x+1]);
}
}
sem_init(&mutex,1,1);
sem_init(&fullCount,1,0);
sem_init(&emptyCount,1,bufferSize);
std::stack<char> myStack;
pthread_t myPThreads[pThreadCount];
thread_data thrData[pThreadCount];
pthread_t myCThreads[cThreadCount];
thread_data cThrData[cThreadCount];
for (int i = 0; i < pThreadCount; ++i) {
thrData[i].id = i;
thrData[i].itemcount = itemCount;
thrData[i].ptr = &myStack;
pthread_create(&myPThreads[i], NULL, producer, &thrData[i]);
}
for (int i = 0; i < cThreadCount; ++i) {
cThrData[i].id = i;
cThrData[i].itemcount = itemCount;
thrData[i].ptr = &myStack;
pthread_create(&myCThreads[i], NULL, consumer, &cThrData[i]);
}
for (int k = 0; k < pThreadCount; ++k) {
pthread_join(myPThreads[k], NULL);
}
for (int j = 0; j < cThreadCount; ++j) {
pthread_join(myCThreads[j], NULL);
}
return 0;
}
【问题讨论】:
-
extern "C" int main不是有效的 C++,pthread_t myPThreads[pThreadCount]; -
@VTT 感谢您的快速回复,我删除了
extern "C"部分,但我在一堆示例中看到pthread_t someName[numberOfThreads],我错过了什么吗? -
可变长度数组在最近的 C 变体中作为(可选且不常见的)功能存在,但在 C++ 中不存在。如果你的编译器编译了这段代码,那么它很可能是一个 gnu 扩展。
-
@plshelp 请更新您的代码以反映您尝试过的事情的当前状态(删除 extern "C" 和其他任何内容)
-
@VTT 有没有更好的方法来实现输入缓冲区大小、生产者数量、消费者数量和项目总数的能力?当我删除尝试引用 myStack 的行并仅添加打印语句时,线程似乎工作正常。