#include<iostream>
#include<pthread.h>
#include<unistd.h> using namespace std; //typedef void *(*methodFunction)(void *); class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
void wait();
private:
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
}; CThread::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(),
m_count()
{
} CThread::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = ;
} void CThread::run()
{
m_state = pthread_create(&m_thread, NULL, m_WorkFunction,m_args);
} void CThread::wait()
{
if( == m_state)
{
pthread_join(m_thread, NULL);
}
} int CThread::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction; return ;
} void * thread_work(void*)
{
for ( int i = ; i < ; i++)
{
cout << "the thread output the number:"<<i<< endl;
}
} int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread T;
T.setWorkMethod(thread_work);
T.run();
T.wait();
cout << "the process ending"<< endl;
return ;
}

//demo2: 传入相应参数

#include<iostream>
#include<pthread.h>
#include<unistd.h> using namespace std; //typedef void *(*methodFunction)(void *); class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
void wait();
private:
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
}; CThread::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(),
m_count()
{
} CThread::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = ;
} void CThread::run()
{
m_state = pthread_create(&m_thread, NULL, m_WorkFunction,m_args);
} void CThread::wait()
{
if( == m_state)
{
pthread_join(m_thread, NULL);
}
} int CThread::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction; return ;
} void * thread_work(void* args)
{
for ( int i = ; i < ; i++)
{
cout << "the thread output the number:"<<i<< (char*) args<<endl;
}
} int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread T;
T.setWorkMethod(thread_work, (void *)"hellowrold");
T.run();
T.wait();
cout << "the process ending"<< endl;
return ;
}

//demo3:抛出发送信息

#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string> using namespace std;
struct MSG
{
MSG():
_msg_count(),
_msg_str_message("")
{}
int _msg_count;
std::string _msg_str_message;
};
//typedef void *(*methodFunction)(void *); class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
int setSleepTimeForSeconds(int sec);
void sendMessage(void *(*dealMessageFunction)(MSG *), void* type = NULL);
void stop();
void wait();
private:
static void * func_workFunction(void * args);
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
bool m_destroy;
int m_sleepSeconds;
MSG m_msg;
}; CThread::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(),
m_count(),
m_sleepSeconds(),
m_destroy(false)
{
m_msg._msg_count = ;
m_msg._msg_str_message ="this is a message";
} CThread::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = ;
} void CThread::run()
{
m_state = pthread_create(&m_thread, NULL, func_workFunction, this);
} void CThread::wait()
{
if( == m_state)
{
pthread_join(m_thread, NULL);
}
}
void * CThread::func_workFunction(void * args)
{
CThread * ptrCThread = (CThread *)args;
while(NULL == ptrCThread->m_WorkFunction && !ptrCThread->m_destroy)
{
cout << "the thread do nothing, sleep "<< ptrCThread->m_sleepSeconds<<" seconds" << endl;
sleep(ptrCThread->m_sleepSeconds);
}
if(NULL != ptrCThread->m_WorkFunction && !ptrCThread->m_destroy)
{
//cout << "DEBUG:run the function "<< endl;
ptrCThread->m_WorkFunction(ptrCThread->m_args);
//cout << "DEBUG:end the function "<< endl;
}
} int CThread::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction;
//cout << "DEBUG:set method "<< endl;
return ;
}
void CThread::sendMessage(void *(*dealMessageFunction)(MSG *), void* type)
{
if(!m_msg._msg_str_message.empty())
{
if(NULL == type)
{
dealMessageFunction(&m_msg);
}else
{ }
}
} int CThread::setSleepTimeForSeconds(int sec)
{
if(sec > )
{
m_sleepSeconds = sec;
}
}
void CThread::stop()
{
m_destroy = true;
} void * thread_work(void* args)
{
for ( int i = ; i < ; i++)
{
cout << "the thread output the number: "<< i <<" "<< (char*) args<<endl;
}
} void * dealwithMessage(MSG * msg)
{
MSG tmpMsg;
tmpMsg._msg_count = msg->_msg_count;
tmpMsg._msg_str_message = msg->_msg_str_message; cout << "[INFO]***get the message from the thread***:"<< tmpMsg._msg_str_message << endl;
} void * tread_work1(void *args)
{
for( int i = ; i< ; i++)
{
cout <<"another tread output: "<< i <<" "<<(char*) args<< std::endl;
}
} int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread T;CThread T1;
//T.setWorkMethod(thread_work, (void *)"hellowrold");
T.run();
T1.run();T1.setWorkMethod(tread_work1, (void*)"wssdsfsafa");
T1.sendMessage(dealwithMessage);
sleep();
T.setWorkMethod(thread_work, (void *)"hellowrold");
T.wait();
T1.wait();
cout << "the process ending"<< endl;
return ;
}

