读写锁:
1. 如果某线程申请了读锁,其他线程可以再申请读锁,但不能申请写锁
2. 如果某线程申请了写锁,其他线程不能申请 读锁 、 写锁- 初始化: pthread_rwlock_init
-
阻塞读: pthread_rwlock_rdlock
-
非阻塞读: pthread_rwlock_tryrdlock
-
阻塞写: pthread_rwlock_wrlock
-
非阻塞写: pthread_rwlock_trywrlock
-
释放 : pthread_rwlock_unlock
-
销毁: pthread_rwlock_destroy
代码: pthread_rwlock.rar - #include <stdio.h>
-
#include <pthread.h>
-
#include <stdlib.h> //exit
-
#include <errno.h>
-
#include <bits/pthreadtypes.h>
-
-
static pthread_rwlock_t rwlock;
-
#if 0
-
111: typedef struct _pthread_rwlock_t
-
112: {
-
113: struct _pthread_fastlock __rw_lock; /* Lock to guarantee mutual exclusion */
-
114: int __rw_readers; /* Number of readers */
-
115: _pthread_descr __rw_writer; /* Identity of writer, or NULL if none */
-
116: _pthread_descr __rw_read_waiting; /* Threads waiting for reading */
-
117: _pthread_descr __rw_write_waiting; /* Threads waiting for writing */
-
118: int __rw_kind; /* Reader/Writer preference selection */
-
119: int __rw_pshared; /* Shared between processes or not */
-
120: } pthread_rwlock_t;
-
#endif
-
-
#define WORK_SIZE 1024
-
-
char work_area[WORK_SIZE]; // 共享全局数据
-
int time_to_exit; //退出标识
-
-
void print_res(int *a, char *s);
-
-
void *thread_fun_read_o(void *a); //读线程 one
-
void *thread_fun_read_t(void *a); //读线程 two
-
void *thread_fun_write_o(void *a); //写线程 one
-
void *thread_fun_write_t(void *a); //写线程 two
-
-
int main(int argc, char *argv[])
-
{
-
int res;
-
pthread_t a_thread,b_thread,c_thread,d_thread; //unsigned long
-
-
void *thread_result; //thread_join 参数
-
-
//初始化 读写锁
-
res = pthread_rwlock_init(&rwlock, NULL);
-
print_res(&res, "thread_rwlock");
-
-
//产生 4 个子线程
-
res = pthread_create(&a_thread, NULL, thread_fun_read_o, NULL);
-
print_res(&res,"pthread_create a ");
-
-
res = pthread_create(&b_thread, NULL, thread_fun_read_t, NULL);
-
print_res(&res,"pthread_create b ");
-
-
res = pthread_create(&c_thread, NULL, thread_fun_write_o, NULL);
-
print_res(&res,"pthread_create c ");
-
-
res = pthread_create(&d_thread, NULL, thread_fun_write_t, NULL);
-
print_res(&res,"pthread_create d ");
-
-
// 等待 4 个子线程 退出
-
res = pthread_join(a_thread, &thread_result);
-
print_res(&res, "pthread_join a");
-
-
res = pthread_join(b_thread, &thread_result);
-
print_res(&res, "pthread_join b");
-
-
res = pthread_join(c_thread, &thread_result);
-
print_res(&res, "pthread_join c");
-
-
res = pthread_join(d_thread, &thread_result);
-
print_res(&res, "pthread_join d");
-
-
pthread_rwlock_destroy(&rwlock);
-
-
exit(EXIT_SUCCESS);
-
-
}
-
-
void print_res(int *a, char *s)
-
{
-
if(*a != 0)
-
{
-
printf("%s failed.\n",s);
-
exit(EXIT_FAILURE);
-
}
-
else
-
{
-
printf("%s success\n",s);
-
}
-
}
-
-
void *thread_fun_read_o(void *a)
-
{
-
printf("thread read one try to get lock\n");
-
pthread_rwlock_rdlock(&rwlock); //获取 读取锁
-
while(strncmp("end", work_area, 3) != 0)
-
{
-
printf("this is thread read one.");
-
printf("the characters is %s\n",work_area); //输出
-
-
pthread_rwlock_unlock(&rwlock); //解锁
-
sleep(2);
-
-
pthread_rwlock_rdlock(&rwlock); //获取 读取锁
-
-
while(work_area[0] == '\0')
-
{
-
pthread_rwlock_unlock(&rwlock);
-
sleep(2);
-
pthread_rwlock_rdlock(&rwlock);
-
}
-
}
-
pthread_rwlock_unlock(&rwlock);
-
time_to_exit = 1;
-
pthread_exit(0);
-
}
-
-
void *thread_fun_read_t(void *a)
-
{
-
printf("thread read two try to get lock");
-
pthread_rwlock_rdlock(&rwlock); //获取 读取锁
-
while(strncmp("end", work_area, 3) != 0)
-
{
-
printf("this is thread read two.\n");
-
printf("the characters is %s\n",work_area); //输出
-
-
pthread_rwlock_unlock(&rwlock); //解锁
-
sleep(5);
-
-
pthread_rwlock_rdlock(&rwlock); //获取 读取锁
-
-
while(work_area[0] == '\0')
-
{
-
pthread_rwlock_unlock(&rwlock);
-
sleep(5);
-
pthread_rwlock_rdlock(&rwlock);
-
}
-
}
-
pthread_rwlock_unlock(&rwlock);
-
time_to_exit = 1;
-
pthread_exit(0);
-
}
-
-
void *thread_fun_write_o(void *a)
-
{
-
printf("this is write thread one try to get lock\n");
-
while(!time_to_exit)
-
{
-
pthread_rwlock_wrlock(&rwlock);
-
printf("this is write thread one.input some text. enter 'end' to finish\n");
-
fgets(work_area, WORK_SIZE, stdin);
-
pthread_rwlock_unlock(&rwlock);
-
sleep(15);
-
}
-
pthread_rwlock_unlock(&rwlock);
-
pthread_exit(0);
-
}
-
-
void *thread_fun_write_t(void *a)
-
{
-
sleep(10);
-
// printf("this is write thread two try to get lock\n");
-
while(!time_to_exit)
-
{
-
pthread_rwlock_wrlock(&rwlock);
-
printf("this is write thread two.input some text. enter 'end' to finish\n");
-
fgets(work_area, WORK_SIZE, stdin);
-
pthread_rwlock_unlock(&rwlock);
-
sleep(20);
-
}
-
pthread_rwlock_unlock(&rwlock);
-
pthread_exit(0);
-
}
- ywx@yuweixian:~/yu/professional/4$ ./pthread_rwlock thread_rwlock success
-
pthread_create a success
-
pthread_create b success
-
pthread_create c success
-
thread read one try to get lock
-
this is thread read one.the characters is
-
pthread_create d success
-
thread read two try to get lockthis is thread read two.
-
the characters is
-
this is write thread one try to get lock
-
this is write thread one.input some text. enter 'end' to finish
-
abcdefghijklmnopqrstuvwxyz
-
this is write thread two.input some text. enter 'end' to finish
-
123456789
-
this is thread read one.the characters is 123456789
-
-
this is thread read two.
-
the characters is 123456789
-
-
this is thread read one.the characters is 123456789
-
-
this is thread read one.the characters is 123456789
-
-
this is write thread one.input some text. enter 'end' to finish
-
end
-
pthread_join a success
-
pthread_join b success
-
pthread_join c success
-
pthread_join d success
阅读(1804) | 评论(0) | 转发(0) |