c++ - boost named_condition is not waking up waiting process -


i have 2 processes (producer , consumer) sharing int deque in shared memory, have producer process put 2 numbers in deque , gets in wait state losing mutex lock. have consumer process removing numbers , printing them. notify on condition producer waiting on. consumer goes on own wait on second condition. after case producer not wake up. using same mutex between processes. please find code below.

include file shared_memory.h:

#ifndef sharedmemory_h #define sharedmemory_h  #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/containers/deque.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/interprocess/offset_ptr.hpp> #include <boost/interprocess/sync/named_condition.hpp>  using namespace boost::interprocess;  typedef allocator<offset_ptr<int>, managed_shared_memory::segment_manager> shmemallocator; typedef deque<offset_ptr<int>, shmemallocator> deque;  #endif  

producer process:

#include "shared_memory.h"  struct shm_remove {     shm_remove() { shared_memory_object::remove("mysharedmemory"); }     ~shm_remove() { shared_memory_object::remove("mysharedmemory"); } } remover;  struct mutex_remove {     mutex_remove() { named_mutex::remove("mymutex"); }     ~mutex_remove() { named_mutex::remove("mymutex"); } } mutex_remover;  //create shared memory, mutex , condtion managed_shared_memory segment(create_only, "mysharedmemory", 10000000); named_mutex mutex(create_only,"mymutex"); named_condition full(open_or_create,"fullcondition"); named_condition empty(open_or_create,"emptycondition");  const shmemallocator alloc_inst (segment.get_segment_manager());   int main()  {     deque* mydeque;     offset_ptr<int> a, b;     try{         mydeque = segment.construct<deque>("mydeque")(alloc_inst);         try{            = static_cast<int*> (segment.allocate(sizeof(int)));            b = static_cast<int*> (segment.allocate(sizeof(int)));         }catch(bad_alloc &ex){              std::cout << "could not allocate int" << std::endl;         }     }catch(bad_alloc &ex){         std::cout << "could not allocate queue" << std::endl;     }     scoped_lock<named_mutex> lock(mutex);     while(1)     {         while (mydeque->size() == 2)         {             full.wait(lock);             std::cout << "unlocked producer" << std::endl;         }          if (mydeque->size() == 0)         {             *a = 2;             mydeque->push_back(a);         }          if (mydeque->size() == 1)         {             *b = 4;             mydeque->push_back(b);             empty.notify_one();         }     } } 

consumer process:

#include "shared_memory.h"  managed_shared_memory segment(open_only, "mysharedmemory"); deque* mydeque = segment.find<deque>("mydeque").first;  named_mutex mutex(open_only, "mymutex"); named_condition full(open_only, "fullcondition"); named_condition empty(open_only, "emptycondition");  int main() {     scoped_lock<named_mutex> lock(mutex);     while(1)     {           //volatile int size = mydeque->size();          while (mydeque->size() == 0)          {              empty.wait(lock);          }           if (mydeque->size() == 2)          {              std::cout << "consumer: " << *mydeque->front() << std::endl;              mydeque->pop_front();          }           if (mydeque->size() == 1)          {              std::cout << "consumer: " << *mydeque->front() << std::endl;              mydeque->pop_front();              full.notify_one();          }     } } 

while debugging things seem go ok first iteration, producer puts numbers 2 , 4 on deque waits on full condition. consumer gets lock, prints these numbers, notify_one on full condition , goes wait. after producer not wake up.

the mutex must not locked across notifying. reason of deadlock.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -