以下纯属个人观点,如有不妥之处,请大家指正
对于C/C++,可变参数宏在windows和linux下是不同的。需要分别定义
#include <stdarg.h>
#if defined(_WIN32) //C99
#define dbg(level, fmt, ...) \
do{\
log(LogLevel, fmt, __VA_ARGS__); \
} \
}while(0)
#else //linux内核代码
#define dbg(level, fmt, args...) \
do{\
log(LogLevel, fmt,##args); \
} \
}while(0)
#endif
select做延迟
static void sleep_ms(int32_t ms)
{
struct timeval timeout;
int ret = 0;
int32_t fd;
timeout.tv_sec = 0;
timeout.tv_usec = ms * 1000;//10ms
#if defined(_WIN32)
//Sleep(ms); //Sleep函数延迟ms精度不够
fd_set fds;
FD_ZERO(&fds);
fd = socket(AF_INET, SOCK_DGRAM, 0);
FD_SET(fd, &fds);
ret = select(0, NULL, NULL, &fds, &timeout);
closesocket(fd);
#else
select(0,NULL,NULL,NULL,&timeout);
#endif
}
信号量
sem_t* sem;
#if __APPLE__
sem = sem_open(SEM_NAME,O_CREAT, S_IRUSR | S_IWUSR, 0);
#else
ret = sem_init(sem, 0, 0);
#endif
线程命名
#if __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_MAC
#include <pthread.h>
#endif
#endif
#if _WIN32
SetThreadDescription(GetCurrentThread(), "abcdThread");
#elif TARGET_OS_MAC
pthread_setname_np("abcdThread");
#else
prctl(PR_SET_NAME,"abcdThread");
#endif
strcasecmp
#if _WIN32
if (stricmp(Name, "Name") == 0)
#else
if (strcasecmp(Name, "Name") == 0)
#endif
socket控制
#if _WIN32
ioctlsocket(sock, FIONREAD, &length);
closesocket(sock);
#else
ioctl(sock, FIONREAD, &length);
close(sock);
#endif
strtok_r strtok_s
char list[123];
char* token = NULL;
char * holder = NULL;
#if _WIN32
token = strtok_s(list, DP_SEPARATOR, &holder);
#else
token = strtok_r(list, DP_SEPARATOR, &holder);
#endif
ns
static int64_t get_ns()
{
int64_t result;
struct timespec ts;
#if _WIN32
LARGE_INTEGER m_liPerfFreq = { 0 };
LARGE_INTEGER liPerfNow = { 0 };
QueryPerformanceFrequency(&m_liPerfFreq);
QueryPerformanceCounter(&liPerfNow);
ts.tv_sec = (liPerfNow.QuadPart / 1000000) / m_liPerfFreq.QuadPart;
ts.tv_nsec = (liPerfNow.QuadPart * 1000) / m_liPerfFreq.QuadPart;
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
result = 1000000000LL * (int64_t)ts.tv_sec + (int64_t)ts.tv_nsec;
return result;
}
*原子操作
#if _WIN32
#include <intrin.h>
#endif
int32_t atomic_add32(volatile int32_t *mem, int32_t val)
{
#if _WIN32
return _InterlockedExchangeAdd(mem, val);
#else
return __sync_fetch_and_add(mem, val);
#endif
}
int32_t atomic_sub32(volatile int32_t *mem, int32_t val)
{
#if _WIN32
val = 0 - val;
return _InterlockedExchangeAdd(mem, val);
#else
return __sync_fetch_and_sub(mem, val);
#endif
}
int32_t atomic_inc32(volatile int32_t *mem)
{
#if _WIN32
return _InterlockedIncrement(mem);
#else
return __sync_fetch_and_add(mem, 1);
#endif
}
int32_t atomic_dec32(volatile int32_t *mem)
{
#if _WIN32
return _InterlockedDecrement(mem);
#else
return __sync_sub_and_fetch(mem, 1);
#endif
}
int32_t atomic_cas32(volatile int32_t *mem, int32_t with, int32_t cmp)
{
#if _WIN32
return _InterlockedCompareExchange(mem, with, cmp);
#else
return __sync_val_compare_and_swap(mem, cmp, with);
#endif
}