Search code examples
c++boost-interprocess

boost::interprocess::interprocess_condition::timed_wait() compiler error


I'm trying to create some objects in shared memory (which will be a future question) but for now I've getting a compiler error (boost 1.53.0 using g++ 4.8.5 on RHEL 7.8) that I can't figure out.

Event.h

#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>

class Event
{
public:
   Event (std::string name, boost::interprocess::managed_shared_memory shmem);

   int SetEvent ();
   int ResetEvent ();

   int WaitForEvent (int64_t milliseconds = -1);

private:
   typedef boost::interprocess::allocator<boost::interprocess::basic_string<char>, std::char_traits<char> >, boost::interprocess::managed_shared_memory::segment_manager> StringAllocator;
   typedef boost::interprocess::basic_string<char, std::char_traits<char>, StringAllocator> ShareableString;

   typedef boost::interprocess::allocator<boost::interprocess::deque<pwfmo_info_t_>, boost::interprocess::managed_shared_memory::segment_manager> DequeAllocator;
   typedef boost::interprocess::deque<pwfmo_info_t_, DequeAllocator> SharedDeque;

   boost::interprocess::interprocess_mutex Mutex;
   boost::interprocess::interprocess_condition Condition;
   ShareableString mName;
   bool State;
   ShareableDeque  mRegisteredWaits;
}

Event.cpp

#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

int Event::WaitForEvent(int64_t milliseconds)
{
    Mutex.lock();
       
    if(!State)
    {   
        boost::posix_time::ptime time;
        if (milliseconds == -1)
        {
            time = boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time();
            time += boost::posix_time::millisec (milliseconds);
        }

        do
        {
            boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock (Mutex);
            if(milliseconds != -1)
            {
                result = Condition.timed_wait (lock, &time);
            }
            else
            {
                Condition.wait (lock);
            }
        } while(result == 0 && !State);
            
        if (result == 0)
        {
            State = false;
        }
    }

    Mutex.unlock ();
        
    return result;
}

When I try to compile the above code I get the following for the timed_wait():

/usr/include/boost/interprocess/sync/interprocess_condition:119:41 required from 'bool boost::interprocess::interprocess_condition::timed_wait (L&, const boost::posix_time::ptime&) [with L=boost::interprocess::interprocess_mutex]'
/usr/include/boost/interprocess/sync/detail/locks.hpp:27:60 error no type named 'mutex_type' in 'class::boost::interprocess_mutex'

I believe that I am declaring and using everything correctly, but obviously something isn't correct. I've been tearing what's left of my hair out trying to figure this out for the past couple of days and I haven't got anywhere. I assume there's some Boost guru out there that could solve/explain to me what's going on that I'm not getting. Any help would be appreciated.


Solution

  • I added a scoped_lock inside the do...while() replacing the Mutex in the timed_wait() and wait() calls. This solved my compiler issues.