提升互斥锁抛出(奇怪的?)异常

5

我正在使用从这个网站获取的阻塞队列示例,认为它非常不错。 这个阻塞队列使用了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;
    }
  };
}

有没有人能发现这里出了什么问题?我已经试图整天找出问题,但是徒劳无功。我想我需要外部的视角来看待这个问题。

请查找“//The exception is thrown here!”的注释,以确定问题发生的确切位置。

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

希望这能回答有关更多信息的请求...
有趣的事情:
如果我将每个“_mysql”替换为“pools::MySQL::get_instance()”,似乎不会崩溃。但我怀疑下面还有更重要的错误...

1
我想指出的是,尽管可能会从这里引发异常,但实际上错误可能并不在这里。也许你在别处错误地使用了该类,或者损坏了你的内存或栈?... - CygnusX1
3
@TheSquad 请编辑您的问题,提供一个最小完整示例来展示问题。根据您发布的代码,目前没有明显的错误。请至少包含抛出异常的堆栈跟踪。正如其他评论所指出的那样,我怀疑错误在程序的其他地方。 - Sam Miller
@Sam Miller:我会尽快制作一个“最小完整”示例,但不幸的是它不会与我不能透露的当前项目完全相同。希望我能够重现这个问题。 - TheSquad
你应该尝试使用Valgrind或类似的工具,我相信它们会显示出一些使用后释放的问题。 - PlasmaHH
@PlasmaHH:使用了Valgrind和Electric-fence,所有我所知道的...它似乎真的是一个有点特殊的异常... - TheSquad
显示剩余8条评论
1个回答

0

如果您尝试调用队列的put方法,但队列已经被销毁,则会抛出此异常。通过在队列析构函数中设置断点(或打印语句)来检查此问题。


由于它是单例类中的属性对象,因此它并没有被销毁。没错,你之前并不知道这一点。我在我的帖子中添加了更多信息。 - TheSquad
@Andy T:它真的会抛出异常吗?我猜它更可能会导致段错误。 - Atmocreations
如果boost::mutex代码检测到无效状态,它可能会抛出异常。即使对象已被删除,该对象地址仍然可以映射到进程中的有效地址。如果是这种情况,则不会发生段错误。 - selalerer
@TheSquad 也许你在某个地方超出了 _mysql 的范围,导致它返回一个无效的地址,而使用 get_instance() 可以获取正确的地址。 - selalerer
@selalerer:问题在于MySQL对象只在main.cc上通过create_instance(io_service)创建,get_instance()只会返回由create_instance创建的实例。由于它只在main.cc中调用(我已经检查过是否没有将其放在其他地方),因此它不会被覆盖。 - TheSquad
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接