gpt4 book ai didi

c - 互斥线程 - 代码似乎无法正确退出

转载 作者:行者123 更新时间:2023-11-30 20:38:35 26 4
gpt4 key购买 nike

所以我这里有代码可以创建两种线程。一种“生产”数据,另一种“消费”数据。任何一次只能存在一定数量的数据,因此一旦创建了一定数量的数据(即当sharedData = BUFFER时),生产者就会暂停生产,而当sharedData = 0时,消费者将暂停生产. 也只能制作这么多数据(存储在 dataleft 中的数量),一旦制作并消耗了所有数据,程序就应该结束。

出于某种原因,代码末尾的 printf() 行似乎从未触发。我无法判断线程是否因此正确关闭。感觉好像我做了一件非常愚蠢的事情,但我看不到问题所在。

开头的几个定义:

#define                 NUMCONSUMERS    4
#define NUMPRODUCERS 4
#define PACKETS 10
#define tryMainlock pthread_mutex_trylock(&dataMutex)
#define openMainlock pthread_mutex_lock(&dataMutex)
#define closeMainlock pthread_mutex_unlock(&dataMutex)
#define waitMainlock pthread_cond_wait(&dataPresentCondition, &dataMutex);
#define signalMainlock pthread_cond_signal(&dataPresentCondition);

#define trydatalock pthread_mutex_trylock(&IsthereDataleft)
#define opendatalock pthread_mutex_lock(&IsthereDataleft)
#define closedatalock pthread_mutex_unlock(&IsthereDataleft)

pthread_mutex_t dataMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t dataPresentCondition = PTHREAD_COND_INITIALIZER;

pthread_mutex_t IsthereDataleft = PTHREAD_MUTEX_INITIALIZER;

int sharedData=0; //amount of data present
int BUFFER = 5;
int dataleft=PACKETS;

主要功能:

int main(int argc, char **argv)
{
int rc;
int i;
pthread_t consumer[NUMCONSUMERS];
pthread_t producer[NUMPRODUCERS];

rc = opendatalock; //lock to determine whether there's any point waiting for data

for (i=0; i <NUMPRODUCERS; i++) { //Build up the producers
rc = pthread_create(&producer[i], NULL, Producer, (void *)i);
if (rc)
printf("Error building Producer Thread: %x\n", i);
}

for (i=0; i <NUMCONSUMERS; i++) { //Build up the consumers
rc = pthread_create(&consumer[i], NULL, Consumer, (void *)i);
if (rc)
printf("Error building Consumer Thread: %x\n", i);
}

printf("All Producers and Consumers created\n");

for (i=0; i <NUMPRODUCERS; i++) { //Join up the producers
rc = pthread_join(producer[i], NULL);
if (rc)
printf("Error: Producer %x: Failed to join\n", i);
}

rc = closedatalock; //producers finished, no data left to make

printf("datalock closed, consumers finishing...\n");
for (i=0; i <NUMCONSUMERS; i++) { //Join up the consumers
rc = pthread_join(consumer[i], NULL);
if (rc)
printf("Error: Consumer %x: Failed to join\n", i);
}
rc = pthread_mutex_destroy(&dataMutex);
rc = pthread_cond_destroy(&dataPresentCondition);
rc = pthread_mutex_destroy(&IsthereDataleft);

printf("All Threads finished. Exiting....\n");
return 0;
}

消费者线程:

void *Consumer(void *threadid){
int rc;
printf("Consumer Thread %x: Created\n", (int)threadid);
while (1)
{
printf("Consumer %x: Entering Loop\n", (int)threadid);
rc = openMainlock; //take hold of main lock
if (rc)
{
printf("Consumer %x: Waiting...\n", (int)threadid);
rc = waitMainlock; //if main lock is taken, wait
if (rc) //if wait fails, exit the thread.
{
printf("Consumer Thread %x: wait for Main Lock failed\n", threadid);
exit(0);
}
}

while (sharedData == 0) //if the buffer is empty
{
rc = trydatalock;
if (!rc)
{
printf("Consumer %x: Completed. Exiting...\n");
exit(0);
}
rc = closeMainlock;
if (rc)
{
printf("code.\n");
}
rc = waitMainlock;
if (rc)
{
printf("code.\n");
}
}
sharedData--;
rc = closeMainlock;
rc = signalMainlock;
if (rc)
{
printf("code.\n");
}
printf("Consumer %x: Releasing Lock\n", (int)threadid);
}
}

和生产者线程:

void *Producer(void *threadid){
int rc;
printf("Producer Thread %x: Created\n", (int)threadid);
while (1)
{
printf("Producer %x: Entering Loop\n", (int)threadid);
rc = openMainlock; //take hold of the lock
if (rc) //if lock is currently being used by a consumer or a producer
{
printf("Producer %x: Waiting...\n", (int)threadid);
rc = waitMainlock; //wait here until lock is released
if (rc)
{
printf("Producer Thread %x: wait for Main Lock failed\n", threadid);
exit(0);
}
}
if (!dataleft) //If there's no data left to add to the stream, close the thread
{
printf("Producer Thread %x: Completed, exiting...\n", (int)threadid);
exit(0);
}
while (sharedData >=BUFFER)
{
rc = closeMainlock;
if (rc)
{
printf("code.\n");
}
rc = waitMainlock;
if (rc)
{
printf("code.\n");
}
}
printf("Producer %x: Lock Acquired\n", (int)threadid);
sharedData++;
dataleft--;
rc = closeMainlock;
rc = signalMainlock;
if (rc)
{
printf("code.\n");
}
printf("Producer %x: Releasing Lock\n", (int)threadid);
}
}

最佳答案

查看这段代码:

    if (!rc)
{
printf("Consumer %x: Completed. Exiting...\n");
exit(0);
}

如果消费者已完成,则进程(!)将终止。您需要使用 pthread_exit() 来代替,或者只是从线程函数返回。

然后还有

../nptl/pthread_mutex_lock.c:80:
__pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.

我运行了几次代码。这可能是由于例如双重解锁或其他一些无效使用。我将从清理奇怪的宏开始,以便您可以自由地了解程序本身的逻辑。

此外,关于互斥锁的一个重要建议:始终准确记录哪些数据应受互斥锁保护。关键是,它并不总是很清楚,如果犯了这个错误,意味着您意外地访问了没有同步的数据。为了使这一点非常清楚,请使用如下结构:

 struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
int data;
} synced_data = {
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
0
};

实际上,文档的重要性不仅仅与共享数据有关。考虑一下 IsthereDataleft 的例子:这是一个互斥体,但它不保护任何东西,对吧?相反,它用于向启动的线程发出信号,表明没有什么可做的,对吧?记录下来不仅可以帮助其他人理解您的代码,还可以确保您了解自己的意图是什么。有时,当你试图解释它时,你会发现自己有些事情没有道理。

关于c - 互斥线程 - 代码似乎无法正确退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28591604/

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