Windows下pthread多线程使用(1):准备工作

下载pthreads-win32: http://sourceware.org/pthreads-win32/ 目前最新的似乎是2.9.1版本

根据你目标平台,选择并设置pthread的include、lib、dll(或bin)目录(请添加到path变量)

编写可用于windows的unistd.h (这里将此文件保存为unistd_windows.h)

#ifndef _UNISTD_H
#define _UNISTD_H    1
/* This file intended to serve as a drop-in replacement for
*  unistd.h on Windows
*  Please add functionality as neeeded
*/

#include<stdlib.h>
#include<io.h>
#include <process.h>

#define srandom srand
#define random rand

const int W_OK = 2;
const int R_OK = 4;

#define access _access
#define ftruncate _chsize

#define ssize_t int

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* should be in some equivalent to <sys/types.h> */
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;

#endif /* unistd.h */

你的头文件可能会是如下这样子(保存为cmnheader.h)

#define _MULTI_THREADED 
#include <pthread.h> 
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include <unistd_windows.h> 
#include <windows.h>
#pragma comment(lib,"pthreadVC2.lib")

#define __VOID(p)  ((void*)(p))
#define __INT(p) ((int)(p))

#define  checkResults(string, value) \
{ \
if (value!= 0) \
{ \
	printf("Failed with %d at %s", value, string); \
	exit(1); \
} \
}

接下来就可以开始使用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值