Vector SemVector with thread security and blocking functions

Source: Internet
Author: User

This is a vector-type SemVector (LINUX platform) with thread security and blocking functions. Welcome to [cpp] // semvector. h # ifndef SEMVECTOR_H _ # define SEMVECTOR_H _ # include <pthread. h> # include <unistd. h> # include <semaphore. h> # include <stdio. h> template <class T> class SemVector {private: T * m_Array; // the maximum length of the int m_Size vector for storing data; // The vector, -int m_MaxSize; pthread_mutex_t m_Mutex; // The mutex sem_t m_Empty; sem_t m_Stored; public: SemVector (int size ); ~ SemVector (); // Add data to the end of the vector. If the vector is full, use the conditional variable to wait for void PushBack (const T & elem); // delete data from the end of the vector, if the vector is empty, use the conditional variable to wait for T PopBack (); // return the element T At (int I); // return the length of the array int Size () {return m_Size;} // whether the returned array is Empty bool Empty () {return (0 = m_Size);} // returns the maximum length of the array int GetMaxSize () {return m_MaxSize ;}; template <class T> SemVector <T >:: SemVector (int size) {m_MaxSize = size; m_Size = 0; m_Array = new T [size]; pthread_mu Tex_init (& m_Mutex, NULL); sem_init (& m_Empty, 0, size); sem_init (& m_Stored, 0, 0);} template <class T> SemVector <T> :: ~ SemVector () {delete m_Array; pthread_mutex_destroy (& m_Mutex); sem_destroy (& m_Empty); sem_destroy (& m_Stored);} template <class T> void SemVector <T> :: pushBack (const T & elem) {sem_wait (& m_Empty); pthread_mutex_lock (& m_Mutex); m_Array [m_Size ++] = elem; # ifdef PRINT_DEBUG printf ("push: % d, size: % d, thread: % lu \ n ", elem, m_Size-1, (unsigned long) pthread_self (); # endif pthread_mutex_unlock (& m_Mutex ); sem_post (& m_Stored);} template <class T> T SemVector <T>: PopBack () {T res; sem_wait (& m_Stored); pthread_mutex_lock (& m_Mutex ); res = m_Array [-- m_Size]; # ifdef PRINT_DEBUG printf ("pop: % d, size: % d, thread: % lu \ n", res, m_Size, (unsigned long) pthread_self (); # endif pthread_mutex_unlock (& m_Mutex); sem_post (& m_Empty); return res;} template <class T> T SemVector <T >:: At (int I) {T res; pthread_mutex_unlock (& m_Mutex); if (I <m_Size) res = m_Array [I]; pthread_mutex_unlock (& m_Mutex); return res ;}# The following is a test example: [cpp] // test example # define PRINT_DEBUG # include "semvector. h "SemVector <int> g_tvec (10); int g_value = 1; const int TCOUNT = 100; // Producer thread void * Producer (void * arg) {while (1) {g_tvec.PushBack (g_value ++) ;}// consumer thread void * Customer (void * arg) {while (1) {g_tvec.PopBack ();}} // main function int main () {pthread_t proid [TCOUNT], cusid [TCOUNT]; // generates 10 producers and 10 Consumers for (int I = 0; I <TCOUNT; I ++) {pthread_create (& proid [I], NULL, Producer, NULL); pthread_create (& cusid [I], NULL, Customer, NULL );} for (int I = 0; I <TCOUNT; I ++) {pthread_join (proid [I], NULL); pthread_join (cusid [I], NULL );} return 0 ;}

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.