简体   繁体   中英

Unexplained behavior boost::scoped_lock

I am wrote on C++ multithread TCP server, for synchronization using boost:scoped_lock

After connecting to server client freezes.

in gdb i saw more threads in pthread_kill after call boost::mutex::lock

(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

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

Code in 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); 
}

Why after call to mutex lock thread terminating? I think dead lock not reason for that behavior Please help!

tl;dr pthread_kill is likely a red herring.

Why after call to mutex lock thread terminating?

It doesn't. Your threads have not been terminated (as evidenced by them still appearing on info thread ).

You seem to assume that pthread_kill kills the current thread. In fact, what pthread_kill does is send a signal to another thread. And even the sending is optional (if sig=0 ).

See the man page for further details.

You have multiple locks.

Whenever you have multiple locks that can be required simultaneously you need to obtain them in a fixed order, to avoid dead-locking.

It seems likely that you have such a deadlock occurring. See Boost Thread's free function boost::lock http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#thread.synchronization.lock_functions.lock_multiple for help acquiring multiple lock in reliable order.

You will also want to know about std::defer_lock .


Other than this, there might be interference from fork in multi-threaded programs. I think it's beyond the scope now to explain, unless you are indeed using fork in your process

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM