创建线程
使用__pthread_create__函数创建线程。
| #include<pthread.h> int pthread_create (pthread_t *__restrict __newthread, __const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg)
|
例程:
1 2 3
| int err; pthread_t tid1; err = pthread_create(&tid1, NULL, thread_func, NULL);
|
终止线程
三种方式线程从执行函数返回,返回值是线程的退出码线程被同一进程的其他线程取消调用__pthread_exit()__函数退出。这里不是调用exit,因为线程调用exit函数,会导致线程所在的进程退出。
例程一
启动两个线程,一个线程对全局变量num执行加1操作,执行五百次,一个线程对全局变量执行减1操作,同样执行五次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h>
int num = 0; void *add(void *arg) { int i = 0, tmp; for (; i < 5; i++) { tmp = num + 1; num = tmp; printf("add+1,result is:%d\n", num); } return ((void *)0); } void *sub(void *arg) { int i = 0, tmp; for (; i < 5; i++) { tmp = num - 1; num = tmp; printf("sub-1,result is:%d\n", num); } return ((void *)0); } int main(int argc, char** argv) { pthread_t tid1, tid2; int err; void *tret; err = pthread_create(&tid1, NULL, add, NULL); if (err != 0) { printf("pthread_create error:%s\n", strerror(err)); exit(-1); } err = pthread_create(&tid2, NULL, sub, NULL); if (err != 0) { printf("pthread_create error:%s\n", strerror(err)); exit(-1); } err = pthread_join(tid1, &tret); if (err != 0) { printf("can not join with thread1:%s\n", strerror(err)); exit(-1); } printf("thread 1 exit code %d\n", (int)(intptr_t)tret); err = pthread_join(tid2, &tret); if (err != 0) { printf("can not join with thread1:%s\n", strerror(err)); exit(-1); } printf("thread 2 exit code %d\n", (int)(intptr_t)tret); return 0; }
|
编译命令: g++ -o 1_example 1_example.cpp -lpthread
问题: 两个线程可以对同一变量进行修改。假如线程1执行tmp=4+1后,被系统中断,此时线程2对num=5执行了减一操作,当线程1恢复,在执行num=tmp=5。而正确结果应为4。所以当多个线程对共享区域进行修改时,应该采用同步的方式。
线程同步(三种方式)
互斥量
互斥量用pthread_mutex_t数据类型来表示。
第一种:赋值为常量PTHREAD_MUTEX_INITIALIZER;
第二种,当互斥量为动态分配是,使用pthread_mutex_init函数进行初始化,使用pthread_mutex_destroy函数销毁。
1 2 3 4 5
| #include<pthread.h> int pthread_mutex_init (pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mutexattr); int pthread_mutex_destroy (pthread_mutex_t *__mutex);
|
加解锁加锁调用pthread_mutex_lock
解锁调用pthread_mutex_unlock
1 2 3
| #include<pthread.h> int pthread_mutex_lock (pthread_mutex_t *__mutex); int pthread_mutex_unlock (pthread_mutex_t *__mutex);
|
使用互斥修改程序的add和sub两个函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; void *add(void *arg) { int i = 0, tmp; for (; i < 500; i++) { pthread_mutex_lock(&mylock); tmp = num + 1; num = tmp; printf("+1,result is:%d\n", num); pthread_mutex_unlock(&mylock); } return ((void *)0); } void *sub(void *arg) { int i = 0, tmp; for (; i < 500; i++) { pthread_mutex_lock(&mylock); tmp = num - 1; num = tmp; printf("-1,result is:%d\n", num); pthread_mutex_unlock(&mylock); } return ((void *)0); }
|
读写锁
允许多个线程同时读,只能有一个线程同时写。适用于读的次数远大于写的情况。
读写锁初始化:
1 2 3 4 5
| #include<pthread.h> int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, __const pthread_rwlockattr_t *__restrict__attr); int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
|
加锁,分为读加锁和写加锁。
解锁使用同一个函数。
1 2 3 4 5 6
| int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
|
条件变量
条件变量用pthread_cond_t数据类型表示。
条件变量本身由互斥量保护,所以在改变条件状态前必须锁住互斥量。
条件变量初始化
1 2 3 4 5
|
int pthread_cond_init (pthread_cond_t *__restrict __cond, __const pthread_condattr_t *__restrict__cond_attr); int pthread_cond_destroy (pthread_cond_t *__cond);
|
条件等待
使用pthread_cond_wait等待条件为真。
1
| pthread_cond_wait (pthread_cond_t *__restrict __cond,pthread_mutex_t *__restrict __mutex)
|
这里需要注意的是,调用pthread_cond_wait传递的互斥量已锁定,pthread_cond_wait将调用线程放入等待条件的线程列表,然后释放互斥量,在pthread_cond_wait返回时,再次锁定互斥量。
唤醒线程
pthread_cond_signal唤醒等待该条件的某个线程,pthread_cond_broadcast唤醒等待该条件的所有线程。
1 2
| int pthread_cond_signal (pthread_cond_t *__cond); int pthread_cond_broadcast (pthread_cond_t *__cond)
|
例程二
主线程启动4个线程,每个线程有一个参数i(i=生成顺序),无论线程的启动顺序如何,执行顺序只能为,线程0、线程1、线程2、线程3。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> #define DEBUG 1
int num = 0; pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t qready = PTHREAD_COND_INITIALIZER; void * thread_func(void *arg) { int i = (int)arg; int ret; sleep(5 - i); pthread_mutex_lock(&mylock); while (i != num) { #ifdef DEBUG printf("thread %d waiting\n", i); #endif ret = pthread_cond_wait(&qready, &mylock); if (ret == 0) { #ifdef DEBUG printf("thread %d wait success\n", i); #endif } else { #ifdef DEBUG printf("thread %d wait failed:%s\n", i, strerror(ret)); #endif } } printf("thread %d is running \n", i); num++; pthread_mutex_unlock(&mylock); pthread_cond_broadcast(&qready); return (void *)0; } int main(int argc, char** argv) { int i = 0, err; pthread_t tid[4]; void *tret; for (; i < 4; i++) { err = pthread_create(&tid[i], NULL, thread_func, (void *)i); if (err != 0) { printf("thread_create error:%s\n", strerror(err)); exit(-1); } } for (i = 0; i < 4; i++) { err = pthread_join(tid[i], &tret); if (err != 0) { printf("can not join with thread %d:%s\n", i, strerror(err)); exit(-1); } } return 0; }
|
在非DEBUG模式,执行结果如下所示:
1 2 3 4 5 6 7
| jack@desktop: g++ -o 1_example 1_example.cpp -lpthread jack@desktop: ./1_example jack@desktop: thread 0 is running jack@desktop: thread 1 is running jack@desktop: thread 2 is running jack@desktop: thread 3 is running jack@desktop: thread 4 is running
|
在DEBUG模式,执行结果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| jack@desktop: g++ -o 1example 1example.cpp -lpthread jack@desktop: ./1_example jack@desktop: thread 3 waiting jack@desktop: thread 2 waiting jack@desktop: thread 1 waiting jack@desktop: thread 0 is running jack@desktop: thread 3 wait success jack@desktop: thread 3 waiting jack@desktop: thread 2 wait success jack@desktop: thread 2 waiting jack@desktop: thread 1 wait success jack@desktop: thread 1 running jack@desktop: thread 3 wait success jack@desktop: thread 3 waiting jack@desktop: thread 2 wait success jack@desktop: thread 2 running jack@desktop: thread 3 wait success jack@desktop: thread 3 running
|
在DEBUG模式可以看出,线程3先被唤醒,然后执行pthread_cond_wait(输出thread 3 waiting),此时在pthread_cond_wait中先解锁互斥量,然后进入等待状态。这是thread 2加锁互斥量成功,进入pthread_cond_wait(输出thread 2 waiting) ,同样解锁互斥量,然后进入等待状态。直到线程0,全局变量与线程参数i一致,满足条件,不进入条件等待,输出thread 0 is running。全局变量num执行加1操作,解锁互斥量,然后唤醒所有等待该条件的线程。thread 3 被唤醒,输出thread 3 wait success。但是不满足条件,再次执行pthread_cond_wait。如此执行下去,满足条件的线程执行,不满足条件的线程等待。