- 論壇徽章:
- 1
|
本帖最后由 zhangruichao88 于 2015-01-09 17:50 編輯
有一個線程池的程序,用gdb執(zhí)行的時候是正確的,但直接執(zhí)行會出錯,如果使用gdb附著進程繼續(xù)執(zhí)行,也是正確的,我該怎么解決啊,請各位大神出山
/**********************************************************
* 函數(shù) : myFunc *
* 功能 : 數(shù)據(jù)處理 *
* 輸入 : *
* 輸出 : *
* 返回 : *
* 說明 : *
***********************************************************/
void myFunc(void * const arg)
{
printf("myFunc arg = %p\n", arg);
//if(arg == NULL)
// return;
bool RevComplete = false; //數(shù)據(jù)接收完成的標識
struct timespec time_out;
memset(&time_out, 0, sizeof(time_out));
time_out.tv_sec = time(0);// + 60; //60s
time_out.tv_nsec = IDLE_SCAN_PERIOD * 10 * 1000 * 1000; //100ms
pthread_mutex_lock(&m_Mutex);
//AppMend *AppModule = (AppMend *)malloc(sizeof(AppMend));
//memcpy(AppModule, (AppMend*)arg, sizeof(AppMend));
AppMend *AppModule = (AppMend *)arg;
/*if(arg != NULL)
{
free(arg);
arg = NULL;
}*/
pthread_mutex_unlock(&m_Mutex);
u_int FlowID = AppModule->GetFlowId();
pthread_mutex_lock(&m_pthreadMap);
if(flow2pid.find(FlowID) == flow2pid.end())
{
flow2pid.insert(map<u_int, pthread_t>::value_type(FlowID, pthread_self()));
}
pthread_mutex_unlock(&m_pthreadMap);
//std::cout << "CreateTask FlowID = " << FlowID << ", flow2pid[FlowID] = "
// << flow2pid[FlowID] << ", pid = " << pthread_self() << std::endl;
//等待信號
while(1)
{
printf("arg = %p\n", arg);
int rc = 0;
siginfo_t myinfo;
//組包并排序
if(!RevComplete)
{
AppModule->DataSort(false);
}
else
{
AppModule->DataSort(true);
break;
}
sigset_t oldmask;
/*
if(sigprocmask(SIG_BLOCK, &set, &oldmask) < 0)
{
fprintf(stderr, "SIG_BLOCK error/n" ;
continue;
}
*/
rc = sigtimedwait(&set, &myinfo, &time_out);
if(rc == -1)
{
printf("sorry thread time out !\n" ;
//std::cout << "sorry thread[" << pthread_self() << "] time out !" << std::endl;
RevComplete = true; //超時發(fā)送數(shù)據(jù)接收的標識
continue;
}
//if(AppModule == NULL)
// AppModule = (AppMend *)malloc(sizeof(AppMend));
if(AppModule != NULL)
{
memset(AppModule, 0, sizeof(AppMend));
if(!pthread_recv(AppModule))
{
cout << "Share Memory error !\n";
RevComplete = true; //共享內存為空發(fā)送數(shù)據(jù)接收的標識
continue;
}
}
//std::cout << "pthread_recv FlowID = " << AppModule->GetFlowId() << ", flow2pid[FlowID] = "
// << flow2pid[AppModule->GetFlowId()] << ", pid = " << pthread_self() << std::endl;
}
if(AppModule != NULL)
{
free(AppModule);
//AppModule = NULL;
}
pthread_mutex_lock(&m_pthreadMap);
//解除相應map
for(map<u_int, pthread_t>::iterator iter1 = flow2pid.begin(); iter1 != flow2pid.end()
{
if(iter1->first == FlowID)
{
//std::cout << "free FlowID = " << FlowID << ", flow2pid[FlowID] = " << flow2pid[FlowID] << std::endl;
flow2pid.erase(iter1++);
}
else
++iter1;
}
pthread_mutex_unlock(&m_pthreadMap);
}
在打印while循環(huán)下的arg時,會提示printf出錯,如果刪除該行,則會提示User defined signal 1 后程序終止
|
|