Multithreading An example of a mistake

Source: Internet
Author: User
Tags mutex

See Source:

/********* Description: ********* 1. To allow the reader and the writer, as well as the writer and writer of the question to be mutually exclusive to the same data set; ********* 2. Each reader can access the data set at the same time when no write process arrives; ********* 3. Both readers and the writer wait Write on access is preferred. *********/#include<pthread.h>#include<signal.h>#include<stdio.h>//#include "apue.h"#defineR 5//reader NO.#defineY//Reader and writer NO.pthread_mutex_t Critical= Pthread_mutex_initializer;//Protection of critical resource mutexespthread_mutex_t rd = Pthread_mutex_initializer;//Protect Read Mutexpthread_mutex_t WR = Pthread_mutex_initializer;//Protect write Mutexpthread_mutex_t priority = Pthread_mutex_initializer;//read-write MutexintReadcount =0;//Critical ResourcesintWritecount =0;//Critical Resourcesvoid* Reader (void*Arg) {    intn =W; intID = * (int*) Arg;  while(1) {Sleep (rand ()%3); Readcount++;//for street-facing resources, there must be mutually exclusive access, at the same time only one thread is operating.         if(Readcount = =1) {printf ("aaaaaaaaa\n"); Pthread_mutex_lock (&critical); } readcount--; Sleep (rand ()%3); if(Readcount = =0) {printf ("bbbbbbb\n"); Pthread_mutex_unlock (&critical); }} printf ("-----Reader%d has did----, current read count[%d]\n", ID, readcount);}void*writer (void*Arg) {     intn =W;  while(1) {Sleep (rand ()%3); Pthread_mutex_lock (&WR); Pthread_mutex_lock (&critical);//Write threads are also mutually exclusive. printf"\twriter is writing\n"); Pthread_mutex_unlock (&critical); Pthread_mutex_unlock (&WR); } printf ("-----Writer has done-----\ n");}intMainintargcConst Char*argv[]) {     interr;     pthread_t Tid[r], Writertid; intI=0; //for (i; i < W; ++i) {Err = Pthread_create (&tid[i], NULL, Reader, &i); if(Err! =0) {printf ("can ' t create process for reader"); }     //}Err= Pthread_create (&writertid, NULL, writer, (void*) NULL); if(Err! =0) {printf ("can ' t create process for writer"); }      while(1); return 0;}

Compile run:

aaaaaaaaa writer iswritingbbbbbbbaaaaaaaaabbbbbbb writer iswriting writer iswriting writer iswritingaaaaaaaaa writer  is writingbbbbbbb writer iswriting writer iswritingaaaaaaaaabbbbbbbaaaaaaaaa writer iswritingbbbbbbb writer iswritingaaaaaaaaabbbbbbbaaaaaaaaa writer iswritingbbbbbbb writer iswritingaaaaaaaaabbbbbbb

See the red section above and always assume that write will be executed behind BBBB because the lock will not be released until the "BBBBBB" is printed.

However, the output of these writing is actually executed after the "AAAAA" Print is complete and before the lock is acquired, and thecode inside the write thread does not have a sequential relationship with the read thread code. Plus locks allow them to access resources only when they are mutually exclusive and cannot synchronize.

Multithreading An example of a mistake

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.