不明原因的行为boost :: scoped_lock

时间:2017-09-06 16:46:24

标签: c++ boost boost-mutex scoped-lock

我是在C ++多线程TCP服务器上写的,用于使用boost:scoped_lock

进行同步

连接到服务器客户端冻结后。

在gdb中我在调用boost :: mutex :: lock

之后在pthread_kill中看到了更多的线程
(gdb) info thread

277 Thread 808779c00 (LWP 245289330/xgps)  0x0000000802579d5c in poll () at poll.S:3
  276 Thread 808779800 (LWP 245289329/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  275 Thread 808779400 (LWP 245289328/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
 .....
  246 Thread 808c92800 (LWP 245289296/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  245 Thread 808643800 (LWP 245289295/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  244 Thread 808643400 (LWP 245289294/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  243 Thread 806c8f400 (LWP 245289292/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  242 Thread 808643000 (LWP 245286262/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
  241 Thread 808c92400 (LWP 245289288/xgps)  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3



[Switching to thread 205 (Thread 80863a000 (LWP 245289251/xgps))]#0  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
(gdb) where
#0  0x00000008019799bc in pthread_kill () from /lib/libthr.so.3
#1  0x0000000801973cfc in pthread_getschedparam () from /lib/libthr.so.3
#2  0x00000008019782fc in pthread_mutex_getprioceiling () from /lib/libthr.so.3
#3  0x000000080197838b in pthread_mutex_lock () from /lib/libthr.so.3
#4  0x0000000000442b2e in boost::mutex::lock (this=0x803835f10) at mutex.hpp:62
#5  0x0000000000442c36 in boost::unique_lock<boost::mutex>::lock (this=0x7fffe7334270) at lock_types.hpp:346
#6  0x0000000000442c7c in unique_lock (this=0x7fffe7334270, m_=@0x803835f10) at lock_types.hpp:124
#7  0x0000000000466e31 in XDevice::getDeviceIMEI (this=0x803835e20) at /home/xgps_app/device.cpp:639
#8  0x000000000049071f in XDevicePool::get_device (this=0x7fffffffd9c0, device_imei=868683024674230) at /home/xgps_app/pool_devices.cpp:351

line device.cpp的代码:639

IMEI 
XDevice::getDeviceIMEI()
{
    try {
        boost::mutex::scoped_lock lock(cn_mutex);
        return  device_imei;
    }
    catch (std::exception &e )
    {
        cout << " ERROR in getDeviceIMEI " << e.what() << "\n";
    }
    return 0;
}

pool_device中的代码

XDevicePtr  
XDevicePool::get_device(IMEI device_imei)
{
    XDevicePtr device;
    unsigned int i = 0;

    while(i < this->devices.size())
    {
        device = devices[i]; 
        if (device->getDeviceIMEI() == device_imei) {
            LOG4CPLUS_DEBUG(logger,  "XDevicePool::get_device found!");
            return device;
        }
        i++;
    }   
    device.reset();
    return device;
}

XDevicePtr 
XDevicePool::get_device_mt(IMEI device_imei)
{
    try 
    {
        boost::mutex::scoped_lock lock(pool_mutex);

    }
    catch (std::exception & e)
    {
        LOG4CPLUS_ERROR(logger,  "XDevicePool::get_device error! " << e.what());
    } 
//  boost::mutex::scoped_lock lock(pool_mutex);
    return get_device(device_imei); 
}

为什么在调用互斥锁线程终止后? 我认为死锁并不是这种行为的原因 请帮忙!

2 个答案:

答案 0 :(得分:0)

tl; dr pthread_kill可能是红鲱鱼。

  

为什么在调用互斥锁线程终止后?

没有。你的主题没有被终止(正如他们仍然出现在info thread上所证明的那样)。

您似乎认为pthread_kill会杀死当前线程。实际上,pthread_kill所做的是向另一个线程发送信号。甚至发送也是可选的(如果是sig=0)。

有关详细信息,请参阅man page

答案 1 :(得分:0)

你有多个锁。

每当你有多个可以同时需要的锁时,你需要以固定的顺序获取它们,以避免死锁。

你好像发生了这样的僵局。请参阅Boost Thread的自由函数boost::lock http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#thread.synchronization.lock_functions.lock_multiple,以获取可靠顺序获取多个锁的帮助。

您还想了解std::defer_lock

除此之外,多线程程序中可能存在来自fork的干扰。我认为现在超出了解释的范围,除非你确实在你的过程中使用fork