1.线程特点
- 2.pthread创建
- 3.pthread终止
- 4.mutex互斥量使用框架
- 5.cond条件变量
- 6.综合实例
pthread_cond_destroy
- while (1) {
- lock(lock_for_X);
-
- if (X is not empty) {
- unlock(lock_for_X);
- break;
- } else { //X is empty, loop continues
- unlock(lock_for_X);
- sleep(10);
- }
- }
- //X is not empty, loop ends
- process(X);
- while (1) {
- lock(lock_for_X);
- if (X is not empty) {
- unlock(lock_for_X);
- break;
- } else {
- unlock(lock_for_X); //must called before my_wait(), otherwise no one can acquire the lock and make change to X
- -------------------------------------->窗口,由于已经解锁,其他程序可能改变X,并且试图唤醒mywait,但在一个繁忙的系统中,可能此时my_还没被调用!
- my_wait(); //go to sleep and wait for the notification
- }
- }
- lock(lock_for_X);
- while (X is empty) {
- pthread_cond_wait(&qready, &lock_for_X);
- }
- unlock(lock_for_X);
- while(1) {
- lock(lock_for_X);
- dequeue(X);
- unlock(lock_for_X);
- }
- /*
- * =====================================================================================
- *
- * Filename: pthread.c
- *
- * Description:
- *
- * Version: 1.0
- * Created: 08/17/11 11:06:35
- * Revision: none
- * Compiler: gcc
- *
- * Author: YOUR NAME (),
- * Company:
- *
- * =====================================================================================
- */
- #include <stdio.h>
- #include <pthread.h>
- #include <error.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
-
- pthread_cond_t qready;
- pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
-
- struct foo {
- int cnt;
- pthread_mutex_t f_lock;
- };
-
- void cleanup(void *arg)
- {
- printf("clean up: %s\n", (char *)arg);
- }
-
- void printids(char *str)
- {
- printf("%s pid = %u tid = %u / 0x%x\n",
- str, (unsigned int)getpid(), (unsigned int)pthread_self(), (unsigned int)pthread_self());
- }
-
- void *thread1(void *arg)
- {
- pthread_mutex_lock(&qlock);
- pthread_cond_wait(&qready, &qlock);
- pthread_mutex_unlock(&qlock);
-
- printids("thread1:");
-
- pthread_cleanup_push(cleanup, "thread 1 first cleanup handler");
- pthread_cleanup_push(cleanup, "thread 1 second cleanup handler");
- printf("thread 1 push complete!\n");
-
- pthread_mutex_lock(&((struct foo *)arg)->f_lock);
- ((struct foo *)arg)->cnt ;
- printf("thread1: cnt = %d\n", ((struct foo *)arg)->cnt);
- pthread_mutex_unlock(&((struct foo *)arg)->f_lock);
-
- if (arg)
- return ((void *)0);
-
- pthread_cleanup_pop(0);
- pthread_cleanup_pop(0);
-
- pthread_exit((void *)1);
- }
-
- void *thread2(void *arg)
- {
- int exit_code = -1;
- printids("thread2:");
-
- printf("Now unlock thread1\n");
-
- pthread_mutex_lock(&qlock);
- pthread_mutex_unlock(&qlock);
- pthread_cond_signal(&qready);
-
- printf("Thread1 unlocked\n");
-
- pthread_cleanup_push(cleanup, "thread 2 first cleanup handler");
- pthread_cleanup_push(cleanup, "thread 2 second cleanup handler");
- printf("thread 2 push complete!\n");
-
- if (arg)
- pthread_exit((void *)exit_code);
-
- pthread_cleanup_pop(0);
- pthread_cleanup_pop(0);
-
- pthread_exit((void *)exit_code);
- }
-
- int main(int argc, char *argv[])
- {
- int ret;
- pthread_t tid1, tid2;
- void *retval;
- struct foo *fp;
-
- ret = pthread_cond_init(&qready, NULL);
- if (ret != 0) {
- printf("pthread_cond_init error: %s\n", strerror(ret));
- return -1;
- }
-
-
-
- if ((fp = malloc(sizeof(struct foo))) == NULL) {
- printf("malloc failed!\n");
- return -1;
- }
-
- if (pthread_mutex_init(&fp->f_lock, NULL) != 0) {
- free(fp);
- printf("init mutex failed!\n");
- }
-
- pthread_mutex_lock(&fp->f_lock);
-
- ret = pthread_create(&tid1, NULL, thread1, (void *)fp);
- if (ret != 0) {
- printf("main thread error: %s\n", strerror(ret));
- return -1;
- }
- ret = pthread_create(&tid2, NULL, thread2, (void *)1);
- if (ret != 0) {
- printf("main thread error: %s\n", strerror(ret));
- return -1;
- }
-
-
- ret = pthread_join(tid2, &retval);
- if (ret != 0) {
- printf("pthread join falied!\n");
- return -1;
- }
- else
- printf("thread2 exit code %d\n", (int)retval);
-
- fp->cnt = 1;
- printf("main thread: cnt = %d\n",fp->cnt);
-
- pthread_mutex_unlock(&fp->f_lock);
-
- sleep(1); //there is no guarantee the main thread will run before the newly created thread, so we wait for a while
- printids("main thread:");
-
- printf("Press to exit\n");
-
- ret = pthread_cond_destroy(&qready);
- if (ret != 0) {
- printf("pthread_cond_destroy error: %s\n", strerror(ret));
- return -1;
- }
-
- getchar();
- return 0;
- }
pthread_cond_wait总和一个互斥锁结合使用。在调用pthread_cond_wait前要先获取锁。pthread_cond_wait函数执行时先自动释放指定的锁,然后等待条件变量的变化。在函数调用返回之前,自动将指定的互斥量重新锁住。
int pthread_cond_signal(pthread_cond_t * cond);
pthread_cond_signal通过条件变量cond发送消息,若多个消息在等待,它只唤醒一个。pthread_cond_broadcast可以唤醒所有。调用pthread_cond_signal后要立刻释放互斥锁,因为pthread_cond_wait的最后一步是要将指定的互斥量重新锁住,如果pthread_cond_signal之后没有释放互斥锁,pthread_cond_wait仍然要阻塞。
无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求pthread_cond_wait()(或pthread_cond_timedwait(),下同)的竞争条件(Race Condition)。mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁 (PTHREAD_MUTEX_ADAPTIVE_NP),且在调用pthread_cond_wait()前必须由本线程加锁 (pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开 pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。
激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;而pthread_cond_broadcast()则激活所有等待线程。
下面是另一处说明:给出了函数运行全过程。 为什么在唤醒线程后要重新mutex加锁?
了解 pthread_cond_wait() 的作用非常重要 -- 它是 POSIX 线程信号发送系统的核心,也是最难以理解的部分。首先,让我们考虑以下情况:线程为查看已链接列表而锁定了互斥对象,然而该列表恰巧是空的。这一特定线程什么也干不了 -- 其设计意图是从列表中除去节点,但是现在却没有节点。因此,它只能:
锁定互斥对象时,线程将调用 pthread_cond_wait(&mycond,&mymutex)。pthread_cond_wait() 调用相当复杂,因此我们每次只执行它的一个操作。
pthread_cond_wait() 所做的 第一件事 就是同时对互斥对象解锁(于是其它线程可以修改已链接列表),并等待条件 mycond 发生(这样当 pthread_cond_wait() 接收到另一个线程的“信号”时,它将苏醒)。现在互斥对象已被解锁,其它线程可以访问和修改已链接列表,可能还会添加项。 【 要求解锁并阻塞是一个原子操作 】
此时,pthread_cond_wait() 调用还未返回。对互斥对象解锁会立即发生,但等待条件 mycond 通常是一个阻塞操作,这意味着线程将睡眠,在它苏醒之前不会消耗 CPU 周期。这正是我们期待发生的情况。线程将 一直睡眠,直到特定条件发生 ,在这期间不会发生任何浪费 CPU 时间的繁忙查询。从线程的角度来看,它只是在等待 pthread_cond_wait() 调用返回。
现在继续说明,假设另一个线程(称作“2 号线程”)锁定了 mymutex 并对已链接列表添加了一项。在对互斥对象解锁之后,2 号线程会立即调用函数 pthread_cond_broadcast(&mycond)。此操作之后,2 号线程将使所有等待 mycond 条件变量的线程立即苏醒。这意味着第一个线程(仍处于 pthread_cond_wait() 调用中)现在将 苏醒 。
现在,看一下第一个线程发生了什么。您可能会认为在 2 号线程调用 pthread_cond_broadcast(&mymutex) 之后,1 号线程的 pthread_cond_wait() 会立即返回。不是那样!实际上,pthread_cond_wait() 将执行最后一个操作: 重新锁定 mymutex 。一旦 pthread_cond_wait() 锁定了互斥对象,那么它将返回并允许 1 号线程继续执行。那时,它可以马上检查列表,查看它所感兴趣的更改。
来看一个例子(你是否能理解呢?):
In Thread1:
pthread_mutex_lock(&m_mutex);
pthread_cond_wait(&m_cond,&m_mutex);
pthread_mutex_unlock(&m_mutex);
In Thread2:
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
为什么要与pthread_mutex 一起使用呢? 这是为了应对 线程1在调用pthread_cond_wait()但线程1还没有进入wait cond的状态的时候,此时线程2调用了 cond_singal 的情况。 如果不用mutex锁的话,这个cond_singal就丢失了。加了锁的情况是,线程2必须等到 mutex 被释放(也就是 pthread_cod_wait() 释放锁并进入wait_cond状态 ,此时线程2上锁) 的时候才能调用cond_singal.
pthread_cond_signal即可以放在pthread_mutex_lock和pthread_mutex_unlock之间,也可以放在pthread_mutex_lock和pthread_mutex_unlock之后,但是各有有缺点。
之间:
pthread_mutex_lock
xxxxxxx
pthread_cond_signal
pthread_mutex_unlock
缺点:在某下线程的实现中,会造成等待线程从内核中唤醒(由于cond_signal)然后又回到内核空间(因为cond_wait返回后会有原子加锁的 行为),所以一来一回会有性能的问题。但是在LinuxThreads或者NPTL里面,就不会有这个问题,因为在Linux 线程中,有两个队列,分别是cond_wait队列和mutex_lock队列, cond_signal只是让线程从cond_wait队列移到mutex_lock队列,而不用返回到用户空间,不会有性能的损耗。
所以在Linux中推荐使用这种模式。
之后:
pthread_mutex_lock
xxxxxxx
pthread_mutex_unlock
pthread_cond_signal
优点:不会出现之前说的那个潜在的性能损耗,因为在signal之前就已经释放锁了
缺点:如果unlock和signal之前,有个低优先级的线程正在mutex上等待的话,那么这个低优先级的线程就会抢占高优先级的线程(cond_wait的线程),而这在上面的放中间的模式下是不会出现的。
Thread 就是线程。一个小小的对象而已。线程上可以执行一个函数。主要用法可以用来并发执行一些动作,也能在不阻塞UI的情况下完成一些持续计算。
但是,很多人觉得每次调用一个函数都要new一个线程是很麻烦的。所以干脆提前New好了很多线程。装在一个list中。你要调用函数的时候就从list中提取出一个空闲的线程。函数执行完毕后,就把这个空闲的线程又放到这个list中。减少了new thread的时间。
所以线程池,说白了就是List<Thread>。提供一个方法,让你能方便的把自己的函数不管三七二十一都放这个List中去,然后依次执行。
所以,如果你常常使用系统的线程池,你甚至不需要知道Thread是什么东西。你只要知道,这是一个魔术口袋,你把你的函数塞进去,过一阵子就执行完了。根本不要你来操心。
微软真是培养懒人啊。。
后来大家发现,线程池也不方便,因为进了这个魔术口袋的函数,你不能突然中断它的执行。在多核时代,它的效率也不尽如人意。所以微软又把原来的线程池改造了一下,现在都不叫threadPool了。直接叫Task。你不必管我是怎么实现的,你只要把函数塞我肚肚里,我一定会执行。而且你能用我提供的API。来控制整个过程。
所以Task。就是一个方便使用的线程池。至于把函数塞进去。肯定是在其它线程中执行的,只是这不是我们需要操心的了