Pthread段错误,使用指向main中变量的指针
Pthread segment fault using pointer to variable in main
我是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 producern";
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 %dn", data->id, message, data->ptr->size());
std::cout << temp;
//std::cout << "hi I'm a producern";
sem_post(&mutex);
sem_post(&fullCount);
}
else{
sem_post(&fullCount);
sem_post(&mutex);
pthread_exit(nullptr);
}
}
}
void *consumer(void *arg){
std::cout << ("spawned consumern");
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 %dn", data->id, message, data->ptr->size());
data->ptr->pop();
std::cout << temp;
//std::cout << "Hi I'm a consumern";
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 %dn", 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;
}
iCount <= data->itemcount
始终为真。CCD_ 2从不退出循环。在某个时刻,它耗尽了堆栈,随后的top()
调用显示出未定义的行为。
i
,cThrData[i].ptr
从未初始化。consumer
通过未初始化的指针调用top()
,因此程序显示出未定义的行为。
相关文章:
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- Pthread段错误,使用指向main中变量的指针
- 将变量从 main 传递到公共类函数?
- 如何使用类中的函数从 main 打印变量,帮助理解 OOP c++
- 在 main 之前调试全局变量的赋值
- C++ main 函数中初始化静态变量
- 如何使用GDB跟踪Main()之前初始化的所有静态全局变量
- 如何使一个成员变量等于在main()中设置的另一个成员变量
- C++ - 类方法更改成员变量,但不在 main 中
- 如何让变量在每次运行时增加 "return main();"
- 静态变量需要在MAIN处理ARGV参数之前进行初始化
- 静态内联变量是否需要在 main 之前初始化?
- 如何获取函数以在 Main() 中重新处理变量
- 我需要创建一个函数在用户下打印"=",但由于变量是使用 main() 声明的,因此函数看不到参数
- 非本地非内联变量的初始化:是否严格在"main()"函数调用之前进行
- 为什么我不能在 main 函数中更改类的公共变量
- 如何使用 x86 程序集在 c 中访问 main 函数中的变量
- 在 main() 中定义全局变量