Chinaunix首页 | 论坛 | 博客
  • 博客访问: 677360
  • 博文数量: 103
  • 博客积分: 2532
  • 博客等级: 大尉
  • 技术积分: 2039
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-26 16:07
文章分类

全部博文(103)

文章存档

2012年(38)

2011年(28)

2010年(16)

2009年(16)

2008年(5)

分类: C/C++

2011-05-06 15:55:33

多线程介绍
 
 定义了多线程编程的标准API。这个API就是广为人知的pthreads。它的目的在于为跨平台编写多线程程序提供便利。
 
多线程程序的编写
 
本文介绍了Linux 和 WIN32 平台下的多线程程序的编写方法
 
Linux 系统对 pthreads 提供了良好的支持。一般地安装完Linux系统后在/usr/include目录下包含了 pthreads 的头文件。在 /usr/lib 下面包含了pthread 的库文件:libpthread.a 和 libpthread.so。其中libpthread.a 为静态链接库,libpthread.so为动态连接库。
 
WIN32 threads 线程库并不直接支持 pthreads,因此网络上出现了POSIX Threads for Win32 的开源项目,为 WIN32 下开发 Pthreads 多线程程序提供了方便。Pthreads for win32 的下载地址为:。最新版本为 2-8-10。下载pthreads-w32-2-8-0-release.exe 后运行解压出三个目录:pre-build.2 pthreads.2 和QueueUserAPCEx。Pthreads.2 里面包含了pthreads 的源代码,我们主要关心pre-build.2 这个目录。Pre-build.2 里面包含了include和 lib 分别包含了pthreads for win32 的头文件和库文件(包括动态连接库)。将include 和lib 里面的内容分别复制到你的编译器的include 和 lib 目录,同时将lib 目录中的 dll 文件copy 到操作系统的system32 文件夹中。
 
 
  为了保证platform-independent, 很好的一个方法就是使用3rd-party的library, 呵呵. 
    official site: . 
    source code: 

1. 编译: 
    虽然源码包里提供了vc6的项目文件, 但是打不开的, 只能用nmake. 默认的会告诉你一堆nmake参数的. 
    我所要用的是编译成static的library, 所以输入"nmake clean VC-static", 编译很快的. 不过默认会链接到VC的crt, 我们需要修改它的makefile. 找到CFLAGS那一行, 把"/MD"改成"/MT". 

2. 项目: 
    诶.. 有好多地方要改的. 
    a) 当然是vs路径的include啊, lib啊.. 自己加. 
    b) 项目的crt设置成"/MT"和"/MTd". 额外的lib加: pthreadVC2(d).lib ws2_32.lib
    c) preprocesser定义的地方, 加一个“PTW32_STATIC_LIB”宏, 不然link的时候会找不到symbol的. 
    d) 好了, 你可以coding了, 随便pthread_create()一把吧. 

3. 编码: 
    嗯嗯.. 如果真的直接pthread_create()的话可是会access violation的呀. win32下的线程很诡异的, 像winsock一样, 调用任何其它函数之前必须调用pthread_win32_process_attach_np(), 结束后必须调用pthread_win32_process_detach_np(). 代码大概就是这样的: 

int main()

{

#ifdef WIN32

#ifdef PTW32_STATIC_LIB

    pthread_win32_process_attach_np();

#endif

#endif

    /* do something with pthread library */

#ifdef WIN32

#ifdef PTW32_STATIC_LIB

    pthread_win32_process_detach_np();

#endif

#endif

}


    额.. *nix下应该不会有人sb到用static link吧.. 我可不知道怎样弄的. 下面是一个完整的例子: 

#include 

#include 

#include 

 

pthread_mutex_t    count_mutex        = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t    condition_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t    condition_cond  = PTHREAD_COND_INITIALIZER;

 

void *functionCount1(voidparam);

void *functionCount2(voidparam);

int count = 0;

#define COUNT_DONE    10

#define COUNT_HALT1    3

#define COUNT_HALT2    6

 

int main()

{

#ifdef WIN32

#ifdef PTW32_STATIC_LIB

    pthread_win32_process_attach_np();

#endif

#endif

 

    pthread_t thread1thread2;

    pthread_create(&thread1, 0, functionCount1, 0);

    pthread_create(&thread2, 0, functionCount2, 0);

    pthread_join(thread1, 0);

    pthread_join(thread2, 0);

 

#ifdef WIN32

#ifdef PTW32_STATIC_LIB

    pthread_win32_process_detach_np();

#endif

#endif

    return 0;

}

 

void *functionCount1(voidparam)

