【问题标题】:SOLVED - boost::interprocess::interprocess_condition::timed_wait() compiler error已解决 - boost::interprocess::interprocess_condition::timed_wait() 编译器错误
【发布时间】:2020-11-17 07:44:27
【问题描述】:

我正在尝试在共享内存中创建一些对象(这将是一个未来的问题),但现在我遇到了一个编译器错误(在 RHEL 7.8 上使用 g++ 4.8.5 提升 1.53.0),我可以'想不通。

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;
}

当我尝试编译上面的代码时,我得到 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'

我相信我正确地声明和使用了一切,但显然有些地方是不正确的。在过去的几天里,我一直在扯掉我剩下的头发,试图弄清楚这一点,但我一无所获。 我认为那里有一些 Boost 大师可以解决/向我解释我没有得到的正在发生的事情。任何帮助将不胜感激。

【问题讨论】:

  • 创建Minimal, Reproducible Example 以增加有人查看的机会。这很可能不需要 boost guru - 只需要能够弄清楚您的代码缺少什么的人。

标签: c++ boost-interprocess


【解决方案1】:

我在 do...while() 中添加了一个 scoped_lock,替换了 timed_wait() 和 wait() 调用中的 Mutex。这解决了我的编译器问题。

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2014-06-11
    相关资源
    最近更新 更多