gpt4 book ai didi

c - 奇怪的 printf/pthread 错误?

转载 作者:行者123 更新时间:2023-11-30 19:22:42 31 4
gpt4 key购买 nike

所以我正在开发一个线程项目,并且正在测试我的一个文件,确保结构和字段获得正确的值。我正在运行这个函数:

struct ReportQueue {
sem_t count;
pthread_mutex_t mutexAdd;
ReportList *RQList;
};

ReportQueue *RQCreate() {
ReportQueue *rq;

printf("RQCreate() called\n");
rq = calloc(1, sizeof(ReportQueue));
sem_init(&rq->count, 0, 0);
pthread_mutex_init(&rq->mutexAdd, NULL);
rq->RQList = NULL;
return rq;
}

有了这个主要:

int main() {
ReportQueue *rQueue;
Report report;

rQueue = RQCreate();
printf("SemCount: |%d| RQList: |%d| MutexAdd |%d|\n", rQueue->count, rQueue->RQList, rQueue->mutexAdd);
printf("SemCount: |%d|\n", rQueue->count);
printf("RQList: |%d|\n", rQueue->RQList);
printf("MutexAdd: |%d|\n", rQueue->mutexAdd);

return;
}

我得到这个输出:

RQCreate() called
SemCount: |0| RQList: |128| MutexAdd |0|
SemCount: |0|
RQList: |0|
MutexAdd: |0|

这对我来说没有任何意义。 “RQList”的值不应该根据我如何打印它而改变?我做错了什么?

最佳答案

一个工作正常的最小示例...这不是答案,但表明一切似乎都很好

+ 它不适合评论:D

#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>

struct ReportQueue {
sem_t count;
pthread_mutex_t mutexAdd;
int *RQList;
};

struct ReportQueue *RQCreate() {
struct ReportQueue *rq;

printf("RQCreate() called\n");
rq = calloc(1, sizeof(struct ReportQueue));
sem_init(&rq->count, 0, 0);
pthread_mutex_init(&rq->mutexAdd, NULL);
rq->RQList = NULL;
return rq;
}

int main() {
struct ReportQueue *rQueue;

rQueue = RQCreate();
printf("SemCount: |%d| RQList: |%d| \n", rQueue->count, (int)rQueue->RQList);
printf("SemCount: |%d|\n", rQueue->count);
printf("RQList: |%d|\n", (int)rQueue->RQList);

return 1;
}

关于c - 奇怪的 printf/pthread 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466367/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com