【系统运维】windows下pthread线程库的使用

    一直以来,由于标准C++中没有纳入线程库,对于多线程的编程感到束手无策,win32的线程机制感觉用着不便。今天想搜索一下有没有其他线程库,想到了Unix下随处可见的pthread线程库,但是苦于这个库是针对unix系统设计的,无法拿到VS中使用。Google了一下,竟然有win32版的pthread,心中大快。赶紧下来使用了一下,当真可行,故分享之。

下载

建议大家下载:

ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe这个自解压文件,压缩包里的pthreads.2目录是源码,Pre-built.2目录是编译所需的头文件和库文件。


如果要自行编译请看这里:

使用微软的CL来编译:
rem cl.bat
cl.exe main.cpp /c /I"c:/pthreads-w32-2-7-0-release/Pre-built.2/include"
link.exe /out:main_cl.exe main.obj /LIBPATH:"c:/pthreads-w32-2-7-0-release/Pre-built.2/lib" pthreadVC2.lib
pause

或者使用GCC来编译:
rem gcc.bat
g++.exe -o main.o -c main.cpp -I"c:/pthreads-w32-2-7-0-release/Pre-built.2/include"
g++.exe -o main_gcc.exe main.o "c:/pthreads-w32-2-7-0-release/Pre-built.2/lib/libpthreadGC2.a"
pause

嘿嘿!开源就是好啊,跨平台实现得如此容易


使用

建好工程后。
VS-工具-选项-项目和解决方案-VC++目录,右边引用文件添加目录***/include,库文件添加目录***/lib
下面①②选一种就可以
①然后在include pthread之后加句
#pragma comment(lib, "pthreadVC2.lib")

②或者在项目-属性-配置属性-链接器-常规-附加库目录,加进库目录
在项目-属性-配置属性-链接器-输入-附加依赖项,加进lib库名

最后在windows/system32里复制进pthreadVC2.dll。这文件在Pre-built.2里有。然后编译就ok了。


OK!现在写一个简单的pthread来测试:

//main.cpp
#include <stdio.h>
#include 
<pthread.h>
#include 
<assert.h>

void* Function_t(void* Param)
{
    printf(
"我是线程! ");
    pthread_t myid 
= pthread_self();
    printf(
"线程ID=%d ", myid);
    
return NULL;
}


int main()
{
    pthread_t pid;
    pthread_attr_t attr;
    pthread_attr_init(
&attr);
    pthread_attr_setscope(
&attr, PTHREAD_SCOPE_PROCESS);
    pthread_attr_setdetachstate(
&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(
&pid, &attr, Function_t, NULL);
    printf(
"======================================== ");
    getchar();
    pthread_attr_destroy(
&attr);
    
return 1;
}


pthread相关介绍

 
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void*(*start_routine)(void*),  void *arg);
参数tid用于返回新创建线程的线程号;start_routine是线程函数指针,线程从这个函数开始运行;arg是传递给线程函数的参数。由于start_routine是一个指向参数类型为void*,返回值为void*的指针,所以如果需要传递或返回多个参数时,可以使用强制类型转化。

void pthread_exit(void* value_ptr);
参数value_ptr是一个指向返回状态值的指针。

int pthread_join( pthread_t tid,  void **status);
参数tid是希望等待的线程的线程号,status是指向线程返回值的指针,线程的返回值就是pthread_exit中的value_ptr参数,或者是return语句中的返回值,该函数可用于线程间的同步。

int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex_attr_t* attr);
该函数初始化一个互斥变量,如果参数attr为NULL,则互斥体变量mutex使用默认的属性。

int pthread_mutex_lock(pthread_mutex_t *mutex);
该函数用来锁住互斥体变量,如果参数mutex所指的互斥体已经被锁住了,那么发出调用的线程将被阻塞直到其他线程对mutex解锁。

int pthread_mutex_trylock(pthread_t *mutex);
该函数用来锁住mutex所指定的互斥体,但不阻塞。如果该互斥体已经被上锁,该调用不会阻塞等待,而会返回一个错误代码。

int pthread_mutex_unlock(pthread_mutex_t *mutex);
该函数用来对一个互斥体解锁。如果当前线程拥有参数mutex所指定的互斥体,该调用将该互斥解锁。

int pthread_mutex_destroy(pthread_mutex_t *mutex);
该函数用来释放分配给参数mutex的资源。调用成功时返回值为0,否则返回一个非0的错误代码。

int pthread_cond_int(pthread_cond_t *cond,const pthread_cond_attr_t *attr);
该函数按参数attr指定的属性创建一个条件变量。调用成功返回,并将条件变量ID赋值给参数cond,否则返回错误代码。

int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
该函数调用为参数mutex指定的互斥体解锁,等待一个事件(由参数cond指定的条件变量)发生。调用函数的线程被阻塞直到有其他线程调用pthread_cond_signal或pthread_cond_broadcast函数置相应的条件变量,而且获得mutex互斥时才解除阻塞。

int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex_t *mutex,const struct timespec *abstime);
该函数与pthread_cond_wait不同的是当系统时间到达abstime参数指定的时间时,被阻塞线程也可以被唤起继续执行。

int pthread_cond_broadcast(pthread_cond_t *cond);
该函数用来对所有等待参数cond所指定的条件变量的线程解除阻塞,调用成功返回0,否则为错误代码。

int pthread_cond_signal(pthread_cond_t *cond);
该函数的作用是解除一个等待参数cond所指定的条件变量,也只唤醒一个线程。

int pthread_cond_destroy(pthread_cond_t *cond);
该函数释放一个条件变量。释放为条件变量cond所分配的资源。调用成功返回值为0,否则为错误代码。

int pthread_key_create(pthread_key_t key,void(*destructor(void*)));
该函数创建一个键值,该键值映射到一个专有数据结构体上。如果第二个参数不是NULL,这个键值被删除时将调用这个函数指针来释放数据空间。

int pthread_key_delete(pthread_key_t *key);
该函数用于删除一个由pthread_key_create函数调用创建的TSD键。调用成功返回值0,否则为返回错误代码。

int pthread_setspecific(pthread_key_t key,const void(value));
该函数设置一个线程专有数据的值,赋给由pthread_key_create创建的TSD键,调用成功返回值为0,否则返回错误代码。

void* pthread_getspecific(pthread_key_t *key);
该函数获得绑定到指定的TSD键上的值。调用成功,返回给定参数key所对应的数据。如果没有数据连接到该TSD键,则返回NULL。

int pthread_once(pthread_once_t* once_control,void(*int_routine)(void));
该函数的作用是确保init_routine指向的函数,在调用pthread_once的线程中只被运行一次。once_control指向一个静态或全局的变量。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章