windows下使用pthread
有的时候需要使用多线程来测试代码啥的,在Linux下有pthread,在windows也有。
我这儿是使用MingW作为编译工具,具体如何下载MingW自行在网上搜索。
而pthread是在这里下载的:ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
将下载来的包解压,然后将Pre-built.2中的include下的文件拷贝到mingw/include中,将lib/x86(或x64)/libpthreadGC2.a拷贝到mingw/lib中。
之后使用的时候,在Eclipse的Linker选项里添加一个新的库pthreadGC2即可了。

下面是源代码:
/*
============================================================================
Name : t3.c
Author : Merlin
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/ #include <stdio.h>
#include <stdlib.h>
#include <pthread.h> #include <windows.h> void *thread1(void *arg)
{
while ()
{
printf("T1T1T1T1T1T1T1T1T1T1T1\n");
fflush(stdout);
Sleep();
} return NULL;
} void *thread2(void *arg)
{
while ()
{
printf("T2T2T2T2T2\n");
fflush(stdout);
Sleep();
} return NULL;
} int main(void)
{
void *Tret;
pthread_t Tid1, Tid2; // HWND Wnd;
// Wnd = FindWindow(NULL, "微信");
// if (Wnd)
// {
//// SendMessage(Wnd, WM_CLOSE, 0, 0);
// ShowWindow(Wnd, SW_HIDE);
// Sleep(2000);
// ShowWindow(Wnd, SW_SHOW);
// Sleep(2000);
// ShowWindow(Wnd, SW_HIDE);
// } printf("Hello, this is a example test pthread.\n"); pthread_create(&Tid1, NULL, thread1, NULL); pthread_create(&Tid2, NULL, thread2, NULL); pthread_join(Tid2, &Tret); Sleep();
printf("End, this is a example test pthread.\n");
return EXIT_SUCCESS;
}
需要着重说明的函数是pthread_join,功能是等待Tid2线程返回才会继续向下跑。
执行结果:

报错误1:e:\mingw\include\pthread.h:320:8: error: redefinition of 'struct timespec'
那么在GCC C Compiler -> Symbols中添加HAVE_STRUCT_TIMESPEC定义。
如上面的方法不行,那么使用mingw-install-setup.exe添加安装pthread相关的库:

windows下使用pthread的更多相关文章
- 在windows下配置pthread
http://blog.csdn.net/qianchenglenger/article/details/16907821 简单介绍windows平台下的pthread线程库
- 在Windows下使用Dev-C++开发基于pthread.h的多线程程序【转】
在Windows下使用Dev-C++开发基于pthread.h的多线程程序[转] 在Windows下使用Dev-C++开发基于pthread.h的多线程程序 文章分类:C++编程 ...
- windows下 DEV-C++无法连接到pthread.h的解决办法
参考的这个博文,原博文有图片:http://lslin.iteye.com/blog/776325 (我只是为了方便写.copy一遍) dev-C++编写C/C++程序时,非常方便轻巧,但是今天学习多 ...
- Windows下使用Dev-C++开发基于pthread.h的多线程程序
一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录 我选择的目录是:E:\DEV-C ...
- Windows下POSIX线程编程(pThread)环境搭建
系统: Windows 编辑器:codeblocks13.12 1. 简介: Windows有一个叫 POSIX Threads for Win32 的开源项目给出了一个功能比较完善的Windows下 ...
- 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.
说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...
- [笔记]linux下和windows下的 创建线程函数
linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...
- VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载
VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载 #include <stdio.h>#include &l ...
- Linux 和 Windows 下实现多进程的方式以及管道操作
一.多进程 1.windows 多进程 使用 #include<windows.h> 下面的 BOOL CreateProcess( LPCWSTR pszImageName, LPCWS ...
随机推荐
- Oracle 常见函数使用汇总
INSTR用法:INSTR(string,subString,position,ocurrence)解释:string:源字符串 subString:要查找的子字符串 positi ...
- ActivatedEventArgs.IsApplicationInstancePreserved 属性
ActivatedEventArgs IsApplicationInstancePreserved 如果 ApplicationInstancePreserved 为 true,则表示该应用程序已休眠 ...
- Oracle 中用一个表的数据更新另一个表的数据
Oracle 中用一个表的数据更新另一个表的数据 分类: SQL/PLSQL2012-05-04 15:49 4153人阅读 评论(1) 收藏 举报 oraclemergesubqueryinsert ...
- C语言文件操作函数大全
http://blog.csdn.net/mu0206mu/article/details/18980913 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include&l ...
- oracle_单向函数_数字化功能
oracle_单向函数_数字化功能 1.abs(x) 为了获得x绝对值 2.ceil(x) 用于获得大于或等于x的最小整数. 3.floor(x) 用于获得小于或等于x 4.mod(x,y ...
- unity3D实际的原始视频游戏开发系列讲座10它《战斗:外来入侵》在第一季度游戏开发
解说文件夹 <保卫战:异形入侵>游戏开发 第一讲 游戏演示和资源的介绍 第二讲 "异形"怪物的实现 第三讲 "异形"怪物生命值的体现 第四讲 ...
- 【代码优化】当许多构造函数的参数,请考虑使用builder模式
静态工厂和构造具有共同的局限性:我们不能扩展到大量的非常好的可选参数. 1.对于多个可选參数的构造器.我们都习惯採用重叠构造器模式.比方一个參数的构造器调用2个參数的构造器. 2个參数的构造器 ...
- 使用C++实现功能下载文件
今天问一个同学C++实现的下载链接下载并保存给定的文件,互联网搜索.看到这样的事情在网上.因此,改变下直接带来,因为他的代码是在VC++,我导入到VS2010中出现点小问题.所以改了下贴了个VS中亲測 ...
- 编写WCF服务时右击配置文件无“Edit WCF Configuration”(编辑 WCF 配置)远程的解决办法
原文:编写WCF服务时右击配置文件无“Edit WCF Configuration”远程的解决办法 今天在看<WCF揭秘>书中看到作者提出可以在一个WCF Host应用程序的App.Con ...
- HTTP Cookie深入理解
HTTP Cookie 概述:Cookie通常也叫做网站cookie,浏览器cookie或者http cookie,是保存在用户浏览器端的,并在发出http请求时会默认携带的一段文本片段.它可以用来做 ...