CS代考 COMP5426 Distributed

Programming
COMP5426 Distributed
Platforms (Pthread Basics(3))

Copyright By PowCoder代写 加微信 powcoder

set of operations.
Higher lev
synchronization constructs, e.g.,
hronization
reads provide su
semaphore and

Write Locks
In many applications, a frequently but written
ions, we should
data structure is read infrequently. For such
A read lock is granted when there are other that may already have read locks.
If there is a write lock on the data (or if there are queued write locks), the thread performs a condition wait.
If there are multiple threads requesting a write they must perform a condition wait.
With this description, we can design functions
mylib_rwlock_rlock,  write lock mylib_rwlock_wlock,
 read unlock mylib_rwlock_runlock, an  write unlock mylib_rwlock_wunlock
read-write locks.

The lock d following:
Write Locks
ount of the numb
ondition variable readers_proceed signaled when readers can proceed,
 a condition variable writer_proceed that is signaled when one of the writers can proceed, and
 a mutex read_write_lock associated with t
ib_rwlock_
of readers,
data structure and condition variables
pending writers,
 the writer (a 0/1 integer specifying whether a wri is present),
 a count pending_writers of

typedef struct { int readers;
int writer;
int pending_writers;
pthread_cond
pthread_cond
Write Locks
pthread_mutex_t read_writ
mylib_rwlock_t;
mylib_rwlock
_t readers_proceed;
_init (mylib_rwlock_t *l)
l->readers = l->writer = l->pending_writers = 0;
pthread_mutex_init(&(l->read_write_lock), NULL); pthread_cond_init(&(l->readers_proceed), NULL); pthread_cond_init(&(l->writer_proceed), NULL);

pthread_mute
pthread_mute
Write Locks
void mylib_rwlock_rlock(mylib_rwlock_t *l) {
/* if there is a write lock, perform condition count of readers and grant read lock */
x_lock(&(l-
>read_write_loc
riter > 0 || l->pending_writers > 0)
pthread_cond_wait(&(l->readers_proceed), &(l->read_write_lock));
l->readers ++;
ock(&(l->read_write_lock))
else increment

Write Locks
void mylib_rwlock_wlock(mylib_rwlock_t *l) {
/* if there are readers or writers, increment pending writers count and wait. On being awoken, decrement pending writers count and increment writer count */
pthread_mutex_lock(&(l->read_write_lock));
l->pending_writers ++;
while ((l->writer > 0) || (l->readers > 0)) {
pthread_cond_wait(&(l->writer_proceed),
&(l->read_write_lock));
pthread_mutex_unlock(&(l->read_write_loc

pthread_mutex_lo
(l->readers =
Write Locks
void mylib_rwlock_runlock(mylib_rwlock_t *l) {
/* Decrement count of readers. If the reader count is pending writers, let one through */
pthread_mutex_unlock(&(l->
ck(&(l->read_write
read_cond_signal(&(l->writer_proceed));
0 and ther

pthread_mutex_lo
riter = 0;
Write Locks
void mylib_rwlock_wunlock(mylib_rwlock_t *l) {
/* Decrement write count. If there is a pending writer, let it through, else if there are pending readers, let them all go through */
(l->pending_writers > 0)
ck(&(l->read_write
read_cond_signal(&(l->writer_proceed));
read_cond_broadcast(&(l->readers_proc
pthread_mutex_unlock(&(l->

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com