首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

在线程中使用list的pop_front方法出现错误

2012-11-19 
在线程中使用list的pop_front方法出现异常如题,list中存放的是结构体struct CLIENT{char* msgint len}

在线程中使用list的pop_front方法出现异常
如题,list中存放的是结构体
struct CLIENT
{
char* msg;
int len;
};
我已经在线程中加锁同步了,但我这样调用还是出现异常:
  while(true)
  {
  pthread_mutex_lock(&mutex);
CLIENT client;
  if(gClientList.empty())
  {
  pthread_mutex_unlock(&mutex);
  usleep(1000);
  continue;
  }
  client= gClientList.front();
  gClientList.pop_front();//gdb调试此处出错
  pthread_mutex_unlock(&mutex);
printf("%s,%d\n",client.msg,client.len);
delete client.msg;
}
   


[解决办法]
至于为什么出错就不知道了,但是你的设计有问题,应该把这一段改为等待条件变量,push的代码也要相应改动:

C/C++ code
if(gClientList.empty())  {  pthread_mutex_unlock(&mutex);  usleep(1000);  continue;  }while(gClientList.empty())  {  phtread_cond_wait(xx,yy);  }
[解决办法]
1.可能和线程没关系的吧,你暂时不用线程,只做基本的push和pop操作试试看看
2.list中push的结构数据,好像得将结构取地址后再做push(&client),在pop的时候也需要用指针变量来存储该
数据的。
3.delete client.msg这句好像要改成 delete [] client.msg; client.msg = NULL;

热点排行