1、WINDOWS + LINUX 多线程开发
学习C++,少不了学习Linux,更少不了跨平台的多线程。如今采用如下几种方式实现:
1、自己封装,自己实现
抛开UI界面,多线程函数在两个平台之间没有多大区别,在有区别的地方采用适配器方式,对外统一接口,并且采用宏定义区分: 如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
引用http://bbs.csdn.net/topics/390159005
2、采用QT
QT在跨平台方面实现的很好,如UI处理,通信处理。
3、采用其它库
i、BOOST库
在处理多线程或者网络库的跨平台上,BOOST是一个很好的选择,如今的C++11特性很多都是从BOOST中抽离出来的,缺点是,boost比较大,在大型的项目中,一般都会抛弃。
4、pthread库
1.简介
POSⅨ thread 简称为pthread,Posix线程是一个POSⅨ标准线程,该标准定义内部API创建和操纵线程。
线程库实行了POSIX线程标准通常称为pthreads.POSIX线程具有很好的可移植的性,使用pthread编写的代码可运行于Solaris、FreeBSD、Linux 等平台,Windows平台亦有pthread-win32可供使用。
Pthreads定义了一套 C程序语言类型、函数与常量,它以 pthread.h 头文件和一个线程库实现。
下载地址:ftp://sourceware.org/pub/pthreads-win32
源码结构包括了已经编译好的二进制Pre-built.2,源码pthreads.2,还有一个QueueUserAPCEx ,是一个alert的driver,编译需要DDK ,默认vs2010 没有安装。Windows Device Driver Kit NTDDK.h 需要额外单独安装。
2.windows环境下使用pthread
VS-工具-选项-项目和解决方案-VC++目录,右边引用文件添加目录/include,库文件添加目录/lib
下面①②选一种就可以
①然后在include pthread之后加句
pragma comment(lib, “pthreadVC2.lib”)
②或者在项目-属性-配置属性-链接器-常规-附加库目录,加进库目录
在项目-属性-配置属性-链接器-输入-附加依赖项,加进lib库名
注意事项:
在Windows下使用静态编译的pthread时要特别注意一下,必须显式的调用如下四个函数,否则pthread用到的一些全局变量会没有被初始化,导致所有的pthread的APIs调用都crash。
pthread_win32_process_attach_np (void);
pthread_win32_process_detach_np (void);
pthread_win32_thread_attach_np (void);
pthread_win32_thread_detach_np (void);
pthread官方代码在动态编译的版本中主动做了这个attach和detach操作。而静态编译版本由于没有一个合适的地方来做这件事,就将attach和detach的的操作扔给用户来完成了。
3.测试
include
include
include
pragma comment(lib, “pthreadVC2.lib”) //必须加上这句
void*Function_t(void* Param)
{
pthread_t myid = pthread_self();
while(1)
{
printf(“线程ID=%d \n”, myid);
Sleep(4000);
}
return NULL;
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, Function_t,NULL);
while (1)
{
printf(“in fatherprocess!\n”);
Sleep(2000);
}
getchar();
return 1;
}
摘自http://blog.csdn.net/yinyhy/article/details/10959997
4.参考资料
http://baike.baidu.com/link?url=_FRBxaMGMtQUtpKMJSpW42u9cvGv8VEUR4-WGuDxAIPaCjIzUH6ubC3kABl3ZVpt
http://www.cnblogs.com/ayanmw/archive/2012/08/06/2625275.html
http://www.rosoo.net/a/201108/14914.html
pthread多线程编程的学习小结
http://blog.csdn.net/ithomer/article/details/5921003