[英]C Reader Writer Threads Lock Unlock
我試圖完成我認為簡單的任務,但幾天后就達到了一個臨界點。
我正在嘗試使用多個讀取器和一個寫入器來模擬數據庫。 當我運行程序時,它陷入僵局。
我試圖基於此算法:
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
鎖,然后另一個線程可能將其解鎖。 這是允許用信號量互斥體,但不允許用於並行線程互斥。
pthread中有信號量,由sem_init()
, sem_post()
和sem_wait()
函數提供。 您可以使用它們編寫源算法的直接實現,並且它應該可以正常工作。
另外,pthreads還提供了本機讀寫鎖定類型-請參見函數pthread_rwlock_init()
, pthread_rwlock_rdlock()
, pthread_rwlock_wrlock()
和pthread_rwlock_unlock()
。 您可以將其用於一個非常簡單的實現,但是很顯然,如果應該將其作為學習練習,則可能會遺漏要點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.