Linux Component Package (v) Workshop package

Source: Internet
Author: User

After we have encapsulated the buffer, but do not know how many threads, a thread of the de-association buffer is too cumbersome.

So, can we say that buffer is encapsulated with a queue of threads?

Since the thread cannot be stored in the vector, we should add the corresponding thread * in the vector so that the encapsulation can be completed.

The declaration code is as follows:

1 #ifndef Workshop_h2 #defineWorkshop_h3#include"NonCopyable.h"4#include"Buffer.h"5#include <vector>6 classProducer;7 classConsumer;8 classworkshop:noncopyable9 {Ten  Public: One WorkShop (size_t buffersize, size_t producersize, size_t consumersize); A  -~WorkShop (); -     voidstartwork (); the Private: -  - size_t _buffersize; -  + Buffer _buffer; - size_t _producersize; + size_t _consumersize; A  atStd::vector<producer *>_producers; -Std::vector<consumer *>_consumers; - }; -  -  - #endif/*workshop_h*/
View Code

Next, we need to implement the function in the declaration, in the constructor, we need to initialize the pointers in two vectors, initialize the size of the vector to the size of the vector we get, initialize each pointer to null, and then We need to new a thread object for each pointer, so the initialization is done.

Similarly, when we deconstruct, we should also delete the pointer in the vector.

The CPP code is as follows:

1#include"WorkShop.h"2#include"Producer.h"3#include"Consumer.h"4 5 Workshop::workshop (size_t buffersize, size_t producersize, size_t consumersize)6 : _buffersize (buffersize),7 _buffer (_buffersize),8 _producersize (producersize),9 _consumersize (consumersize),Ten _producers (_producersize, NULL), One _consumers (_consumersize, NULL) A { -      for(Std::vector<producer *>::iterator iter =_producers.begin (); -Iter! =_producers.end (); the++iter) -*iter =NewProducer (_buffer); -  -      for(Std::vector<consumer *>::iterator iter =_consumers.begin (); +Iter! =_consumers.end (); -++iter) +*iter =NewConsumer (_buffer); A } at  -workshop::~WorkShop () - { -      for(Std::vector<producer *>::iterator iter =_producers.begin (); -Iter! =_producers.end (); -++iter) inDelete *iter; -  to      for(Std::vector<consumer *>::iterator iter =_consumers.begin (); +Iter! =_consumers.end (); -++iter) theDelete *iter; * } $ Panax Notoginseng voidworkshop::startwork () - { the      for(Std::vector<producer *>::iterator iter =_producers.begin (); +Iter! =_producers.end (); A++iter) the(*iter)start (); +  -      for(Std::vector<consumer *>::iterator iter =_consumers.begin (); $Iter! =_consumers.end (); $++iter) -(*iter)start (); -  the      for(Std::vector<producer *>::iterator iter =_producers.begin (); -Iter! =_producers.end ();Wuyi++iter) the(*iter)join (); -  Wu      for(Std::vector<consumer *>::iterator iter =_consumers.begin (); -Iter! =_consumers.end (); About++iter) $(*iter)join (); -}
View Code

In the Startwork function, we first call the start () function on each thread's pointer, turn the thread on, and then call the join () function to close each thread when we exit the process.

The test code is as follows:

1#include"WorkShop.h"2 3 intMainintargcConst Char*argv[])4 {5WorkShop A (5,Ten, the);6 a.startwork ();7     return 0;8}
View Code

When compiling, be aware that the individual CPP files that you need are listed in the previous articles, please download your own attempt.

Linux Component Encapsulation (v) Workshop encapsulation

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.