因此,我正在研究线程项目,并且正在测试我的文件之一,确保结构和字段获得正确的值。我正在运行此功能:
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/