pthread_rwlock_unlock引起的死锁问题
最近使用读写锁的时候遇到了一个奇怪的死锁现象:调用pthread_rwlock_unlock竟然阻塞了。
堆栈:
(gdb) t 10
[Switching to thread 10 (Thread 0x7ffd621fc700 (LWP 17864))]#0 0x000000363b80e054 in __lll_lock_wait () from /lib64/libpthread.so.0
(gdb) bt
#0 0x000000363b80e054 in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x000000363b80ae90 in pthread_rwlock_unlock () from /lib64/libpthread.so.0
#2 0x000000000040e2b9 in OutboundServer::OutbdExcute (this=0x80, outbd_uuid=..., app_name=..., app_arg=..., app_exe_uuid=..., event_lock=253) at ../os/mutex.h:64
#3 0x0000000000413234 in IxApi::execute_lua (this=0x17b7350, lua_name=..., lua_arg=..., cuid=..., event_lock=false) at ixapi.cpp:1089
#4 0x00007ffd8f55b42c in TelMgr::play_agent_num (this=0x17bb920, cuid=..., language=..., agent_num=...) at TelMgr.cpp:2790
#5 0x00007ffd8f574bb8 in QueueMgr::OnEvent_Answer (this=0x17ce780, msg=0x17ce788) at ../queue_and_acd/QueueMgr.cpp:876
#6 0x00007ffd8f57528c in QueueMgr::processMsg (this=0x17ce780, event=0x80) at ../queue_and_acd/QueueMgr.cpp:473
#7 0x00007ffd8f5755a7 in QueueMgrThread::run (this=0x17d1f40) at ../queue_and_acd/QueueMgr.cpp:421
#8 0x000000000042a717 in util::Thread::threadFun (this=0x17d1f40) at thread.cpp:92
#9 0x000000000042a4aa in util::Thread::begin (pArg=0x7ffd8802ae78) at thread.cpp:35
#10 0x000000363b807851 in start_thread () from /lib64/libpthread.so.0
#11 0x000000363b0e890d in clone () from /lib64/libc.so.6
代码:
std::string OutboundServer::OutbdExcute(const string outbd_uuid,const std :: string app_name,const std :: string app_arg,const std :: string app_exe_uuid,bool event_lock)
{
Oubound_handle_t *handle = ListenThread->otbdConnections.find(outbd_uuid);
if(!handle)
{
TelError<<"Error,cann't find the handle of outbd_uuid:"<<outbd_uuid<<",app_name:"<<app_name<<",arg:"<<app_arg<<",app_exe_uuid:"<<app_exe_uuid<<",event_lock:"<<event_lock;
return "-1";
}
TelTrace<<"find handle,handlenum:"<<handle->handle->sock<<",execute app:"<<app_name<<",arg:"<<app_arg<<",app_exe_uuid:"<<app_exe_uuid<<",event_lock:"<<event_lock;
util::MutexLock lock(handle->handle_Guard);
if(event_lock)
handle->handle->event_lock = 1;
else
handle->handle->event_lock = 0;
if(ESL_SUCCESS!=esl_execute(handle->handle,app_name.c_str(),app_arg.c_str(),app_exe_uuid.c_str()))
return "-1";
if(!handle->handle->last_sr_reply)
return "-1";
return handle->handle->last_sr_reply;
}
这个锁是类内部的锁,且只有一个函数使用(会存在多线程的情况,所以加了锁),且使用的地方是自动锁,所以好奇问一下大家有没有遇到这样的问题?大概会是什么原因?谢谢!
Linux C++ 死锁 读写锁
[解决办法]
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt
[解决办法]
0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中