6
\$\begingroup\$

I currently have a container that has two features it allows adding objects to it and at the same time an independent thread continuously consumes items from it. It is a FIFO type deque. Which receives object at an extremely high rate. Following is the code for the container

std::deque<Some_object*> my_container;
void Container::Add_item(Some_object* obj)
 {
 try
 {
 {//Begin Lock
 boost::lock_guard<boost::mutex> lock(mutex_lk);
 my_container.push_back(obj);
 }
 condition_consumer_silo.notify_one(); 
 }
 catch (std::exception& e)
 {
 __debugbreak();
 }
 }
 //This method continuously consumes object in FIFO order
 void Container::Consume_Object()
 {
 while(true)
 {
 Some_object* obj; 
 {//Lock
 try
 {
 boost::unique_lock<boost::mutex> lock(mutex_lk);
 while(my_container.empty()) { condition_consumer_silo.wait(lock); }//Block if empty - for spurious wakeup
 if(!my_container.empty())
 {
 obj = my_container.front();
 my_container.pop_front();
 }
 }
 catch (std::exception& e)
 {
 __debugbreak();
 }
 }//Unlock 
 try
 {
 Consume_obj(obj);
 } catch (std::exception& e)
 {
 __debugbreak();
 }
 }//end while
 }

Could this container be improved ? Is there a library out there that would do a better job.

asked Aug 5, 2013 at 3:28
\$\endgroup\$
2
  • \$\begingroup\$ hi, got your comment message, hope you get a good answer cheers \$\endgroup\$ Commented Aug 5, 2013 at 3:35
  • \$\begingroup\$ In case you don't get an answer: I'm not quite sure, but I think there is something in "C++ Concurrency in Action" by Anthony Williams that could help you. \$\endgroup\$ Commented Aug 5, 2013 at 14:51

1 Answer 1

1
\$\begingroup\$

Ownership symantics:

void Container::Add_item(Some_object* obj)

Who owns obj? This is a C-Style interface. Don't do it.

It would be better to pass in as unique_ptr. This shows transfer of ownership. Also with good compiler optimization is no more expensive than a normal pointer. This also results in slight modification in your code to make sure it handles ownership correctly.

Note: I would still use: std::deque<Some_object*> my_container;

Lockless queues

Lock are very expensive (relatively to queue management). There is a concept of thread safe lock-less queues.

If you have a very high frequency modification to the queue then that is something I would look into. see How do I build a lockless queue?

answered Aug 6, 2013 at 7:21
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.