下载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); \
} \
}
接下来就可以开始使用了
本文介绍如何在Windows环境下配置并使用pthreads-win32库进行多线程编程。主要内容包括下载安装最新版本的pthreads-win32库、设置库的include、lib、dll目录,并编写一个可用于Windows平台的unistd.h文件。此外,还提供了一个通用的头文件示例,用于简化多线程编程的过程。
1745

被折叠的 条评论
为什么被折叠?



