Poco_Thread的用法
2018-03-29 15:40
183 查看
Poco_Thread 中所有的业务逻辑全部在Runnable里面,Thread类只负责开始(Start)和停止(Join)两个动作。
下面贴出一个示例一看便明白;
// Poco_thread_2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include <iostream>
using namespace std;
class HelloRunnable : public Poco::Runnable
{
virtual void run()
{
for(int i=0;i<30;i++)
{
Sleep(1000);
cout << i++ << endl;
}
}
};
int main(int argc,int **argv[])
{
HelloRunnable runnable;
Poco::Thread thread;
thread.start(runnable);
thread.join();
getchar();
return 0;
}众所周知在VC中创建线程的入口函数需要一个全局函数或静态函数,而在实际工程中线程是不停的变化来满足工程需求的,那么如何来封装Thread来满足其实际所需呢?
分析如下:void ThreadImpl::createImpl(Entry ent, void* pData)
{
#if defined(_DLL)
_thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId);
#else
unsigned threadId;
_thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId);
_threadId = static_cast<DWORD>(threadId);
#endif
if (!_thread)
throw SystemException("cannot create thread");
if (_prio != PRIO_NORMAL_IMPL && !SetThreadPriority(_thread, _prio))
throw SystemException("cannot set thread priority");
}
#if defined(_DLL)
DWORD WINAPI ThreadImpl::callableEntry(LPVOID pThread)
#else
unsigned __stdcall ThreadImpl::callableEntry(void* pThread)
#endif
{
_currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
setThreadName(-1, reinterpret_cast<Thread*>(pThread)->getName().c_str());
#endif
try
{
ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread);
pTI->_callbackTarget.callback(pTI->_callbackTarget.pData);
}
catch (Exception& exc)
{
ErrorHandler::handle(exc);
}
catch (std::exception& exc)
{
ErrorHandler::handle(exc);
}
catch (...)
{
ErrorHandler::handle();
}
return 0;
}
由源码看出在创建线程时候
下面贴出一个示例一看便明白;
// Poco_thread_2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include <iostream>
using namespace std;
class HelloRunnable : public Poco::Runnable
{
virtual void run()
{
for(int i=0;i<30;i++)
{
Sleep(1000);
cout << i++ << endl;
}
}
};
int main(int argc,int **argv[])
{
HelloRunnable runnable;
Poco::Thread thread;
thread.start(runnable);
thread.join();
getchar();
return 0;
}众所周知在VC中创建线程的入口函数需要一个全局函数或静态函数,而在实际工程中线程是不停的变化来满足工程需求的,那么如何来封装Thread来满足其实际所需呢?
分析如下:void ThreadImpl::createImpl(Entry ent, void* pData)
{
#if defined(_DLL)
_thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId);
#else
unsigned threadId;
_thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId);
_threadId = static_cast<DWORD>(threadId);
#endif
if (!_thread)
throw SystemException("cannot create thread");
if (_prio != PRIO_NORMAL_IMPL && !SetThreadPriority(_thread, _prio))
throw SystemException("cannot set thread priority");
}
#if defined(_DLL)
DWORD WINAPI ThreadImpl::callableEntry(LPVOID pThread)
#else
unsigned __stdcall ThreadImpl::callableEntry(void* pThread)
#endif
{
_currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
setThreadName(-1, reinterpret_cast<Thread*>(pThread)->getName().c_str());
#endif
try
{
ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread);
pTI->_callbackTarget.callback(pTI->_callbackTarget.pData);
}
catch (Exception& exc)
{
ErrorHandler::handle(exc);
}
catch (std::exception& exc)
{
ErrorHandler::handle(exc);
}
catch (...)
{
ErrorHandler::handle();
}
return 0;
}
由源码看出在创建线程时候
void ThreadImpl::createImpl(Entry ent, void* pData)自己便携带了一个this指针用于指向线程对象本身;线程对象本身封装了一个CallbackDate,
Callback指向了真实的业务路口,不同的线程对象在初始化时,会被赋值不同的业务入口函数;
静态函数callbackEntry中,调用this指针运行真正的业务函数;
ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread); pTI->_callbackTarget.callback(pTI->_callbackTarget.pData);
相关文章推荐
- SyncThread用法
- Thread communication java.util.concurrent.locks.Condition 用法(二)
- PetaPoco.cs的基本用法
- java Thread两种简单用法总结
- Python Thread用法
- python中thread的setDaemon、join的用法
- boost::thread用法
- VC获取其它程序窗口焦点方法 AttachThreadInput的用法
- JAVA Scanner 用法注意事项(scanner 使用错误:Exception in thread "main" java.util.NoSuchElementExceptionation )
- TThreadList的用法
- AfxBeginThread的介绍/基本用法
- Java Thread类的yield()和join()的区别和用法
- iOS开发笔记--高端Thread用法
- Android 中 C++ Thread线程用法
- Android HandlerThread和IntentService用法和源码解析
- android点滴之HandlerThread的用法
- ThreadPool用法与优势
- Boost Thread中mutex 用法
- AfxBeginThread的介绍/基本用法和Window多线程使用详解
- C# 线程池ThreadPool的用法简析