Read/write Lock

Source: Internet
Author: User

1 /*2 * Reader Writer's question3 * Problem Description4 *) allows multiple readers to access a data at the same time, but only one writer is allowed to write the data5 when no reader is reading the data, the writer is allowed to write the data6 When a writer is writing data, the reader is not allowed to read and write Data .7 *history:8 *2010/03/16 Huangwei First release9 */Ten  One#include <stdio.h> A#include <unistd.h> -#include <pthread.h> -  the #defineReader_max 3/* Maximum number of people allowed to read data together */ - #defineWriter_max 2/* Maximum number of people to write data */ -  -pthread_rwlock_t Rw_lock;/*read/write lock*/ +  - /** + *reader_thread-Analog reader for data reading A * @arg: Not used at */ -  - void* Reader_thread (void*Arg) - { -      while(1) { -         /*try to add a read-only lock*/         in         if(Pthread_rwlock_tryrdlock (&Rw_lock)) { -             /*read-only lock not successful, print information, 1 seconds to continue to try*/ toprintf"read \ t user%u is temporarily unable to read data. \ n", (unsignedint) pthread_self ()); +Sleep1); -}Else { the             /*the read-only lock succeeds to show which reader is reading the data*/ *printf"read \ t user%u is reading data. \ n", (unsignedint) pthread_self ()); $Sleep1);/*readers are reading data*/Panax Notoginsengprintf"read \ t user%u read the data. \ n", (unsignedint) pthread_self ()); -Pthread_rwlock_unlock (&rw_lock);/*unlocking a read-only lock*/ theSleep2);/*take some time off before attempting to read data*/ +         }     A     } the } +  - /** $ *writer_thread-Write data to the analog writer $ * @arg: Not used - */ -  the void* Writer_thread (void*Arg) - {Wuyi      while(1) { the         /*attempt to write lock*/ -         if(Pthread_rwlock_trywrlock (&Rw_lock)) { Wu             /*write lock not successful, output information, 2 seconds before continuing to try*/ -printf"writer%u is temporarily unable to write data \ n", (unsignedint) pthread_self ()); AboutSleep2); $}Else { -             /*The write lock is successful, showing where the writer is writing the data*/ -printf"Writer %u is writing data. \ n", (unsignedint) pthread_self ()); -Sleep2);/*Writer is writing data*/ Aprintf"Writer %u has finished writing the data. \ n", (unsignedint) pthread_self ()); +Pthread_rwlock_unlock (&rw_lock); theSleep3);/*after a period of rest, try to write the data again .*/ -         } $     } the } the  the intMainintargcChar*argv[]) the { -pthread_t Reader, writer;/*record the thread number of the reader and the writer*/ in      the     inti =0;/*Loop Variable*/ the      AboutPthread_rwlock_init (&rw_lock, NULL);/*initializing a read-write lock*/ the      the     /*Create a Reader_max reader*/ the      for(i =0; i < Reader_max; i++)     +Pthread_create (&reader, NULL, (void*) Reader_thread, NULL); -  the     /*Create a Writer_max reader*/Bayi      for(i =0; i < Writer_max; i++) thePthread_create (&writer, NULL, (void*) Writer_thread, NULL); the      -SleepTen);/*program runs after 10 seconds to exit*/ -      the     return 0; the}

Program Run Result:

1 Reader 3434592000 is reading the data.2 Reader 3426199296 is reading the data.3 Reader 3417802496 is reading the data.4 writer 3409409792 cannot write data temporarily5 writer 3401017088 cannot write data temporarily6 Reader 3434592000 reading data is complete.7 Reader 3417802496 reading data is complete.8 Reader 3426199296 reading data is complete.9 Writer 3409409792 is writing data.Ten writer 3401017088 cannot write data temporarily One Reader 3434592000 is temporarily unable to read data. A Reader 3426199296 is temporarily unable to read data. - Reader 3417802496 is temporarily unable to read data. - Writer 3409409792 Write data complete. the Writer 3401017088 is writing data. - Reader 3434592000 is temporarily unable to read data. - Reader 3417802496 is temporarily unable to read data. - Reader 3426199296 is temporarily unable to read data. + Reader 3426199296 is temporarily unable to read data. - Reader 3417802496 is temporarily unable to read data. + Reader 3434592000 is temporarily unable to read data. A Writer 3401017088 Write data complete. at Reader 3426199296 is reading the data. - Reader 3417802496 is reading the data. - Reader 3434592000 is reading the data. - writer 3409409792 cannot write data temporarily - Reader 3426199296 reading data is complete. - Reader 3434592000 reading data is complete. in Reader 3417802496 reading data is complete. - Writer 3409409792 is writing data. to writer 3401017088 cannot write data temporarily + Reader 3417802496 is temporarily unable to read data. - Reader 3426199296 is temporarily unable to read data. theReader 3434592000 is temporarily unable to read data.

Read/write Lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.