【问题标题】:boost mutex throwing (odd?) exception提升互斥量抛出(奇怪?)异常
【发布时间】:2011-12-16 18:58:10
【问题描述】:

我正在使用从该网站获得的阻塞队列示例,认为它非常好。 这个阻塞队列正在使用 boost::mutex。 有时会抛出异常:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'

what(): 错误的文件描述符

这是阻塞队列代码:

#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <exception>
#include <list>
#include <stdio.h>

struct BlockingQueueTerminate
  : std::exception
{};

namespace tools {
  template<class T>
  class BlockingQueue
  {
  private:
    boost::mutex mtx_;
    boost::condition_variable cnd_;
    std::list<T> q_;
    unsigned blocked_;
    bool stop_;

  public:
    BlockingQueue()
      : blocked_()
      , stop_()
    {}

    ~BlockingQueue()
    {
      this->stop(true);
    }

    void stop(bool wait)
    {
      // tell threads blocked on BlockingQueue::pull() to leave
      boost::mutex::scoped_lock lock(mtx_);
      stop_ = true;
      cnd_.notify_all();

      if(wait) // wait till all threads blocked on the queue leave BlockingQueue::pull()
    while(blocked_)
      cnd_.wait(lock);
    }

    void put(T t)
    {
      boost::mutex::scoped_lock lock(mtx_);  // The exception is thrown here !
      q_.push_back(t);
      cnd_.notify_one();
    }

  T pull()
    {
      boost::mutex::scoped_lock lock(mtx_);
      ++blocked_;
      while(!stop_ && q_.empty())
    cnd_.wait(lock);
      --blocked_;

      if(stop_) {
    cnd_.notify_all(); // tell stop() this thread has left
    throw BlockingQueueTerminate();
      }

      T front = q_.front();
      q_.pop_front();
      return front;
    }
  };
}

任何人都可以发现这里出了什么问题?因为我已经尝试了一整天都徒劳无功。我想我需要一只外眼才能看到它。 寻找评论'//这里抛出异常!'看看问题到底出在哪里。

编辑 1:

上下文:我正在使用这个阻塞队列来创建 MySQL 异步包装器。

这是我的 MySQL.hh

#ifndef MYSQL_HH_
# define MYSQL_HH_
# include <boost/asio.hpp>
# include <boost/thread.hpp>
# include <boost/function.hpp>
# include <mysql++/mysql++.h>
# include <queue>
# include "async_executor.hh"
# include "BlockingQueue.hh"

class t_mysql_event {
public:
  t_mysql_event(std::string query, boost::function<void(mysqlpp::StoreQueryResult)> cb) :
    m_query(query), m_store_cb(cb), m_store_bool(true) {}

  t_mysql_event(std::string query, boost::function<void()> cb) :
    m_query(query), m_exec_cb(cb),  m_store_bool(false) {}

  bool is_store_query() {
    return m_store_bool;
  }

  std::string toString() {
    return m_query;
  }

  std::string                       m_query;
  boost::function<void(mysqlpp::StoreQueryResult)>  m_store_cb;
  boost::function<void()>               m_exec_cb;

private:
  bool                          m_store_bool;
};

namespace pools {
  class MySQL {
  public:
    ~MySQL() {}

    static MySQL* create_instance(boost::asio::io_service& io);

    static MySQL* get_instance();

    void exec(std::string query, boost::function<void()> cb);
    void store(std::string query, boost::function<void(mysqlpp::StoreQueryResult)> cb);

  private:
    MySQL(boost::asio::io_service& io) : executor(io, 100), parent_io(io), m_strand(io)
    {
      for (int i=0; i < 100; ++i) {
    boost::thread(boost::bind(&MySQL::retreive, this));
      }
    }

    void async_exec(std::string query, boost::function<void()> cb, mysqlpp::Connection& conn);
    void async_store(std::string query, boost::function<void(mysqlpp::StoreQueryResult)> cb, mysqlpp::Connection& conn);

    void retreive();

  private:
    tools::async_executor           executor;
    boost::asio::io_service&        parent_io;
    boost::asio::strand         m_strand;
    tools::BlockingQueue<t_mysql_event*>    m_events;
    std::queue<mysqlpp::Connection*>    m_stack;
  };
}

#endif //MYSQL_HH_

这是 MySQL.cc:

#include "MySQL.hh"

static pools::MySQL* _instance = 0;

namespace pools {


