Transferred from: http://blog.csdn.net/dsg333/article/details/22113489
/* Use a read-write lock to implement four threads to read and write an instance of a program, create four new threads, two threads to read data, and two threads to write data. At any moment, if one thread is writing the data, it will block any operation on all other threads. */
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <bits/pthreadtypes.h>
Static pthread_rwlock_t rwlock;//Read-write lock object
#define WORK_SIZE 1024
Char Work_area[work_size];
int time_to_exit;
void *thread_function_read_o (void *arg);//Read thread 1
void *thread_function_read_t (void *arg);//Read thread 2
void *thread_function_write_o (void *arg);//write Thread 1
void *thread_function_write_t (void *arg);//write Thread 2
int main (int argc,char *argv[])
{
int res;
pthread_t A_thread,b_thread,c_thread,d_thread;
void *thread_result;
Res=pthread_rwlock_init (&rwlock,null);//Initialize read-write lock
if (res! = 0)
{
Perror ("Rwlock initialization Failed");
Exit (Exit_failure);
}
res = pthread_create (&a_thread, NULL, thread_function_read_o, NULL);//create new Thread creating threads
if (res! = 0)
{
Perror ("Thread creation failed");
Exit (Exit_failure);
}
res = pthread_create (&b_thread, NULL, thread_function_read_t, NULL);//create new Thread
if (res! = 0)
{
Perror ("Thread creation failed");
Exit (Exit_failure);
}
res = pthread_create (&c_thread, NULL, thread_function_write_o, NULL);//create new Thread
if (res! = 0)
{
Perror ("Thread creation failed");
Exit (Exit_failure);
}
res = pthread_create (&d_thread, NULL, thread_function_write_t, NULL);//create new Thread
if (res! = 0)
{
Perror ("Thread creation failed");
Exit (Exit_failure);
}
res = Pthread_join (A_thread, &thread_result);//Waiting for a_thread thread to end
if (res! = 0)
{
Perror ("Thread join failed");
Exit (Exit_failure);
}
res = Pthread_join (B_thread, &thread_result);
if (res! = 0)
{
Perror ("Thread join failed");
Exit (Exit_failure);
}
res = Pthread_join (C_thread, &thread_result);
if (res! = 0)
{
Perror ("Thread join failed");
Exit (Exit_failure);
}
res = Pthread_join (D_thread, &thread_result);
if (res! = 0)
{
Perror ("Thread join failed");
Exit (Exit_failure);
}
Pthread_rwlock_destroy (&rwlock);//Destroy read/write lock
Exit (exit_success);
}
void *thread_function_read_o (void *arg)
{
printf ("Thread read one try to get lock\n");
Pthread_rwlock_rdlock (&rwlock);//Get read lock
while (STRNCMP ("End", Work_area, 3)! = 0)
{
printf ("This is thread read one.");
printf ("The characters is%s", Work_area);
Pthread_rwlock_unlock (&rwlock);
Sleep (2);
Pthread_rwlock_rdlock (&rwlock);
while (work_area[0] = = ' + ')
{
Pthread_rwlock_unlock (&rwlock);
Sleep (2);
Pthread_rwlock_rdlock (&rwlock);
}
}
Pthread_rwlock_unlock (&rwlock);
Time_to_exit=1;
Pthread_exit (0);
}
void *thread_function_read_t (void *arg)
{
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.");
printf ("The characters is%s", Work_area);
Pthread_rwlock_unlock (&rwlock);
Sleep (5);
Pthread_rwlock_rdlock (&rwlock);
while (work_area[0] = = ' + ')
{
Pthread_rwlock_unlock (&rwlock);
Sleep (5);
Pthread_rwlock_rdlock (&rwlock);
}
}
Pthread_rwlock_unlock (&rwlock);
Time_to_exit=1;
Pthread_exit (0);
}
void *thread_function_write_o (void *arg)
{
printf ("This is the write thread one try to get lock\n");
while (!time_to_exit)
{
Pthread_rwlock_wrlock (&rwlock);
printf ("This is write thread one.\ninput 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_function_write_t (void *arg)
{
Sleep (10);
while (!time_to_exit)
{
Pthread_rwlock_wrlock (&rwlock);//Get Write lock
printf ("This is write thread two.\ninput some text. Enter ' End ' to finish\n ");
Fgets (Work_area, Work_size, stdin);//write
Pthread_rwlock_unlock (&rwlock);//unlock
Sleep (20);
}
Pthread_rwlock_unlock (&rwlock);//unlock
Pthread_exit (0);
}
Linux read-write lock application Example