有的时候需要使用多线程来测试代码啥的,在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. Java的进程内缓存框架:EhCache (转)

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种缓存 ...

  2. (四)左右ng-app自己主动bootstrap相框

    博客之前 (三)ng-app的使用困惑和angularJS框架的自己主动载入 提出了使用ng-app指令的情况.之前认为出现第4和第5种情况非常奇怪,由于仅仅看到了现象,没有看到本质.JS错误.最直观 ...

  3. cocos2dX 音乐和声音效果

    今天, 我们学习cocos2dX里面音乐和音效, 为什么学他呢, 难道你喜欢看无声电影( 旁白: 我就喜欢关着声音玩), 我们不要管旁白了, 她是个疯子 老规矩, 资源准备好: 一首<故宫神思& ...

  4. 网络爬虫WebCrawler(1)-Http网页内容抓取

    在windows在下面C++由Http协议抓取网页的内容: 首先介绍了两个重要的包(平时linux在开源包,在windows下一个被称为动态链接库dll):curl包和pthreads_dll,其中c ...

  5. SQL开发中容易忽视的一些小地方(二)

    原文:SQL开发中容易忽视的一些小地方(二) 目的:继上一篇:SQL开发中容易忽视的一些小地方(一) 总结SQL中的null用法后,本文我将说说表联接查询. 为了说明问题,我创建了两个表,分别是学生信 ...

  6. Rabbitmq 加入用户訪控制台(guest无法登陆控制台问题)

    对于rabbitmq的guest用户无法訪问控制台的问题,是由于rabbitmq做了安全措施,禁止guest登陆控制台.须要我们自己创建用户进行登陆 1,运行加入用户命令 rabbitmqctl ad ...

  7. Windows 2008 卸载 IIS7 批处理

    @echo offcolor 0aecho 正在卸载IIS功能,这可能需要几分钟时间...start /w pkgmgr /uu:IIS-WebServerRole;WAS-WindowsActiva ...

  8. NPOI以及在ASP.NET MVC中的使用

    NPOI以及在ASP.NET MVC中的使用 1.前言 相信大家在工作中经常要遇到一些导入导出Execl操作.学习贵在分享,分享使人快乐,园子里的前辈已经有很多好的文章,鄙人也是能力有限,在这里把这些 ...

  9. Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC

    Listing 6-1. The Product Model Class using System; using System.Collections.Generic; using System.Li ...

  10. linux(fedora) 下dvwa 建筑环境

    linux(fedora)下dvwa组态 1.下载httpd,dvwa,mysql,mysqlserver, php-mysql,php 除了dvwa 这是外界进入下一官方网站.该服务通过休息inst ...