//demo4: 线程调用类方法执行

#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string> using namespace std;
struct MSG
{
MSG():
_msg_count(0),
_msg_str_message("")
{}
int _msg_count;
std::string _msg_str_message;
};
//typedef void *(*methodFunction)(void *);
class CWork
{
public:
CWork();
~CWork();
void init(void *);
};
class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
int setWorkMethod(CWork & t, void * args = NULL);
// int setWorkMethod(const T *t, void * args = NULL);
int setSleepTimeForSeconds(int sec);
void sendMessage(void *(*dealMessageFunction)(MSG *), void* type = NULL);
void stop();
void wait();
private:
static void * func_workFunction(void * args);
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
bool m_destroy;
int m_sleepSeconds;
CWork *ptrCWork;
MSG m_msg;
}; CThread::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(0),
m_count(0),
m_sleepSeconds(2),
m_destroy(false),
ptrCWork(NULL)
{
m_msg._msg_count = 1;
m_msg._msg_str_message ="this is a message";
} CThread::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = 0;
} void CThread::run()
{
m_state = pthread_create(&m_thread, NULL, func_workFunction, this);
} void CThread::wait()
{
if( 0 == m_state)
{
pthread_join(m_thread, NULL);
}
}
void * CThread::func_workFunction(void * args)
{
CThread * ptrCThread = (CThread *)args;
while(NULL == ptrCThread->m_WorkFunction && !ptrCThread->m_destroy && ptrCThread->ptrCWork == NULL)
{
cout << "the thread do nothing, sleep "<< ptrCThread->m_sleepSeconds<<" seconds" << endl;
sleep(ptrCThread->m_sleepSeconds);
}
if(NULL != ptrCThread->m_WorkFunction && !ptrCThread->m_destroy)
{
//cout << "DEBUG:run the function "<< endl;
ptrCThread->m_WorkFunction(ptrCThread->m_args);
//cout << "DEBUG:end the function "<< endl;
}else if(NULL !=ptrCThread->ptrCWork && !ptrCThread->m_destroy)
{
ptrCThread->ptrCWork->init(ptrCThread->m_args);
}
} int CThread::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction;
//cout << "DEBUG:set method "<< endl;
return 0;
}
int CThread::setWorkMethod( CWork & t, void * args)
{
if(args != NULL)
{
m_args = args;
}
ptrCWork = &t;
}
// int CThread::setWorkMethod(const T *t, void * args)
// {
// T * ptrClass = (T*)t;
// ptrClass->init(args);
// }
void CThread::sendMessage(void *(*dealMessageFunction)(MSG *), void* type)
{
if(!m_msg._msg_str_message.empty())
{
if(NULL == type)
{
dealMessageFunction(&m_msg);
}else
{ }
}
} int CThread::setSleepTimeForSeconds(int sec)
{
if(sec > 0)
{
m_sleepSeconds = sec;
}
}
void CThread::stop()
{
m_destroy = true;
} void * thread_work(void* args)
{
for ( int i = 0; i < 20; i++)
{
cout << "the thread output the number: "<< i <<" "<< (char*) args<<endl;
}
} void * dealwithMessage(MSG * msg)
{
MSG tmpMsg;
tmpMsg._msg_count = msg->_msg_count;
tmpMsg._msg_str_message = msg->_msg_str_message; cout << "[INFO]***get the message from the thread***:"<< tmpMsg._msg_str_message << endl;
} void * tread_work1(void *args)
{
for( int i = 0; i< 40; i++)
{
cout <<"another tread output: "<< i <<" "<<(char*) args<< std::endl;
}
} CWork::CWork(){}
CWork::~CWork(){}
void CWork::init(void * args)
{
cout << "this is the class function result: "<< (char*)args <<endl;
} int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread T;
CWork work;
//T.setWorkMethod(thread_work, (void *)"hellowrold");
T.run();
sleep(6);
// T.setWorkMethod(thread_work, (void *)"hellowrold");
T.setWorkMethod(work, (void *)"hellowrold");
// T.setWorkMethod(&work, (void *)"hellowrold");
T.wait();
cout << "the process ending"<< endl;
return 0;
}

 //demo4:使用类模板方式,线程调用类方法

