有的时候需要使用多线程来测试代码啥的,在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的更多相关文章

  1. 在windows下配置pthread

    http://blog.csdn.net/qianchenglenger/article/details/16907821 简单介绍windows平台下的pthread线程库

  2. 在Windows下使用Dev-C++开发基于pthread.h的多线程程序【转】

    在Windows下使用Dev-C++开发基于pthread.h的多线程程序[转]     在Windows下使用Dev-C++开发基于pthread.h的多线程程序   文章分类:C++编程     ...

  3. windows下 DEV-C++无法连接到pthread.h的解决办法

    参考的这个博文,原博文有图片:http://lslin.iteye.com/blog/776325 (我只是为了方便写.copy一遍) dev-C++编写C/C++程序时,非常方便轻巧,但是今天学习多 ...

  4. Windows下使用Dev-C++开发基于pthread.h的多线程程序

    一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录      我选择的目录是:E:\DEV-C ...

  5. Windows下POSIX线程编程(pThread)环境搭建

    系统: Windows 编辑器:codeblocks13.12 1. 简介: Windows有一个叫 POSIX Threads for Win32 的开源项目给出了一个功能比较完善的Windows下 ...

  6. 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.

    说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...

  7. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  8. 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 ...

  9. Linux 和 Windows 下实现多进程的方式以及管道操作

    一.多进程 1.windows 多进程 使用 #include<windows.h> 下面的 BOOL CreateProcess( LPCWSTR pszImageName, LPCWS ...

随机推荐

  1. gradle中使用cobertura做代码覆盖(转)

    gradle很好用,但是默认是没有代码覆盖功能的,只好自己写.曾经在网上找到过别人的一段脚本,虽然也能用,但是有一些不爽的地方,一个原因是它不支持对层级工程中全部代码的覆盖,另一个原因是它用替换bui ...

  2. ExtJs4得知(五岁以下儿童)主要的Ext分类

    Ext类是ExtJs最常见的.最基本的类,它是一个全局对象,它封装了全班.辛格尔顿和 Sencha 该方法提供了一种有用的库. 嵌套在该命名空间中一个较低的水平最用户界面组件. 但是提供了很多有用的功 ...

  3. vs2010模板修改

     使用visual studio 2010好久了,也遇到了不少问题,下面跟大家分享一些. 模板修改 说明: 主要工具: 以visual studio 2010作为例子,具体目录可能会根据不同的安装目录 ...

  4. ACdream 1427 Nice Sequence

    主题链接:http://115.28.76.232/problem? pid=1427 Nice Sequence Time Limit: 12000/6000MS (Java/Others)Memo ...

  5. Oracle存储过程详解(引用)+补充(转) dbms_output包 good

    执行存储过程时,execute和call的区别 EXEC is a sqlplus command that put its argument as an anonymous pl/sql block ...

  6. Word2vec 讨论

    我没有在自然语言处理完成.但基于Deep Learning 关注,自然知道一些Word2vec强大. Word2vec 是google 在2013年提供的一款将词表征为实数值向量的高效工具.而Word ...

  7. Android对于静默安装和卸载

    在一般情况下,Android系统安装apk会有一个安装界面,用户可以单击确定或取消apk设备. 但在实际的项目需求中,有一种需求.就是希望apk在后台安装(不出现安装界面的提示),这样的安装方式称为静 ...

  8. 五通信算法:五种编码增益比较matlab模拟

    1. 卷积编码增益性能.BER 信道环境:AWGN 信噪比SNR :0:0.1:6 MATALB仿真架构:源比特 +卷积码 +BPSK +AWGN +Viterbi +BER 说明:卷积编码,不同的R ...

  9. ABP应用层——权限验证

    ABP应用层——权限验证 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP的官方网站:http://www.aspnetboilerpla ...

  10. Hdu 3962 Microgene (AC自己主动机+矩阵)

    标题效果: 构造一个字符串,使得有两个和两个以上的目标串.长短L这一系列有多少串都. IDEAS: 只有全款减有1一些字符串,没有目标就是答案. 假定数据是非常小的,够用dp解.dp[i][j][k] ...