Producer Consumer multi-thread demo program in linux

Source: Internet
Author: User
Introduction to the basic API of the multi-thread demo program for producer consumer in linux: intpthread_create (pthread * thread, pthread_attr_t * attr, void * (* start_routine) (* void ), void * arg); the first parameter is the second parameter pointing to the thread... introduction to the basic API of the multi-thread demo program for producer consumer in linux: int pthread_create (pthread * thread, pthread_attr_t * attr, void * (* start_routine) (* void ), void * arg );
The first parameter points to the second parameter of the thread to set the thread attributes. The third parameter is the starting address of the thread running function. The fourth parameter is the parameter of the running function. Pthread_attr_t * and void * arg can be set to NULL; int pthread_join (pthread_t thread, void ** retval); the first parameter is the waiting thread identifier, the second parameter is a user-defined pointer used to store the return values of the waiting thread. This function is a thread-blocking function. the Function called will wait until the end of the thread. when the function returns, it will be reclaimed by the resources of the waiting thread. If the execution is successful, 0 is returned. if the execution fails, an error number is returned. Www.2cto.com mutex initialization: Function: int pthread_mutex_init (pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restrict attr); this function dynamically creates mutex locks, the attr parameter specifies the attribute for creating a mutex lock. If the attr parameter is null, the default mutex lock attribute is used. the default attribute is quick mutex. The mutex lock attribute is specified when the lock is created. there is only one lock type attribute in the LinuxThreads implementation. different lock types are different when trying to lock a locked mutex lock. After successful completion, zero is returned, and any other returned values indicate an error. Lock: int pthread_mutex_lock (pthread_mutex_t * mutex); return value: zero is returned after successful completion. Any other return values indicate an error. If any of the following conditions occurs, the function fails and the corresponding value is returned. Unlock: int pthread_mutex_unlock (pthread_mutex_t * mutex); release the mutex lock, which exists in pairs with pthread_mutex_lock. The data type of semaphores www.2cto.com is sem_t, which is essentially a long integer. The sem_init () function is used to initialize a semaphore. Int sem_init (sem_t * sem, int pshared, unsigned int value); initializes the Semaphore specified by sem, sets its sharing options, and specifies an integer type initial value. The pshared parameter controls the semaphore type. if the pshared value is 0, it indicates that the semaphore is shared within the process. Otherwise, the semaphore can be shared between processes. Sem_post (sem_t * sem) is used to increase the semaphore value. When a thread is blocked on this semaphore, calling this function can wake up the blocked thread. Sem_wait (sem_t * sem), indicating waiting for the semaphore. if the Semaphore cannot be obtained, it is blocked. after blocking is removed, the sem value is reduced by one, indicating that the public resources are reduced after use. Sem_destroy (sem_t * sem) is used to release semaphores sem. Code: # include # Include # Include # Include # Include Using namespace std; # define MAX_SIZE 100 vector Buffer; // share data buffer pthread_mutex_t mutex; // buffer access mutex sem_t full; // There is a data semaphore sem_t empty in the buffer; // buffer idle data semaphore void * producer (void *) // producer {for (int I = 0; I <MAX_SIZE; I ++) {sem_wait (& empty ); // if the buffer zone has free space, put data. Otherwise, the block pthread_mutex_lock (& mutex); buffer. push_back (I); cout <"producer:" < : Iterator iter = buffer. begin (); int value = * iter; buffer. erase (iter); cout <"consumer:" <
Related Article

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.