#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string> using namespace std;
struct MSG
{
MSG():
_msg_count(),
_msg_str_message("")
{}
int _msg_count;
std::string _msg_str_message;
};
//typedef void *(*methodFunction)(void *);
template<typename T>
class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
int setWorkMethod(T & t, void * args = NULL);
int setSleepTimeForSeconds(int sec);
void sendMessage(void *(*dealMessageFunction)(MSG *), void* type = NULL);
void stop();
void wait();
private:
static void * func_workFunction(void * args);
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
bool m_destroy;
int m_sleepSeconds;
MSG m_msg;
T * m_ptrClass;
}; template<typename T>
CThread<T>::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(),
m_count(),
m_sleepSeconds(),
m_destroy(false),
m_ptrClass(NULL)
{
m_msg._msg_count = ;
m_msg._msg_str_message ="this is a message";
} template<typename T>
CThread<T>::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = ;
} template<typename T>
void CThread<T>::run()
{
m_state = pthread_create(&m_thread, NULL, func_workFunction, this);
} template<typename T>
void CThread<T>::wait()
{
if( == m_state)
{
pthread_join(m_thread, NULL);
}
}
template<typename T>
void * CThread<T>::func_workFunction(void * args)
{
CThread<T> * ptrCThread = (CThread<T> *)args;
while(NULL == ptrCThread->m_WorkFunction && !ptrCThread->m_destroy && ptrCThread->m_ptrClass == NULL)
{
cout << "the thread do nothing, sleep "<< ptrCThread->m_sleepSeconds<<" seconds" << endl;
sleep(ptrCThread->m_sleepSeconds);
}
if(NULL != ptrCThread->m_WorkFunction && !ptrCThread->m_destroy)
{
//cout << "DEBUG:run the function "<< endl;
ptrCThread->m_WorkFunction(ptrCThread->m_args);
//cout << "DEBUG:end the function "<< endl;
}else if(NULL !=ptrCThread->m_ptrClass && !ptrCThread->m_destroy)
{
ptrCThread->m_ptrClass->init(ptrCThread->m_args);
}
} template<typename T>
int CThread<T>::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction;
//cout << "DEBUG:set method "<< endl;
return ;
}
template<typename T>
int CThread<T>::setWorkMethod( T & t, void * args)
{
if(args != NULL)
{
m_args = args;
}
m_ptrClass = &t;
}
template<typename T>
void CThread<T>::sendMessage(void *(*dealMessageFunction)(MSG *), void* type)
{
if(!m_msg._msg_str_message.empty())
{
if(NULL == type)
{
dealMessageFunction(&m_msg);
}else
{ }
}
} template<typename T>
int CThread<T>::setSleepTimeForSeconds(int sec)
{
if(sec > )
{
m_sleepSeconds = sec;
}
} template<typename T>
void CThread<T>::stop()
{
m_destroy = true;
} void * thread_work(void* args)
{
for ( int i = ; i < ; i++)
{
cout << "the thread output the number: "<< i <<" "<< (char*) args<<endl;
}
} void * dealwithMessage(MSG * msg)
{
MSG tmpMsg;
tmpMsg._msg_count = msg->_msg_count;
tmpMsg._msg_str_message = msg->_msg_str_message; cout << "[INFO]***get the message from the thread***:"<< tmpMsg._msg_str_message << endl;
} void * tread_work1(void *args)
{
for( int i = ; i< ; i++)
{
cout <<"another tread output: "<< i <<" "<<(char*) args<< std::endl;
}
} class CWork
{
public:
CWork();
~CWork();
void init(void *);
};
CWork::CWork(){}
CWork::~CWork(){}
void CWork::init(void * args)
{
cout << "this is the class function "<< (char*)args <<endl;
} int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread<CWork> T;
CWork work;
//T.setWorkMethod(thread_work, (void *)"hellowrold");
T.run();
sleep();
//T.setWorkMethod(thread_work, (void *)"hellowrold");
T.setWorkMethod(work, (void *)"hellowrold");
// T.setWorkMethod(&work, (void *)"hellowrold");
T.wait();
cout << "the process ending"<< endl;
return ;
}

