一、线程互斥
1. 线程概念
线程就是“轻量级”的进程。
线程与创建它的进程共享 代码段,数据段。
线程拥有自己 独立的栈。
2. 函数学习
创建线程:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
* thread:函数创建的线程id
*attr:创建线程的属性,NULL
void *(*start_routine)(void *):运行的函数
*arg:函数的参数
等待函数结束:pthread_join
函数原型:int pthread_join(pthread_t thread, void **retval);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
thread:等待结束的线程id
**retval:保存线程退出时的状态,一般为NULL
退出线程:pthread_exit
函数原型:void pthread_exit(void *retval);
头文件: 编译时需要连接-phtread
返回值为空
*retval:保存线程退出时的状态
3. 线程互斥
在实际应用中,多个线程往往会访问同一数据或资 源,为避免线程之间相互影响,需要引入线程互斥 机制,而互斥锁(mutex)互斥机制中的一种。
初始化mutex: int pthread_mutex_init(pthread_mutex_t *retrict mutex, const pthread_mutexattr_t *restrict attr);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
attr:互斥锁的属性
互斥锁上锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
互斥锁解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
4 编写代码
thread.c
二、线程同步
1.线程同步
多个线程按照规定的顺序来执行,即为线程同步。
2.条件变量
初始化
sync.c:
阅读(65) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
1. 线程概念
线程就是“轻量级”的进程。
线程与创建它的进程共享 代码段,数据段。
线程拥有自己 独立的栈。
2. 函数学习
创建线程:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
* thread:函数创建的线程id
*attr:创建线程的属性,NULL
void *(*start_routine)(void *):运行的函数
*arg:函数的参数
等待函数结束:pthread_join
函数原型:int pthread_join(pthread_t thread, void **retval);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
thread:等待结束的线程id
**retval:保存线程退出时的状态,一般为NULL
退出线程:pthread_exit
函数原型:void pthread_exit(void *retval);
头文件: 编译时需要连接-phtread
返回值为空
*retval:保存线程退出时的状态
3. 线程互斥
在实际应用中,多个线程往往会访问同一数据或资 源,为避免线程之间相互影响,需要引入线程互斥 机制,而互斥锁(mutex)互斥机制中的一种。
初始化mutex: int pthread_mutex_init(pthread_mutex_t *retrict mutex, const pthread_mutexattr_t *restrict attr);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
attr:互斥锁的属性
互斥锁上锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
互斥锁解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
4 编写代码
thread.c
- #include <pthread.h>
- #include <stdio.h>
-
- pthread_t thread[2];
- int number = 0;
- pthread_mutex_t mutex;
-
- void *worker1()
- {
- int i = 0;
- printf("I am worker1.\n");
- for(i=0;i<10;i++)
- {
- pthread_mutex_lock(&mutex);
- number++;
- pthread_mutex_unlock(&mutex);
- printf("worker1 number is %d\n", number);
-
- sleep(1);
- }
- pthread_exit(NULL);
- }
-
- void *worker2()
- {
- int i = 0;
- printf("I am worker2.\n");
- for(i=0;i<10;i++)
- {
- pthread_mutex_lock(&mutex);
- number++;
- pthread_mutex_unlock(&mutex);
- printf("worker2 number is %d\n", number);
-
- sleep(1);
- }
- pthread_exit(NULL);
- }
-
- void main()
- {
- pthread_mutex_init(&mutex, NULL);
- /*创建工人1线程*/
- pthread_create(&thread[0], NULL, worker1, NULL);
-
- /*创建工人2线程*/
- pthread_create(&thread[1], NULL, worker2, NULL);
-
- /*等待工人1线程结束*/
- pthread_join(thread[0], NULL);
-
- /*等待工人2线程结束*/
- pthread_join(thread[1], NULL);
-
- return;
- }
1.线程同步
多个线程按照规定的顺序来执行,即为线程同步。
2.条件变量
初始化
pthread_cond_t cond_ready=
PTHREAD_COND_INITIALIZER;
等待条件成熟
pthread_cond_wait(&cond_ready, &mut);
设置条件成熟
pthread_cond_signal(&cond_ready);
sync.c:
- #include <sys/types.h>
- #include <pthread.h>
-
- int number = 0;
- pthread_t pthread[2];
- pthread_mutex_t mut;
- pthread_cond_t cond_ready = PTHREAD_COND_INITIALIZER;
-
- void studentA()
- {
- int i;
- /*扫5次地*/
- for(i=0;i<5;i++)
- {
- pthread_mutex_lock(&mut);
- number++;
- if(number >=5)
- {
- printf("student A has funished his work!\n");
- /*通知B同学*/
- pthread_cond_signal(&cond_ready);
- }
- pthread_mutex_unlock(&mut);
- /*休息1秒钟*/
- sleep(1);
- }
-
- /*退出*/
- pthread_exit(NULL);
- }
-
- void studentB()
- {
- pthread_mutex_lock(&mut);
- if(number < 5) /*判断A是否已经扫完5次地*/
- pthread_cond_wait(&cond_ready, &mut);
- /*拖地*/
- number = 0;
- printf("sudent B has finished his work.\n");
- /*退出*/
- pthread_exit(NULL);
- }
-
- int main()
- {
- pthread_mutex_init(&mut, NULL);
- /*创建A同学线程*/
- pthread_create(&pthread[0], NULL, studentA, NULL);
- /*创建B同学线程*/
- pthread_create(&pthread[1], NULL, studentB, NULL);
- /*等待A同学线程结束*/
- pthread_join(pthread[0], NULL);
- /*等待B同学线程结束*/
- pthread_join(pthread[1], NULL);
- }
给主人留下些什么吧!~~
评论热议