这是我的一个简单例子,目前不够完善。
需要后续的优化,如,线程锁的更换,selector模式的加入,以及每个Chan依赖一个独立的线程,性能问题(一个线程管理多个Chan)。chan如何主动结束,程序结束时chan如何结束(通过数据未处理完)等等情况。阻塞式通道和非阻塞通道的设计!!!
Lock.h
#pragma once
#if defined(__cplusplus) && (__cplusplus >= 201103)
#include <thread>
typedef std::thread::id TID;
#else
// #error "not support c++11"
#if defined(WIN32) || defined(WIN64)
#include <windows.h>
typedef DWORD TID;
#elif defined(__linux__)
#include <pthread.h> // 需要 -pthread
#include <unistd.h>
typedef pthread_t TID ;
#endif
#endif
#if defined(__cplusplus) && (__cplusplus >= 201103)
#include <mutex>
class VLock {
std::mutex m_Lock;
public:
void Lock() {m_Lock.lock();}
void UnLock() {m_Lock.unlock();};
bool TryLock() {return m_Lock.try_lock();};
};
#else
//#error "not support c++11"
#if defined(WIN32) || defined(WIN64)
class VLock {
CRITICAL_SECTION m_Lock ;
public:
VLock() {InitializeCriticalSection(&m_Lock);}
~VLock() {DeleteCriticalSection(&m_Lock);}
void Lock() {EnterCriticalSection(&m_Lock);}
void UnLock() {LeaveCriticalSection(&m_Lock);};
};
#elif defined(__linux__)
class VLock {
pthread_mutex_t m_Mutex;
public:
VLock() {pthread_mutex_init( &m_Mutex , NULL );}
~VLock() {pthread_mutex_destroy( &m_Mutex) ;}
void Lock() {pthread_mutex_lock(&m_Mutex);}
void UnLock() {pthread_mutex_unlock(&m_Mutex);};
};
#endif
#endif
//自动加锁解锁器
class AutoLock_T
{
public:
AutoLock_T(VLock& rLock)
{
m_pLock = &rLock;
m_pLock->Lock();
}
~AutoLock_T()
{
m_pLock->UnLock();
}
protected:
private:
AutoLock_T();
VLock* m_pLock;
};
//当前线程挂起一定时间
extern void VSleep( unsigned int millionseconds = 0 ) ;
//获得当前线程ID
extern TID VGetCurrentThreadId( ) ;
Lock.cpp
#include "Lock.h"
void VSleep( unsigned int millionseconds ) {
#if defined(__cplusplus) && (__cplusplus >= 201103)
std::this_thread::sleep_for(std::chrono::milliseconds(millionseconds));
#else
// #error "not support c++11"
#if defined(WIN32) || defined(WIN64)
Sleep( millionseconds ) ;
#elif defined(__linux__)
usleep( millionseconds*1000 ) ;
#endif
#endif
}
TID VGetCurrentThreadId() {
#if defined(__cplusplus) && (__cplusplus >= 201103)
return std::this_thread::get_id();
#else
// #error "not support c++11"
#if defined(WIN32) || defined(WIN64)
return GetCurrentThreadId( ) ;
#elif defined(__linux__)
return pthread_self();
#endif
#endif
}
Thread.h
#pragma once
#include "Lock.h"
//当定义此宏时,所有线程将只执行一次后就推出。
//#define _EXEONECE 10
#if defined(WIN32) || defined(WIN64)
DWORD WINAPI MyThreadProcess( void* derivedThread );
#elif