Read-write locks communication mechanisms
write lock into read and write locks, the following functions
(1) If a thread is applied for a read lock, other threads can request read locks, but can not apply for a write lock.
(2) If a thread applies for a write lock, other threads cannot apply for a read lock or write lock.
Initializing write lock
int to pthread_rwlock_init (pthread_rwlock_t the restrict rwlock is acquired *,
const pthread_rwlockattr_t the restrict * attr);
destruction write lock
int pthread_rwlock_destroy (pthread_rwlock_t * rwlock);
applying a read lock
int the pthread_rwlock_rdlock (pthread_rwlock_t * rwlock is acquired);
int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock is acquired);
Apply for write lock
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
unlock
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
program example:
程序实现4个线程,两个读线程,两个写线程。
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata[BUFSIZE];
int gtime;
void *thread_fun_read1(void*arg);
void *thread_fun_read2(void*arg);
void *thread_fun_write1(void*arg);
void *thread_fun_write2(void*arg);
int main(int argc,char* argv[])
{
int res;
pthread_t th1,th2,th3,th4;
void* thret;
res = pthread_rwlock_init(&rwlock,NULL);
if(res != 0)
{
perror("rwlock initialization failed");
exit(-1);
}
res = pthread_create(&th1,NULL,thread_fun_read1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th2,NULL,thread_fun_read2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th3,NULL,thread_fun_write1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th4,NULL,thread_fun_write2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_join(th1,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th2,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th3,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th4,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
pthread_rwlock_destroy(&rwlock);
exit(0);
}
void *thread_fun_read1(void*arg)
{
printf("thread read one try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read one. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_read2(void*arg)
{
printf("thread read two try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read two. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_write1(void*arg)
{
printf("this is write thread one try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread one.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
void *thread_fun_write2(void*arg)
{
printf("this is write thread two try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread two.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
write lock into read and write locks, the following functions
(1) If a thread is applied for a read lock, other threads can request read locks, but can not apply for a write lock.
(2) If a thread applies for a write lock, other threads cannot apply for a read lock or write lock.
Initializing write lock
int to pthread_rwlock_init (pthread_rwlock_t the restrict rwlock is acquired *,
const pthread_rwlockattr_t the restrict * attr);
destruction write lock
int pthread_rwlock_destroy (pthread_rwlock_t * rwlock);
applying a read lock
int the pthread_rwlock_rdlock (pthread_rwlock_t * rwlock is acquired);
int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock is acquired);
Apply for write lock
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
unlock
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
program example:
程序实现4个线程,两个读线程,两个写线程。
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata[BUFSIZE];
int gtime;
void *thread_fun_read1(void*arg);
void *thread_fun_read2(void*arg);
void *thread_fun_write1(void*arg);
void *thread_fun_write2(void*arg);
int main(int argc,char* argv[])
{
int res;
pthread_t th1,th2,th3,th4;
void* thret;
res = pthread_rwlock_init(&rwlock,NULL);
if(res != 0)
{
perror("rwlock initialization failed");
exit(-1);
}
res = pthread_create(&th1,NULL,thread_fun_read1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th2,NULL,thread_fun_read2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th3,NULL,thread_fun_write1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th4,NULL,thread_fun_write2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_join(th1,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th2,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th3,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th4,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
pthread_rwlock_destroy(&rwlock);
exit(0);
}
void *thread_fun_read1(void*arg)
{
printf("thread read one try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read one. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_read2(void*arg)
{
printf("thread read two try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read two. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_write1(void*arg)
{
printf("this is write thread one try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread one.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
void *thread_fun_write2(void*arg)
{
printf("this is write thread two try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread two.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}