为了使项目跨平台,那么boost库作为解决平台差异化就必不可少。尤其是在使用了boost的并发相关库,可以做到完美的多平台编译。但是如果在网络方面使用libevent这种更加专业的库作为网络底层,就需要一些特殊配置来让libevent支持boost的并发库。
1.提供回调函数让libevent使用boost的锁
static void* evthread_boost_lock_alloc(unsigned locktype) {
if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE) {
boost::recursive_mutex* rmutex = new boost::recursive_mutex();
return rmutex;
}
else if (locktype & EVTHREAD_LOCKTYPE_READWRITE) {
return NULL;
}
return NULL;
}
static void evthread_boost_lock_free(void *_lock, unsigned locktype) {
if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE) {
boost::recursive_mutex* rmutex = (boost::recursive_mutex*)_lock;
delete rmutex;
}
else if (locktype & EVTHREAD_LOCKTYPE_READWRITE) {
//TODO
}
}
static int evthread_boost_lock(unsigned mode, void *_lock)
{
if (mode & EVTHREAD_TRY) {
boost::recursive_mutex* rmutex = (boost::recursive_mutex*)_lock;
rmutex->tr