// worker is changed

#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string> #define FREE 0
#define BUSY 1
using namespace std;
struct MSG
{
MSG():
_msg_count(),
_msg_str_message("")
{}
int _msg_count;
std::string _msg_str_message;
};
//typedef void *(*methodFunction)(void *);
template<typename T>
class CThread
{
public:
CThread();
~CThread();
void run();
int setWorkMethod(void *(*methodFunction)(void *), void * args = NULL);
int setWorkMethod(T & t, void * args = NULL);
int setSleepTimeForSeconds(int sec);
void sendMessage(void *(*dealMessageFunction)(MSG *), void* type = NULL);
void stop();
void wait();
private:
static void * func_workFunction(void * args);
void *(*m_WorkFunction)(void *);
void *m_args;
pthread_t m_thread;
int m_count;
int m_state;
bool m_destroy;
int m_sleepSeconds;
MSG m_msg;
T * m_ptrClass;
}; template<typename T>
CThread<T>::CThread():
m_WorkFunction(NULL),
m_args(NULL),
m_state(),
m_count(),
m_sleepSeconds(),
m_destroy(false),
m_ptrClass(NULL)
{
m_msg._msg_count = ;
m_msg._msg_str_message ="this is a message";
} template<typename T>
CThread<T>::~CThread()
{
m_WorkFunction = NULL;
m_args = NULL;
m_state = ;
} template<typename T>
void CThread<T>::run()
{
m_state = pthread_create(&m_thread, NULL, func_workFunction, this);
} template<typename T>
void CThread<T>::wait()
{
if( == m_state)
{
pthread_join(m_thread, NULL);
}
}
template<typename T>
void * CThread<T>::func_workFunction(void * args)
{
CThread<T> * ptrCThread = (CThread<T> *)args;
while(NULL == ptrCThread->m_WorkFunction && !ptrCThread->m_destroy && ptrCThread->m_ptrClass == NULL)
{
cout << "the thread do nothing, sleep "<< ptrCThread->m_sleepSeconds<<" seconds" << endl;
sleep(ptrCThread->m_sleepSeconds);
}
if(NULL != ptrCThread->m_WorkFunction && !ptrCThread->m_destroy)
{
//cout << "DEBUG:run the function "<< endl;
ptrCThread->m_WorkFunction(ptrCThread->m_args);
//cout << "DEBUG:end the function "<< endl;
}else if(NULL !=ptrCThread->m_ptrClass && !ptrCThread->m_destroy)
{
ptrCThread->m_ptrClass->init(ptrCThread->m_args);
}
} template<typename T>
int CThread<T>::setWorkMethod(void *(*methodFunction)(void *), void * args)
{
if(args != NULL)
{
m_args = args;
}
m_WorkFunction = methodFunction;
//cout << "DEBUG:set method "<< endl;
return ;
}
template<typename T>
int CThread<T>::setWorkMethod( T & t, void * args)
{
if(args != NULL)
{
m_args = args;
}
m_ptrClass = &t;
}
template<typename T>
void CThread<T>::sendMessage(void *(*dealMessageFunction)(MSG *), void* type)
{
if(!m_msg._msg_str_message.empty())
{
if(NULL == type)
{
dealMessageFunction(&m_msg);
}else
{ }
}
} template<typename T>
int CThread<T>::setSleepTimeForSeconds(int sec)
{
if(sec > )
{
m_sleepSeconds = sec;
}
} template<typename T>
void CThread<T>::stop()
{
m_destroy = true;
} void * thread_work(void* args)
{
for ( int i = ; i < ; i++)
{
cout << "the thread output the number: "<< i <<" "<< (char*) args<<endl;
}
} void * dealwithMessage(MSG * msg)
{
MSG tmpMsg;
tmpMsg._msg_count = msg->_msg_count;
tmpMsg._msg_str_message = msg->_msg_str_message; cout << "[INFO]***get the message from the thread***:"<< tmpMsg._msg_str_message << endl;
} void * tread_work1(void *args)
{
for( int i = ; i< ; i++)
{
cout <<"another tread output: "<< i <<" "<<(char*) args<< std::endl;
}
} class CWork
{
public:
CWork();
~CWork();
void init(void *);
void start(char * args = NULL);
private:
int m_state;
string m_strMsg;
};
CWork::CWork():m_state(FREE){}
CWork::~CWork(){}
void CWork::init(void * args)
{
while(m_state == FREE)
{
cout << "this is the class work function "<< (char*)args <<endl;
sleep();
cout << "wait msg"<<endl;
}
while(m_state == BUSY)
{
cout << "the work start wokrking, the work message is :"<< m_strMsg<<endl;
sleep(); }
/*
for( int i = 0; i < 100 ; i++)
{
cout << "the work start wokrking, the work message is :"<< m_strMsg<<endl;
}
*/ }
void CWork::start(char * args)
{
if( NULL == args)
{
std::cout <<"the start args is none, so nothing to do "<< std::endl;
}else
{
m_strMsg = args;
m_state = BUSY;
}
}
int main(int argc, char * argv[])
{
cout << "test for thread class" <<endl;
CThread<CWork> T;
CWork work;
T.run();
for(int i =;i <; i++)
{
std::cout << "main thread message:first -----" <<i <<std::endl;
} sleep();
cout<<"********set the thread worker"<<endl;
T.setWorkMethod(work, (void *)"hellowrold");
for(int i =;i <; i++)
{
std::cout << "main thread message:second -----" <<i <<std::endl;
}
string strinput;
while(cin>>strinput)
{
cout << strinput << endl;
if(strinput.compare("q") == )
break;
}
cout<<"********set the worker start state"<<endl;
work.start("we have a good way to work");
strinput.clear();
while(cin>>strinput)
{
cout << strinput << endl;
if(strinput.compare("q") == )
break;
}
for(int i =;i <; i++)
{
std::cout << "main thread message:" <<i <<std::endl;
}
// sleep(6);
//T.stop();
T.wait();
cout << "the process ending"<< endl;
return ;
}

