2011.04.09
最近脑子不灵活,你伤不起啊,有木有,有木有
简单应用无外乎就是两个步骤了。在类里面的应用,哥喜欢用C++,那就这样吧
先定义
classservercore
{
public:
servercore(void);
virtual~servercore(void);
public:
voidinit();
voidrun();
private:
voidcreate_recv_thread(void*arg);
static void* recv_thread(void*arg);
voidclear_all();
private:
std::list<std::string> m_devlist; //上?线?的?家?电?列?表?
//thread_manager //管?理?各?个?家?电?的?worker_thread
pthread_t m_recv_tid; //接?收?线?程?的?tid
};
1.创建线程 pthread_create
void servercore::create_recv_thread( void *arg ) { if (0 != pthread_create(&m_recv_tid,NULL,recv_thread,NULL)) { throw "servercore.cpp : create_recv_thread error"; } } void* servercore::recv_thread( void *arg ) { }
附:pthread_create原型
#include <pthread.h> int pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg);
2.到线程结束了,等待线程结束
void servercore::clear_all() { if (NULL != m_recv_tid) { pthread_join(m_recv_tid,NULL); } }