64,654
社区成员
发帖
与我相关
我的任务
分享
#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;
}
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;
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
};