#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char**argv)
{
if(argc<3)
{
printf("--usage %s [r r|r w |w w]\n",argv[0]);
exit(1);
}
pthread_rwlock_t rw_lock;
pthread_rwlock_init(&rw_lock,NULL);
if(!strcmp(argv[1],"r"))
{
if(pthread_rwlock_rdlock(&rw_lock)!=0)
{
printf("the first lock failed\n");
}
else
printf("the first lock success\n");
}
if(!strcmp(argv[1],"w"))
{
if(pthread_rwlock_wrlock(&rw_lock)!=0)
{
printf("the first lock failed\n");
}
else
printf("the first lock success\n");
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char**argv)
{
if(argc<3)
{
printf("--usage %s [r r|r w |w w]\n",argv[0]);
exit(1);
}
pthread_rwlock_t rw_lock;
pthread_rwlock_init(&rw_lock,NULL);
if(!strcmp(argv[1],"r"))
{
if(pthread_rwlock_rdlock(&rw_lock)!=0)
{
printf("the first lock failed\n");
}
else
printf("the first lock success\n");
}
if(!strcmp(argv[1],"w"))
{
if(pthread_rwlock_wrlock(&rw_lock)!=0)
{
printf("the first lock failed\n");
}
else
printf("the first lock success\n");
}
if(!strcmp(argv[2],"r"))
{
if(pthread_rwlock_rdlock(&rw_lock)!=0)
{
printf("the second lock failed\n");
}
else
printf("the second lock success\n");
}
if(!strcmp(argv[2],"w"))
{
if(pthread_rwlock_wrlock(&rw_lock)!=0)
{
printf("the second lock failed\n");
}
else
printf("the second lock success\n");
}
pthread_rwlock_unlock(&rw_lock);
pthread_rwlock_unlock(&rw_lock);
pthread_rwlock_destroy(&rw_lock);
return 0;
}