最新文章

  1. CSharpGL(18)分别处理glDrawArrays()和glDrawElements()两种方式下的拾取(ColorCodedPicking)
  2. JSTL String时间转成 date
  3. ImageUtil(验证码数据生成工具类)
  4. Oracle 11g XE release2安装与指导
  5. poj 2337 欧拉回路输出最小字典序路径 ***
  6. JAVA,JSP新建默认UTF-8
  7. Sql Server 2008完全卸载方法(其他版本类似)
  8. Android——KEYCODE列表
  9. 通过Javascript数组设计一个省市联动菜单
  10. uml 在需求分析阶段的应用
  11. sim卡中电话本(ADN)的简要格式
  12. git pull VS git fetch&amp;merge(good)
  13. Tigase8.0 源代码分析:一、启动篇
  14. Unity长连接
  15. 安装Go插件遇到的问题及解决方法
  16. 9.26/27 blog项目
  17. cube-ui中弹窗
  18. wapp HTTP Error 404. The requested resource is not found.
  19. 服务发现与消费 --&gt; Spring Cloud Eureka
  20. 双态运维分享之二: 服务型CMDB的消费场景

热门文章

  1. virtualenvwrapper的安装及问题解决
  2. WPF 中依赖属性的继承(Inherits)
  3. C# 使用 System.Web.Script.Serialization 解析 JSON
  4. 【js 正则表达式】记录所有在js中使用正则表达式的情况
  5. 50行代码实现缓存,JAVA内存模型原理
  6. D3.js系列——布局:弦图和集群图/树状图
  7. Chrome 37 Beta: 使用Windows的DirectWrite和支持&lt;dialog&gt;元素
  8. 在redhat下使用x11vnc进行桌面共享
  9. selenium从入门到应用 - 8,selenium+testNG实现多线程的并发测试
  10. 一个256行代码的第一人称引擎(Direct2D移植版)