转载http://www.cnblogs.com/tfanalysis/p/5505163.html

ftp://sourceware.org/pub/pthreads-win32/

有的时候需要使用多线程来测试代码啥的,在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 (1)
{
printf("T1T1T1T1T1T1T1T1T1T1T1\n");
fflush(stdout);
Sleep(1000);
} return NULL;
} void *thread2(void *arg)
{
while (1)
{
printf("T2T2T2T2T2\n");
fflush(stdout);
Sleep(2000);
} 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(100);
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相关的库:

mingw using pthread的更多相关文章

  1. CodeBlocks配置pthread环境

    参考资料:MinGW配置pthread环境 按[参考资料]里说的[下载资源]后,将libpthreadGC2.a放到codeBlocks安装目录下的MinGW\lib目录下,然后将pthread.h ...

  2. windows下使用pthread

    有的时候需要使用多线程来测试代码啥的,在Linux下有pthread,在windows也有. 我这儿是使用MingW作为编译工具,具体如何下载MingW自行在网上搜索. 而pthread是在这里下载的 ...

  3. Windows下安装MinGW,编译c/c++时出现cannot find -lpthread解决办法

    由于Mingw下没有带pthread库,所以在eclipse中设置多线程动态链接库,也不管用.需要自己下载, ftp://sourceware.org/pub/pthreads-win32/pthre ...

  4. 在Windows下利用MinGW编译FFmpeg

    目录 [隐藏]  1 环境与软件 2 第一步:安装MinGW 3 第二步:配置编译环境 4 第三步:配置SDL 5 第四步:编译 5.1 编译faac 5.2 编译fdk-aac 5.3 编译x264 ...

  5. HOWTO Install the MinGW (GCC) Compiler Suite

    Posted July 25th, 2008 by mingwadmin getting started install mingw Automated Installer If you are ne ...

  6. eclipse mingw cpp开发环境

    Eclipse开发c++ 对比:微软的VC++6.0:太老了,对win7兼容不好, 现在微软的Visual Studio:安装包太大,好几个G,装了一堆你不需要的东西,要钱,教 育版申请麻烦 DOS下 ...

  7. [科普]MinGW vs MinGW-W64及其它

    转载:http://tieba.baidu.com/p/3186234212?pid=54372018139&cid=#54372018139 这里也转一下吧. 部分参照备忘录原文: bitb ...

  8. MinGW 使用 msvcr90.dll

    MinGW 编译出来的程序总是使用 VC6 的 msvcrt.dll ,VC8,9,10有很多新的API(仅限于c runtime),想使用怎么办? 比如:boost 对 MinGW 最低要求就是 m ...

  9. linux下使用mingw编译NSIS-3.03

    简述 最近在研究使用NSIS做安装包,语法不算复杂,插件也很多,中文资料也不少,还挺好用的.先后用NSIS做出了安装和卸载需要输入密码,通过自定义页面实现安装时候选择多个目录.安装的时候输入配置文件信 ...

随机推荐

  1. P3649 [APIO2014]回文串

    思路 回文自动机 回文自动机的fail[i]就是编号为i的这个字符串的最长的回文后缀的编号,然后len[i]表示编号为i的回文串的长度,cnt[i]表示编号为i的回文串的出现次数 然后trans边就是 ...

  2. tomcat启动后8005端口未被占用

    8005端口是tomcat本身的端口,如果这个端口在启动时未被tomcat占用的话,就无法使用它自带的shutdown.sh脚本关闭tomcat 接下来我以tomcat-9.0.12为例说明 下载to ...

  3. maven+springmvc出现:java.sql.SQLException: Unknown system variable 'query_cache_size'

    连接mysql时一直出现以下的错误: org.springframework.web.util.NestedServletException: Request processing failed; n ...

  4. 使用qrcode输入信息生成二维码包含二维码说明信息,点击转化为图片并下载

    说明:输入汉字和数字都可以识别并展示 <body> <h2 id="h2">二维码生成</h2> <br> <span id= ...

  5. [python]windows截图

    Windows截图 截图:截取屏幕任意大小图片 下载pip install Pillow from PIL import ImageGrab # x1:开始截图的x坐标 # x2:开始截图的y坐标; ...

  6. (16)线程---定时器Timer

    # ### 定时器:指定时间执行任务 from threading import Timer def func(): print("目前正在执行任务") t = Timer(5,f ...

  7. Practical Node.js (2018版) 14章, async code in Node

    Asynchronous Code in Node 历史上,Node开发者只能用回调和事件emitters. 现在可以使用一些异步的语法: async module Promises Async/aw ...

  8. Steam饥荒

    存档回滚 D:\Program Files (x86)\Steam\userdata\***\219740\remote 巨人国是survival_数字,海难是shipwreck_数字,哈姆雷特是po ...

  9. yii中的restful方式输出并调用接口和判断用户是否登录状态

    //创建一个控制器接口 返回的是restful方式 <?php namespace frontend\controllers; use frontend\models\Fenlei; use f ...

  10. prometheus告警配置注意事项

    global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minut ...