boost 线程处理消息队列崩溃 ,不知道怎么同步 在线等!!!

tan625747 2011-07-29 10:48:03
用的是跨平台boost 线程,开1000个线程处理队列的里面的数据,因为对线程不熟所以,可到最后,程序崩溃了?不知如何是好?

望大牛们指点迷津。

dhw_UrlPath::instance()为队列,进队列是在线程处理过程当中,写进去的。

#include "getHtml.h"
#include "parserHtml.h"
#include "parserUrl.h"
#include <boost/regex.hpp>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>


//这个地方到底,应该怎么写呀?
void runChild(const int n)
{
boost::mutex::scoped_lock lock(io_mutex);
if (!dhw_UrlPath::instance()->isEmpty())
{
dhw_UrlPath::instance()->size();
UrlPath * url = dhw_UrlPath::instance()->front();//得到数据
dhw_UrlPath::instance()->pop();//弹出
getHtml * gh = new getHtml;
gh->setUrl(url);
gh->doWork();
delete gh;
}
}


int main()
{
thread_group threads;
for(int i = 0; i < num; i++)
{
threads.create_thread(bind(&runChild, i));
}
threads.join_all();

return 0;
}



下面是dhw_UrlPath:
template <class T>
class Singleton
{
public:
static inline T* instance();
private:
Singleton(void){}
~Singleton(void){}
Singleton(const Singleton&){}
Singleton & operator= (const Singleton &){}
static auto_ptr<T> _instance;
static CResGuard _rs;
};

template <class T>
auto_ptr<T> Singleton<T>::_instance;
template <class T>
CResGuard Singleton<T>::_rs;
template <class T>
inline T* Singleton<T>::instance()
{
if( 0 == _instance.get() )
{
CResGuard::CGuard gd(_rs);
if( 0== _instance.get())
{
_instance.reset ( new T);
}
}
return _instance.get();
}

#define DECLARE_SINGLETON_CLASS( type ) friend class auto_ptr< type >; friend class Singleton< type >;

class queue_UrlPath
{
public:
void Run()
{
}

void push(UrlPath* _up)
{
m_qUrlPath.push(_up);
}
void pop()
{
m_qUrlPath.pop();
}

UrlPath * front()
{
return m_qUrlPath.front();
}

bool isEmpty()
{
return m_qUrlPath.empty();
}

void size()
{
cout<<"---------------------m_qUrlPath size"<< m_qUrlPath.size()<<endl;
}

private:
queue_UrlPath(void)
{
}
virtual ~queue_UrlPath(void)
{
}

private:
queue<UrlPath*> m_qUrlPath;
DECLARE_SINGLETON_CLASS(queue_UrlPath);
};

typedef Singleton<queue_UrlPath> dhw_UrlPath;
...全文
284 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tan625747 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stormlk1983 的回复:]

这样的内存崩溃无非就这些情况
1 使用了野指针
2. 线程用到了已经被析构了的资源

感觉你的代码贴得不全,queue<UrlPath*> m_qUrlPath你是怎么维护的看不到
[/Quote]

已经有维护过,代码如下。

class CResGuard {
public:
CResGuard() { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }
~CResGuard() { DeleteCriticalSection(&m_cs); }
// IsGuarded is used for debugging
bool IsGuarded() const { return(m_lGrdCnt > 0); }
public:
class CGuard {
public:
CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
~CGuard() { m_rg.Unguard(); }
private:
CResGuard& m_rg;
};

private:
void Guard() { EnterCriticalSection(&m_cs); m_lGrdCnt++; }
void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }
// Guard/Unguard can only be accessed by the nested CGuard class.
friend class CResGuard::CGuard;
private:
CRITICAL_SECTION m_cs;
long m_lGrdCnt; // # of EnterCriticalSection calls
};
夹心饼干 2011-07-29
  • 打赏
  • 举报
回复
这样的内存崩溃无非就这些情况
1 使用了野指针
2. 线程用到了已经被析构了的资源

感觉你的代码贴得不全,queue<UrlPath*> m_qUrlPath你是怎么维护的看不到
pengzhixi 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tan625747 的回复:]
引用 3 楼 pengzhixi 的回复:

没看懂你的CResGuard::CGuard gd(_rs);
起什么作用

那是资源方面,单件模式方面方面的。
[/Quote]
我知道你是用来同步单件的一个访问,boost::mutex::scoped_lock lock(io_mutex);
你这里不是已经对整个线程里面的操作都做了同步么
tan625747 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 haoruixiang 的回复:]

多线程: 我把线程分类 一类是写的线程 一类是读的线程

一般尽量避免两个或者多个线程同时去对一个队列去写,或者去读

我的处理方法: 我一般对一个队列只放两个线程去处理 一个是读的线程 一个是写的线程 用特需队列 不用给线程加锁 效率很高 (队列 读线程 写线程) 封装成一个对象
[/Quote]

用这种模式去处理队列,写的队列线程写得很快,比如1秒1000 个数据,由于读的线程很慢可能几秒才会写读出一个数据。

这就是为什么开1000个线程的原因,可到最好程序蹦了。
tan625747 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pengzhixi 的回复:]

没看懂你的CResGuard::CGuard gd(_rs);
起什么作用
[/Quote]

那是资源方面,单件模式方面方面的。
haoruixiang 2011-07-29
  • 打赏
  • 举报
回复
多线程: 我把线程分类 一类是写的线程 一类是读的线程

一般尽量避免两个或者多个线程同时去对一个队列去写,或者去读

我的处理方法: 我一般对一个队列只放两个线程去处理 一个是读的线程 一个是写的线程 用特需队列 不用给线程加锁 效率很高 (队列 读线程 写线程) 封装成一个对象

懒得打字 2011-07-29
  • 打赏
  • 举报
回复
mark一下
pengzhixi 2011-07-29
  • 打赏
  • 举报
回复
没看懂你的CResGuard::CGuard gd(_rs);
起什么作用

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