gpt4 book ai didi

C 读写器线程锁解锁

转载 作者:行者123 更新时间:2023-11-30 15:54:16 26 4
gpt4 key购买 nike

我试图完成我认为很简单的任务,但几天后已经达到了临界点。

我正在尝试使用多个读取器和单个写入器来模拟数据库。当我运行程序时它死锁。

我试图将其基于此算法:

 1 semaphore accessMutex;     // Initialized to 1
2 semaphore readersMutex; // Initialized to 1
3 semaphore orderMutex; // Initialized to 1
4
5 unsigned int readers = 0; // Number of readers accessing the resource
6
7 void reader()
8 {
9 P(orderMutex); // Remember our order of arrival
10
11 P(readersMutex); // We will manipulate the readers counter
12 if (readers == 0) // If there are currently no readers (we came first)...
13 P(accessMutex); // ...requests exclusive access to the resource for readers
14 readers++; // Note that there is now one more reader
15 V(orderMutex); // Release order of arrival semaphore (we have been served)
16 V(readersMutex); // We are done accessing the number of readers for now
17
18 ReadResource(); // Here the reader can read the resource at will
19
20 P(readersMutex); // We will manipulate the readers counter
21 readers--; // We are leaving, there is one less reader
22 if (readers == 0) // If there are no more readers currently reading...
23 V(accessMutex); // ...release exclusive access to the resource
24 V(readersMutex); // We are done accessing the number of readers for now
25 }
26
27 void writer()
28 {
29 P(orderMutex); // Remember our order of arrival
30 P(accessMutex); // Request exclusive access to the resource
31 V(orderMutex); // Release order of arrival semaphore (we have been served)
32
33 WriteResource(); // Here the writer can modify the resource at will
34
35 V(accessMutex); // Release exclusive access to the resource
36 }

但是我尝试仅使用 pthreads 而不是信号量来实现它。正如你所预料的,结果是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX 5 //size of buffer

pthread_mutex_t mutex; //pthread_mutex type
pthread_cond_t condw, condr; //reader/writer cond var
int buffer = 0, rc = 0;

pthread_mutex_t db; //pthread_mutex type

//reader-writer header:
void* writer(void* ptr);
void* reader(void* ptr);

void use_data();
void write_database();
void readdb();

int main(int argc, char** argv)
{
pthread_t read,write;
//allow signal back and forth
pthread_mutex_init(&mutex,0); //init mutex
pthread_cond_init(&condw, 0); //init writer
pthread_cond_init(&condr,0); //init reader

pthread_mutex_init(&db,0); //init db

//this calls the void* reader function
pthread_create(&write,0,writer,0); //create thread
pthread_create(&read,0,reader,0); //create thread

//let them join
pthread_join(read,0);
pthread_join(write,0);

//destroy them
pthread_cond_destroy(&condw);
pthread_cond_destroy(&condr);
pthread_mutex_destroy(&db);
pthread_mutex_destroy(&mutex);

return 0;
}//end main

void* reader(void* arg)
{
while(1) //if there is a reader lock the db
{
pthread_mutex_lock(&mutex);
rc++;
if(rc==1)
{
pthread_mutex_lock(& db);
}
pthread_mutex_unlock(&mutex);
readdb();
pthread_mutex_lock(&mutex);
rc--;
if(rc==0)
{
pthread_mutex_unlock(&db);
}
pthread_mutex_unlock(&mutex);
use_data();
}
}

void* writer(void* arg)
{
while(1) //unlock the db
{
pthread_mutex_lock(&db);
write_database();
pthread_mutex_unlock(&db);
}
}

void use_data(){}
void write_database(){}
void readdb(){}

任何有关工作解决方案和解释我们出错的地方的帮助都将受到赞赏,因为这将帮助我和我的同事摆脱困境。问候。

最佳答案

问题在于您的源算法依赖于这样一个事实:一个线程锁定了 accessMutex 锁,然后另一个线程可能会解锁它。这对于基于信号量的互斥体是允许的,但对于 pthreads 互斥体是不允许的。

pthread 中有信号量,由 sem_init()sem_post()sem_wait() 函数提供。您可以使用它们来编写源算法的直接实现,并且它应该可以正常工作。

另外,pthreads 还提供 native 读写锁类型 - 请参阅函数 pthread_rwlock_init()pthread_rwlock_rdlock()pthread_rwlock_wrlock() 和 pthread_rwlock_unlock()。您可以使用它来进行非常简单的实现,但如果它应该是一个学习练习,那么显然这是没有意义的。

关于C 读写器线程锁解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132621/

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