13.1 Multi-threaded operation shared memory, producer consumer model, multi-threaded server framework

Source: Internet
Author: User
Tags mutex

The producer consumer model is as follows:

The procedure is as follows:

1#include <unistd.h>2#include <sys/types.h>3 4#include <stdlib.h>5#include <stdio.h>6#include <errno.h>7#include <string.h>8 9#include <pthread.h>Ten  One intG_count =0; A  - intNnum, Nloop; -  the //Defining Locks -pthread_mutex_t Mutex =Pthread_mutex_initializer; -  - //define conditions and initialize +pthread_cond_t my_condition=Pthread_cond_initializer; -  + #defineCustom_count 2 A #defineProduct_count 4 at  -  -   //int Pthread_mutex_lock (pthread_mutex_t *mutex); -   //int Pthread_mutex_trylock (pthread_mutex_t *mutex); -    //int Pthread_mutex_unlock (pthread_mutex_t *mutex); -  in /* - int pthread_cond_timedwait (pthread_cond_t *restrict cond, to pthread_mutex_t *restrict Mutex, + const struct TIMESPEC *restrict abstime); - int pthread_cond_wait (pthread_cond_t *restrict cond, the pthread_mutex_t *restrict mutex); *               */ $ Panax Notoginseng //POSIX line Libraries's function line libraries - void*consume (void*Arg) the { +      A     intInum =0; theInum = (int) arg; +      while(1) -     { $Pthread_mutex_lock (&mutex); $printf"consum:%d\n", inum); -          while(G_count = =0)//while waking up need to re-judge whether the condition G_count is satisfied, if not satisfied, wait again -         { theprintf"consum:%d started to wait \ n", inum); -Pthread_cond_wait (&my_condition, &mutex);//API has done three things//pthread_cond_wait false AwakeWuyiprintf"consum:%d woke up \ n", inum); the         } -      Wuprintf"consum:%d Consumer Products begin\n", inum); -g_count--;//Consumer Products Aboutprintf"consum:%d Consumer Products end\n", inum); $          -Pthread_mutex_unlock (&mutex); -      -Sleep1); A     } +  thePthread_exit (0); -  $ }  the  the //Producer Threads the // the void*produce (void*Arg) - { in     intInum =0; theInum = (int) arg; the      About      while(1) the     { the      the     /* + //Because it is a lot of producers call produce, to protect the global variable g_count, so locking - Pthread_mutex_lock (&mutex); the if (G_count >)Bayi         { the printf ("produce:%d products too much, need control, hibernate \ n", inum); the Pthread_mutex_unlock (&mutex); - sleep (1); - continue; the         } the Else the         { the Pthread_mutex_unlock (&mutex); -         } the         */ the      thePthread_mutex_lock (&mutex);94printf"Product Quantity:%d\n", g_count); theprintf"produce:%d Production Products begin\n", inum); theg_count++; the         //as long as I produce a product, I tell consumers to spend98printf"produce:%d Production Products end\n", inum); About          -printf"produce:%d conditions Signal begin\n", inum);101Pthread_cond_signal (&my_condition);//notifications, threads waiting on a condition102printf"produce:%d conditions Signal end\n", inum);103         104Pthread_mutex_unlock (&mutex); theSleep1);106     }107     108Pthread_exit (0);109  the } 111  the //Conclusion: The results of return ARG and pthread_exit () can be used to get pthread_join.113 intMain () the { the     inti =0; thepthread_t tidarray[custom_count+product_count+Ten];117     118     //Create a consumer thread119      for(i=0; i<custom_count; i++) -     {121Pthread_create (&tidarray[i], NULL, consume, (void*) (i);122     }123     124Sleep1); the     //Create a line process126      for(i=0; i<product_count; i++)127     { -Pthread_create (&tidarray[i+custom_count], NULL, Produce, (void*) (i);129     } the     131      the     133      for(i=0; i<custom_count+product_count; i++)134     {135Pthread_join (Tidarray[i], NULL);//wait for thread to end ... 136     }137     138     139printf"The process has to end 1233\n"); $     141     return 0;142}

The results of the implementation are as follows:

13.1 Multi-threaded operation shared memory, producer consumer model, multi-threaded server framework

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.