{

    for (;;)

    {

        pthread_mutex_lock(&condition_mutex);

        while (count >= COUNT_HALT1 && count <= COUNT_HALT2)

        {

            pthread_cond_wait(&condition_cond, &condition_mutex);

            /* ... */

        }

        pthread_mutex_unlock(&condition_mutex);

 

        pthread_mutex_lock(&count_mutex);

        count++;

        printf("Counter value functionCount1: %d\n"count);

        pthread_mutex_unlock(&count_mutex);

 

        if (count >= COUNT_DONEreturn 0;

    }

}

 

void *functionCount2(voidparam)

{

    for (;;)

    {

        pthread_mutex_lock(&condition_mutex);

        if (count < COUNT_HALT1 || count > COUNT_HALT2)

        {

            pthread_cond_signal(&condition_cond);

        }

        pthread_mutex_unlock(&condition_mutex);

 

        pthread_mutex_lock(&count_mutex);

        count++;

        printf("Counter value functionCount2: %d\n"count);

        pthread_mutex_unlock(&count_mutex);

 

        if (count >= COUNT_DONEreturn 0;

    }

}

    举这个例子只是为了说明pthread_cond_signal()和pthread_cond_wait()的用法. 注意到区别了么, 一个用if来判断, 一个用的则是while. 之所以要这样是由于pthread_cond_wait()这个函数很特别, 在进入这个函数开始会先解锁(即解mutex), 离开这个函数时再次加锁, 中间会有空隙, 必须要再次判断. 所以如果注释的地方有代码的话, 是不能保证正确性的. 具体可以man一下自己看. 

4. 参考: 

 
========================================================================
 
第一个多线程程序:
 
我们以最常见的helloworld 程序开始多线程之旅吧。代码如下所示: main.c 
#include
void* thread_one(void* dummy)
{
      while(1)
      {
           printf("Hello,world.this is thread one\n");
      }
     
}
 
void* thread_two(void* dummy)
{
      while(1)
      {
           printf("Hello,world.this is thread two\n");
      }
     
}
 
int main(void)
{
      pthread_t tid[2];
      pthread_create(&tid[0],NULL,thread_one,NULL);
      pthread_create(&tid[1],NULL,thread_two,NULL);
      pthread_exit(NULL);
}
 
编译、链接:
WIN32
VC中新建一个空的工程,加入上述代码,在链接时记得加入pthreadVC2.lib。(在vc 中在“工程”->“设置”->“链接”中)
 
LINUX:
Gcc –o hello main.c –lpthread
 
运行程序就可以看到两个线程在交互运行,不断地出现各自打印的信息。
 
从上面的程序看出,有了pthreads,开发跨平台的多线程程序并不是难事。当然这里只是开始,后续的文章就详细介绍 pthreads 的多线程开发。
 
========================================================================================
(二)
Pthreads 的API
在 pthreads 函数接口可以分为以下三类(关于函数的具体接口参考文章末尾):
1. 线程管理(thread management):用于线程创建、detach、join已经设置和查询线程属性的函数:主要函数有pthread_create,pthread_exit,pthread_detach,pthread_join。pthread_self
2. Mutex 操作函数:用来保证资源的互斥访问,它用来实现多线程访问数据时的同步机制。
主要函数有:pthread_mutex_init,pthread_mutex_lock,pthread_mutex_unlock
3.状态变量操作函数: 这类函数用来建立共享mutex 的多线程通信。它根据程序员设定的条件来决定是否发出信号(signal)或者等待(wait)。主要函数有:pthread_cond_init,pthread_cond_signal: pthread_cond_wait。
 
在pthreads 常用的类型pthread_t 用来记录每个线程的id。
 
下面开始对跨平台多线程编程教程(一)中的例子进行说明。
 
第一行:#include 包含了pthreads 中类型定义和函数接口。
pthread_t tid[2];  用来存储两个线程的id
pthread_create(&tid[0],NULL,thread_one,NULL); 创建了第一个线程。这个线程开始执行thread_one 函数。
pthread_create(&tid[1],NULL,thread_two,NULL); 创建了第二个线程。这个线程开始执行thread_two 函数。
 
两个线程都没有传递参数(第四个参数为NULL)。开始执行后,线程间不断地调度,交替地打印各自的字符串。
 
注意:在前面的例子的main函数的最后部分有调用了函数pthread_exit; 尝试去掉这个语句再运行程序,你会发现两个线程在打印了一些字符串后就退出了。原因是正常情况下,创建了两个线程后,主线程调用 return 0 退出,它所创建的子线程也跟着退出。调用pthread_exit 后子线程就可以一直运行了。
 
后面的章节将对三类 pthreads API 进行相信的介绍。
 
pthreads 常用API 参考(源于网络)
 
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_init(
                  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(*init_routine)(void)
             );
//该函数的作用是确保init_routine 指向的函数,在调用pthread_once的线程中只被运行一次。once_control 指向一个静态或全局的变量。
 
========================================================================================
 
1. 下载pthreads win32源代码:
    
2. 编译静态库:
make clean GC-static
在根目录下面生成libpthreadGC2.a
3. 将生成的libpthreadGC2.a拷贝到mingw库目录下,将pthread.h, sched.h, semaphore.h拷贝到INCLUDE目录下
4. 使用libpthread库,
在程序起始处对libpthread作初始化:
#if defined(PTW32_STATIC_LIB)
    ptw32_processInitialize();
#endif
5. 编译时确保传入-DPTW32_STATIC_LIB,链接时加入-lpthreadGC2, OK!
阅读(7707) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~