  MySQL* MySQL::create_instance(boost::asio::io_service& io) {
    if (!_instance)
      _instance = new MySQL(io);
    return _instance;
  }

  MySQL* MySQL::get_instance() {
    if (!_instance) {
      exit(1);
    }
    return _instance;
  }

  void MySQL::exec(std::string query, boost::function<void()> cb) {
    m_events.put(new t_mysql_event(query, cb));
  }

  void MySQL::store(std::string query, boost::function<void(mysqlpp::StoreQueryResult)> cb) {
    m_events.put(new t_mysql_event(query, cb));
  }

  void MySQL::retreive() {
    mysqlpp::Connection conn("***", "***", "***", "***");
    for(;;) {
      t_mysql_event *event = m_events.pull();
      if (event->is_store_query())
    async_store(event->m_query, event->m_store_cb, conn);
      else
    async_exec(event->m_query, event->m_exec_cb, conn);
      delete event;
    }
  }

  void MySQL::async_exec(std::string query, boost::function<void()> cb, mysqlpp::Connection& conn) {
    mysqlpp::Query db_q = conn.query(query.c_str());
    db_q.exec();
    parent_io.post(cb);
  }

  void MySQL::async_store(std::string query, boost::function<void(mysqlpp::StoreQueryResult)> cb, mysqlpp::Connection& conn) {
    mysqlpp::Query db_q = conn.query(query.c_str());
    mysqlpp::StoreQueryResult res = db_q.store();
    parent_io.post(boost::bind(cb, res));
   }
}

之后:

class MyClass {
public:
   MyClass() : _mysql(pools::MySQL::get_instance()) {}

   startQueries();
private:
   void Query1() {
      std::stringstream query("");
      query << "INSERT INTO Table1 ***";
      _mysql->exec(query.str(),
                   boost::bind(&MyClass::Query2, this, _1));
   }
   void Query2() {
      std::stringstream query("");
      query << "INSERT INTO Table2 ***";
      _mysql->exec(query.str(),
                   boost::bind(&MyClass::Query3, this, _1));
   }
   void Query3() {
      std::stringstream query("");
      query << "INSERT INTO Table3 ***";
      _mysql->exec(query.str(),
                   boost::bind(&MyClass::done, this, _1));
   }
   void done() {}
   pools::MySQL *_mysql;
};

希望这将回答一些关于更多信息的请求......

有趣的事情:

如果我用 pools::MySQL::get_instance() 替换每个 _mysql,我似乎不会崩溃。 但我怀疑有一个比这更重要的错误...

【问题讨论】:

  • 你确定是这段代码,而不是你没有发布的那个吗?
  • 是的,我很确定异常正是从这一点开始抛出的。在询问之前,我已经使用了 gdb 和很多 std::cout 来确定。
  • 这很奇怪,因为我认为构造函数不能抛出异常:boost.org/doc/libs/1_37_0/doc/html/boost/interprocess/…
  • 我想指出,虽然可以从这里引发异常,但实际上可能不在这里。也许您在其他地方错误地使用了该类,甚至浪费了您的内存或堆栈? ....
  • @TheSquad 请使用 minimally complete example 来编辑您的问题,以展示问题。您发布的代码没有明显错误。至少包括引发异常的堆栈跟踪。正如其他 cmets 所指出的那样,我怀疑该错误在您的程序中的其他地方。

标签: c++ exception boost mutex boost-thread


【解决方案1】:

如果队列已被销毁但您尝试调用其put 方法,则可能会引发此异常。通过在队列析构函数中放置断点(或打印语句)来检查这一点。

【讨论】:

  • 它不会被销毁,因为它是作为单例类中的属性的对象。真的,你不知道。我在帖子中添加了更多信息。
  • @Andy T:真的会抛出异常吗?我想它宁愿segfault。
  • @Atmocreations 如果 boost::mutex 代码检测到无效状态,它可能会抛出异常。即使在对象已被删除之后,对象地址仍可能是映射到进程的有效地址。如果是这种情况,则不会发生 seg-fault。
  • @TheSquad 也许你在某个地方超限了 _mysql 并且它返回了一个无效的地址,而使用 get_instance() 不断为你提供正确的地址。
  • @selalerer :问题是 MySQL 对象仅在 main.cc 上使用 create_instance(io_service) 创建,get_instance() 将仅返回由 create_instance 创建的实例。因为它只在 main.cc 中被调用(我已经检查过我是否没有把它放在其他任何地方)它不能被溢出。
猜你喜欢
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多