deepfuture 发表于 2013-1-26 12:35:22

windows下的pthread库


windows下的pthread库,和linux下一模一样,爽。
windows下的pthread库叫做:pthreads-win32,官方网站是:http://sourceware.org/pthreads-win32/,官方FTP是:
ftp://sources.redhat.com/pub/pthreads-win32/。
郁闷的是,FTP里面的内容比较乱,部分已经编译的库有问题。我下载了一个看起来比较新的库,结果弄了半天不能链接。建议大家下载:
ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe这个自解压文件,压缩包里的pthreads.2目录是源码,
Pre-built.2目录是编译所需的头文件和库文件。
先把pthreads-w32-2-7-0-release.exe解压缩到c:/pthreads-w32-2-7-0-release文件夹。
OK!现在写一个简单的pthread来测试:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background-color: #e6e6e6; padding-bottom: 4px; width: 659px; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//main.cpp
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include <stdio.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include <pthread.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include <assert.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid* Function_t(void* Param)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    printf("我是线程! ");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_t myid = pthread_self();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    printf("线程ID=%d ", myid);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    return NULL;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifint main()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_t pid;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_attr_t attr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_attr_init(&attr);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_create(&pid, &attr, Function_t, NULL);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    printf("======================================== ");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    getchar();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    pthread_attr_destroy(&attr);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    return 1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: windows下的pthread库