Post Page Advertisement [Top]

Hey folks,

I'm involved at work in converting a Java application to C++. I won't go into the details to don't get you bored but I run into the problem of how to get the Java BlockingQueue functionalities in C++, that is, a thread-safe queue that makes consuming threads wait if the queue is empty. Since I'm using Boost libraries to make this as interoperable as possible, I first had a glance at Boost data structures but to no avail, none seemed to fit my needs.

After some digging I decided to implement one of my own, using a mix of STL queue and Boost mutex to make it workConclusion. So let's get our hand dirty :). At first I thought my queue should have the following methods:

  • pop
  • push
  • size
  • empty
  • toString (to get a contents string representation)
Looking for some info on how to do this, I stumbled upon this excelent article (thanks to author Anthony Williams), which guide me in the right direction. So after careful reading I started typing some code. Let us see the class declaration:

#include <queue>
#include <sstream>
#include <boost/thread/mutex.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/any.hpp>
#include <boost/thread/condition_variable.hpp>


#ifndef SYNCHRONIZEDQUEUE_DEF
#define SYNCHRONIZEDQUEUE_DEF

template<class T>
class SynchronizedQueue
{

private:
 ///The queue itself
 std::queue<T> sQueue;
 ///A mutex object to control access to the std::queue
 boost::mutex io_mutex_;
 ///A variable condition to make threads wait on specified condition values
 boost::condition_variable waitCondition;

public:
 //default Constructor
 SynchronizedQueue();
 //destructor
 virtual ~SynchronizedQueue();
 void push(T& element);
 bool empty();
 bool pop(T& element);
 unsigned int sizeOfQueue();
 void waitAndPop(T& element);
 std::string toString();
  
};

    As you can see above, our SynchronizedQueue object will have the typical push, pop, empty and size methods. Pop method receives a reference to the T object, instead of returning a reference, which should be the logical thing, I'll explain the reason for this later. Besides we have a waitAndPop method, I will go into the details later, but just let it be known that it will be used to make consuming threads wait based on certain criteria.

    Attributes

    Regarding the attributes, the SyncrhonizedQueue wraps a standard std::queue:

    std::queue<T> sQueue;
    

    This is actually the queue that will store the data (T type). I specified I want copies of the objects (T) instead of holding pointers, the reason behind this is that I don't have control of the objects inserted, so if some are deleted outside the scope of the queue, I'd have bad references. As a standard queue, it is a FIFO structure, so first input is first output, exactly what I needed for my purposes. Next attribute is a boost::mutex that will be used to define protected regions, thus only one thread can access such regions.

    boost::mutex io_mutex_;
    

    Lastly, we need a wait condition. In some cases it can be interesting to make consuming threads wait until the queue has some elements. For this purpose, we need a boost::condition_variable.

    boost::condition_variable waitCondition;
    


    Methods

    Since this is a template class, everything goes into a the header file, or independent header files per method (that's what I normally do) and then use includes to insert the code in the main header. Anyway, the constructor and destructor have nothing of special:

    template<class T>
    //default constructor
    SynchronizedQueue<T>::SynchronizedQueue(){}
    
    
    template<class T>
    //default desconstructor
    SynchronizedQueue<T>::~SynchronizedQueue(){}
    

    push

    Below you can see the implementation of the push method:

    template<class T>
    void SynchronizedQueue<T>::push(T element)
    {
        //try to lock the mutex
        boost::mutex::scoped_lock lock(io_mutex_);
        //insert the element in the FIFO queue
        SynchronizedQueue::sQueue.push(element);
        //Now we need to unlock the mutex otherwise waiting threads will not be able to wake and lock the
        //mutex by time before push is locking again
        lock.unlock();
        //notifiy waiting thread they can pop/push now
        waitCondition.notify_one();
    }
    

    The first important thing to mention is that I'm passing a reference of the object to be inserted. I could pass a copy, but then, since the elements of the queue are declared as T, it will double-copy when calling the push function: one when being called, one when inserting the element. Thus, passing a reference avoids unnecessary copies. First thing we do is locking the mutex, so only one thread can execute the following code. Once locked, we push the element in the queue, unlock the mutex and notify one thread that it can unfroze from waiting and consume an element from the queue. The reason unlocking the mutex before notifying is that if we notify before unlocking, the notified thread will immediately block again since the mutex is still locked, so is better to do it afterwards.

    empty

    Nothing special about this method, besides the fact that we need to protect the access to the std::queue.empty() method, in order to avoid race conditions.

    template<class T>
    bool SynchronizedQueue<T>::empty()
    {
        //try to lock the mutex
        boost::mutex::scoped_lock lock(io_mutex_);
        return SynchronizedQueue::sQueue.empty();
    }
    

    pop

    Now the funny part, this method returns true/false depending whether successful extraction from the queue was possible. The objective of this is that, if you don't want consumer threads to keep waiting blocked while the queue is empty, then use this method. It will try to pop the element and copy it in the passed reference and return true, if unsuccessful it will return false, but the thread will be able to do something else instead of blocking itself waiting.

    template<class T>
    bool SynchronizedQueue<T>::pop(T& element)
    {
    
         //try to lock the mutex
         boost::mutex::scoped_lock lock(io_mutex_);
    
         //ask if the queue is empty
         if (SynchronizedQueue::sQueue.empty())
         {
     return false;
         }
    
         //get the last inserted element
         element = SynchronizedQueue::sQueue.front();
         //remove the last inserted element, since we have a copy in element
         SynchronizedQueue::sQueue.pop();
    
         //no need to unlock the mutex, it will get unlocked when the function ends
         return true;
    
    };
    

    Once again, first thing to do is lock the mutex, then check if the queue is empty. If true then return false, otherwise make a copy of the last inserted element in the queue (front) and then remove it by calling std::queue.pop() method. In this case there is no need to call for unlocking the mutex, since there are no waiting threads blocked waiting for elements being popped, and the lock will automatically unlock when it reaches the end of the method.

    waitAndPop

    This method is very similar to the previous one, albeit the main difference is that it will make threads block waiting as long as the queue is empty.

    template<class T>
    void SynchronizedQueue<T>::waitAndPop(T& element)
    {
    
        //try to lock the mutex
        boost::mutex::scoped_lock lock(io_mutex_);
        //while the queue is empty, make the thread that runs this wait
        while(SynchronizedQueue::sQueue.empty())
        {
     waitCondition.wait(lock);
        }
    
        //when the condition variable is unlocked, popped the element
        element = SynchronizedQueue::sQueue.front();
    
        //pop the element
        sQueue.pop();
    
    };
    

    As you can see, right after locking the mutex, we check if the queue is empty. If so, we call the wait method. This effectively makes the thread wait until other thread calls the notify_one or notify_all methods. If you can recall, this was done in the push method after inserting an element. When a blocking thread is notified it will check once again if the queue is empty. Since its not, it can continue. The rest is just like in the pop method.

    sizeOfQueue

    Self-explanatory method, just returns current queue size.

    template<class T>
    unsigned int SynchronizedQueue<T>::sizeOfQueue()
    {
     //try to lock the mutex
     boost::mutex::scoped_lock lock(io_mutex_);
     return SynchronizedQueue::sQueue.size();
    
    };
    

    toString

    I added this method for your convenience, it tries to get a string representation of the queue contents. Since the elements in the queue can be mostly *anything*, the only reasonable output would be the name of the class. To do this boost::any is used.

    template<class T>
    std::string SynchronizedQueue<T>::toString()
    {
        std::stringstream os;
    
        //make a copy of the class queue, so we dont care about mangling with threads
        std::queue<T> copy = SynchronizedQueue::sQueue;
    
        //check the queue is not empty
        if (!copy.empty())
        {
         int counter = 0;
    
         os << "Elements in the Synchronized queue are as follows:" << std::endl;
         os << "**************************************************" << std::endl;
    
         while (!copy.empty())
     {
          //get the first element in the queue
      boost::any object = copy.front();
      std::string b = "Element at position " + boost::lexical_cast<std::string>(counter) + " is: ";
      b.append("[");
      b.append(object.type().name());
      b.append("]\n");
          os << b;
          //remove the element in the queue
          copy.pop();
     }
         return os.str();
        }
        else
        {
         os << "Queue is empty";
         return os.str();
        }
    }
    

    First thing to notice is that I make a copy of the queue before iterating over the elements. I did this so there was no interference with producer and consumer threads, although I'm not sure if the copy process itself could cause some problems. I recon it should be protected with the mutex but heh, I just made this method for debugging and testing purposes, it shouldn't be used in a production environment. Probably you also realized that I'm not using iterators to loop over the queue elements, mostly because queues are not meant to iterate. Thus, I made a simple front-pop as long as there are elements approach. Probably not optimal at all, but as I said this is only for testing purposes.

    You can download the source from here.

    Conclusion

    Most surely there are better solutions which are already implemented and well tested better than this, but I liked the process of making this as my first step in the thrilling world of multi-threading programming. I tried this and I can tell you that it seems to work nice after some testing. If you want your threads to wait when reading, use waitAndPop, if not use pop, that easy. Besides, you can apply this approach to any other STL container you can think of.

    Stay tuned for the next article on this subject in which a test program for this queue is explained.

    References

    68 comments:

    1. Hi,

      I was looking for a safe thread queue like this and you post and code looks like what i was looking for. Thank you for sharing the logic and the code. Did you ever had a chance to publish a test program for using this queue ?

      Thanks

      Jeff

      ReplyDelete
    2. I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. disinfecting equipment

      ReplyDelete
    3. I am also writer. I read this entire article. Truly you have shared wonderful information. Keep it up. Roku Vs Firestick

      ReplyDelete
    4. Truly quite intriguing post. I was searching for this sort of data and delighted in perusing this one. Continue posting. Much obliged for sharing slot pragmatic bet murah

      ReplyDelete
    5. THANKS FOR THIS AMAZING ARTICLE صيانة غسالات
      THANKS FOR THIS AMAZING ARTICLE فني غسالات
      THANKS FOR THIS AMAZING ARTICLE رقم تصليح غسالات

      ReplyDelete
    6. i've been searching for data on this theme for some time. I'm glad this one is so extraordinaryhow to manifest money fast

      ReplyDelete
    7. laptop tray for bed
      The miracle thoughts are acceptable to look from the correct assistance line. I permit the harmony end to give the correct source on this perfect substance. A debt of gratitude is in order for this pleasant chance Buy TV Dinner Tray Online

      ReplyDelete
    8. Our 14x7 PC fix administrations are accessible all round the year to guarantee our clients appreciate continuous administrations at sensible costs. Our on location Computer Repair administration is the most ideal alternative and takes into account the necessities of clients. This likewise encourages them in setting aside their time and cash. By diagnosing what inconvenience your PC is having, the master experts of Digmanindia can in a flash fix any PC related issues.تصليح كمبيوتر في البيت

      ReplyDelete
    9. diminishes energy misfortune because of direct dampness assimilation. This is especially significant for homes with numerous unfinished plumbing spaces. Energy proficiency is a vital piece of building development, and unfinished plumbing space exemplification can permit contractual workers to expand energy productivity through anticipation of dampness move. crawl space encapsulation cost

      ReplyDelete
    10. Write your name and your lovers name on beautiful Valentine’s Made For Each other image to wish valentine’s day in a heart touching way. Valentine’s Day Ecards refer to a variety of digital greeting cards that are exchanged among friends on social media platforms in celebration of the annual holiday, most notably on Tumblr and Pinterest.
      Valentine’s Made For Each other
      Lovers Hearts Wishes
      Couple Quotes on Valentine
      Valentine Quotes for My Love

      ReplyDelete
    11. The way to progress at online media is only that it's an ideal opportunity to expand your natural lift by improving the quantity of devotees on your page that assists with incorporating your item into a brand classification. شراء متابعين تويتر

      ReplyDelete
    12. The way to progress at online media is only that it's an ideal opportunity to expand your natural lift by improving the quantity of devotees on your page that assists with incorporating your item into a brand classification. شراء متابعين تويتر

      ReplyDelete
    13. The way to progress at online media is only that it's an ideal opportunity to expand your natural lift by improving the quantity of devotees on your page that assists with incorporating your item into a brand classification.شراء متابعين تويتر

      ReplyDelete
    14. The Most Popular Advertising Categories for Classified Ads Portal www.anundos.com

      ReplyDelete
    15. Situated in Manchester, we supply GRP grinding non slip flooring built from fiberglass, as an ideal and practical option in contrast to conventional grinding materials like wood, gentle steel and tempered steel.
      GRP Grating Systems
      Riser Grating
      GRP Mini Mesh Grating
      GRP Grating Systems
      Riser Grating

      ReplyDelete
    16. We strive with all our energies to reach the best time for delivery and are proud that we were able to break this barrier and deliver many of our customers' requests within a few seconds of the request, and a technical support team was provided 24/7 to solve the problems you face during the execution of the requests.internet cafe software



      how to start an internet cafe



      riverslots



      how to start an internet cafe



      riverslots

      ReplyDelete
    17. The appearance of its first worldwide concert – Kala – in 2018 denoted the start of another section for Albania. DJs Ben Böhmer and Moon Boots give within track on why Albania is the most sultry new objective for music sweethearts.
      hitet 2021 balada
      muzik shqip 2021 hitet e reja

      ReplyDelete
    18. Basic personal auto insurance is mandated by most states and provides you with some financial protection in case of an accident. But is it enough? What are the options? Learn how car insurance works and what types of coverage are available.seguros
      melhor seguro

      ReplyDelete
    19. The beauty of design is its ability to morph, adapt, and incorporate influences that enhance style and function. The interior design trends 2020 exemplify this in a way that tells a story.2020 trends in interior design
      2020 trends in interior design

      ReplyDelete
    20. Do you want to make improvements or repairs to your home's plumbing? Would you like to know how to deal with an emergency? Dealing with issues related to plumbing can be complicated. Whether you are doing it yourself or hiring someone, there are a lot of things you need to be aware of. Here are some useful facts and tips.
      Drain Services Mississauga
      Drain Snaking Mississauga
      Emergency Plumber Mississauga
      Plumbing Services Near Me Mississauga

      ReplyDelete
    21. We strive with all our energies to reach the best time for delivery and are proud that we were able to break this barrier and deliver many of our customers' requests within a few seconds of the request, and a technical support team was provided 24/7 to solve the problems you face during the execution of the requests.

      work anniversary meme
      feels good meme
      bob ross meme
      and just like that meme
      maury meme
      fuck me meme
      i
      aliens meme generator
      frozen meme
      is this a pigeon meme template

      ReplyDelete
    22. Milwaukee is the largest city in Wisconsin and has by far the most vehicle sales in the State. Most people have purchased a vehicle and went home to enjoy it for months if not years to come never imagining needing to call a lemon law lawyer in Milwaukee.
      lemon law lawyers Milwaukee Wisconsin
      lemon law attorneys Milwaukee WI
      Milwaukee lemon law lawyer
      Milwaukee lemon law attorney
      Wisconsin lemon law lawyers
      lemon law lawyers Wisconsin
      lemon law lawyers Racine Wisconsin
      lemon law attorneys Milwaukee WI
      lemon law lawyers Wisconsin
      lemon law lawyers Wisconsin

      ReplyDelete
    23. With our fake instagram generator you can easily customize and create an instagram post in few seconds. You can upload your photo as profile picture or of other person or celebrity depending on your prank. Type the text you want, the image you want to share, mention people in the post, add hashtags and more to make it look realistic . You can also have a live preview of the changes you make before you download. Share it on social media to make a fun viral fake Instagram post screenshot. Therefore start using the Best Fake Instagram Generator Ever. Please note that these our fake instagram account generator is not associated with Instagram. Fake Instagram Template
      Instagram template
      Instagram post template
      Instagram mockup
      Edit instagram post
      Instagram layout
      Instagram templates
      Instagram profile template
      Fake Instagram Template
      Instagram template
      undefined

      ReplyDelete
    24. Antonius Marinus Hendricus Maria "Team Tonnie" Dirks (born 12 February 1961 in Zeeland, North Brabant) is a Dutch former long distance runner. He competed internationally on the track, road, and also in cross country competitions.
      He ran in the Marathon at the 1991 World Championships in Tokyo, finishing in 16th place with a time of 2:22:17. That year he also competed in the IAAF World Cross Country Championships, but he only managed to finish in 29th position. The following year he competed at the 1992 Summer Olympics, but he did not finish in the Olympic Marathon race. At the Amsterdam Marathon in 1993, he took second behind Kenichi Suzuki with a time of 2:12:27

      team tonnie dirks
      hardloopteam
      team tonnie dirks
      hardloopteam
      team tonnie dirks

      ReplyDelete
    25. Our Fake Tweet Generator provides you exact mockup of Twitter Tweet. Our Tweet Generator is updated with Twitter’s current look and design. Celebrities and Media use our Twitter Generator to make fake tweets to get viral on social media. We have made Tweets Template according to modern look of Twitter Tweets. So you can prank your friends by creating hilarious and Funny Twitter Posts with our Tweet Generator.
      fake tweet generator
      tweet generator
      fake tweets
      fake tweet maker
      fake twitter generator
      fake tweet generator
      tweet generator
      fake tweets
      fake tweet maker
      fake twitter generator

      ReplyDelete
    26. Per ju shqiptaret qe jetoni dhe veproni ne Franc dhe Zvicer me vitet te tera, kam realizuar playlisten me kenge shqip ne Youtube me titull Musique Albanaise 2021 - Chansons Albanaise 2021 qe ka ne mbrendesi hitet e reja shqip nga me te mirat me musique albanaise. Permes kesaj playliste ne Youtube deshiroj tua mundesoj te keni edhe ju mundesin te degjoni muzik shqip apo chanson albanaise nga me te degjumet gjate keti viti. I keni ne sherbim hitet me te reja musique albanais nga reperet e kengetaret e njohur te muzikes Shqip. Mund ta vlersoni permes pervojes tuaj playlist shqip sipas shijes te stafit ton qe kan radhitur sipas degjueshmeris. Ka ne vete dhe kenge qe jan te realizuar ne France dhe Zvicer te kombinuar ne dy gjuhe nga reperet "rap albanais" Shqiptare te cilet jetojn ne keto vende.
      chanson albanais
      rap albanais
      music albanaise
      albanie musique
      musique albanaise 2021
      chanson albanais

      ReplyDelete
    27. Yoni oil is a home grown mix that vows to support vaginal wellbeing, improve skin versatility and give cell reinforcement assurance, in addition to other things.
      herbal Yoni Soap
      Aloe Vera feminine wash
      Warm womb tea
      yoni steam herbs
      yoni Essential oil

      ReplyDelete
    28. Utilize most recent Grumpy Cat Memes layout to make a Grumpy Cat Meme in a moment or two. Make and offer your images on the web.
      Big Lebowski Memes
      The Dude Abides
      Bernie Sanders Meme
      Bernie Sanders Memes
      Bernie Meme
      Fortnite Meme
      Fortnite Memes
      Memes Fortnite
      Grumpy Cat Memes
      Grumpycat

      ReplyDelete
    29. Utilize most recent Dance Meme format to make a Dance Memes in short order. Make and offer your images on the web.
      Nobody Memes
      Nobody: Meme
      Blank Meme
      Nobody Me Meme
      No One: Meme
      Dance Meme
      Dancing Meme
      Mom Memes
      Mom Meme
      Funny Mom Memes

      ReplyDelete
    30. Meetings with 10 Brazilians who live in the area of Ontario, yet outside. Toronto. This arrangement of digital broadcasts centers around close to home encounters and viewpoints, showing the qualities and benefits of every area regarding openings, work, quality, and average cost for basic items.
      Top brazilian magazines in Toronto
      Brazilian magazines in Toronto
      best brazilian magazines in Toronto
      Best portuguese language magazines in Toronto
      Top portuguese language magazines in Toronto
      Portuguese language magazines in Toronto
      Brazilian magazines in Canada
      Best canadian online magazines
      Top canadian online magazines
      Canadian online magazines

      ReplyDelete
    31. Utilize most recent Mesothelioma Meme layout to make an If You Or A Loved One Meme in a moment or two. Make and offer your images on the web.
      You Got Me There Meme
      We Got This Meme
      Got Memes
      I Got This Meme
      You Got It Meme
      You've Got This Meme
      I've Got This
      You Got It Dude Meme
      You Got That Meme
      Got It Meme

      ReplyDelete
    32. There's a motivation behind why cashmere sweaters regularly cost a chunk of change. The way toward making these comfortable clinchers is a serious, tedious interaction, as Business Insider featured in the wake of visiting a cashmere factor a year ago.
      cashmere wrap cardigan
      long cashmere cardigan
      gray cashmere cardigan
      cashmere hooded cardigan
      cashmere cardigan grey
      cashmere sweater
      womens turtleneck sweater
      womens turtleneck
      cashmere jumper
      women cashmere sweater

      ReplyDelete
    33. Project Chaiwala is a homegrown tea concept that was founded in 2017. Bringing a nostalgic charm and traditional recipes to a modern setting, Project Chaiwala celebrates the Chai culture every day.
      chai dubai
      karak chai dubai
      kashmiri chai dubai
      matcha tea dubai

      ReplyDelete
    34. tài khoản grammarly premium is the most popular grammar checker available today. Grammarly is useful for both native English speakers and those who don't often write in the English language. The Grammarly Premium Account review explores how it can help avoid spelling and grammar errors in English. Here at professorvn.net, you can mua tài khoản grammarly prmium on very cheap price.
      tài khoản grammarly premium
      grammarly premium giá rẻ
      mua tài khoản grammarly prmium
      bán tài khoản grammarly prmium
      mua grammarly premium
      tài khoản grammarly premium

      ReplyDelete
    35. The best game provider website, online slots, fish shooting games, casinos, open an experience that you will be shocked by. Give away free credits from slotxo, a unique entertainment center, online slots, enjoy games, bounce, fire, games. Cards also have fish shooting to enjoy.
      slotxo

      ReplyDelete
    36. Buff Orpington chickens are famous patio chickens. The chicken was developed to be a double-purpose chicken with maximum potential; they are bigger and more useful egg layers.

      Buff Orpington Chickens

      Buff Orpington Chicken

      Buff Orpington Chicks

      Buff Orpington Rooster

      Buff Orpington

      Buff Orpington Chickens

      Buff Orpington Chicken

      Buff Orpington Chicks

      Buff Orpington Rooster

      Buff Orpington

      ReplyDelete
    37. Leghorn chickens are probably the most famous chickens in the world! Their original name was Italians, but over time Leghorns evolved out of it.
      White Leghorn Chicken

      Brown Leghorn Chicken

      Foghorn Leghorn Chicken

      Foghorn Leghorn Chicken Hawk

      White Leghorn Chicken

      White Leghorn Chicken

      ReplyDelete
    38. Participating in betting on odds at the house Fb88.com, you also have the opportunity to receive bonuses and commissions for your online entertainment needs to make money for yourself.
      Fb88
      Fb88.com
      Đăng nhập Fb88
      Fb88 link
      Fb88 casino
      Nhà cái Fb88

      ReplyDelete
    39. Sky Serviços de Banda Larga Ltda. (conhecida popularmente pelo nome Sky) é uma empresa concessionária de serviços de telecomunicações brasileira. Trabalha com televisão por assinatura via satélite e internet banda larga 4G e foi fundada em 11 de novembro de 1996. Sua transmissão digital é feita pelo sistema DTH (direct-to-home) pela banda Ku e a recepção se dá através de uma miniantena parabólica e de um decodificador digital e suas funções dependem de um cartão de acesso. Em 2015, a empresa norte-americana AT&T comprou a DirecTV por US$ 48,5 bilhões, tomando controle da operadora no Brasil.
      Sky Pré Pago

      ReplyDelete
    40. Marketing automation software should be easily integrate together with your CRM software, social media management software, your CMS tools, and ABM platform.

       

      How to choose the simplest marketing automation tool as a marketer?

      Thankfully, there are a couple of selling automation software technologies you'll choose between . However, there are a couple of niche-specific attributes that apply to picking the proper solution for your business. Here are some key elements to think about when evaluating your next Marketing Automation tool:
      marketing automation tool

      ReplyDelete
    41. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks safe deposit box in singapore

      ReplyDelete
    42. I am thankful to you for sharing this plethora of useful information. I found this resource utmost beneficial for me. Thanks a lot for hard work. safe deposit box singapore price

      ReplyDelete
    43. I like your way of defining this article. I like this post. You writing skill amazing. Keep it up! Putlocker

      ReplyDelete

    Bottom Ad [Post Page]