线程同步
在变量是只读的时, 多个线程同时读取该变量都是一样的值. 但是当变量写时, 多个线程执行写操作就可能产生随机值. 所以为了使同一时间只允许一个线程写变量, 就要需要线程同步.
关于书上的例子, 我找了一篇很不错的博主写的案例, 用汇编来解释的, 讲解了原子操作, 线程同步, 还有自旋锁的观点. 原子操作与同步机制
线程不同例子 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
static int a;
void *Fun1(void *)
{
sleep(1);
for(int i = 0; i < 100; i++)
a++;
pthread_exit((void *)0);
}
void *Fun2(void *)
{
sleep(1);
for(int i = 0; i < 100; i++)
a++;
pthread_exit((void *)0);
}
int main(void)
{
pthread_t tid1, tid2;
a = 0;
if((errno = pthread_create(&tid1, NULL, Fun1, NULL)) != 0)
fprintf(stderr, "pthread_create error : %s", strerror(errno));
if((errno = pthread_create(&tid2, NULL, Fun2, NULL)) != 0)
fprintf(stderr,"pthread_create error : %s", strerror(errno));
sleep(2);
printf("a = %d\n", a);
exit(0);
}
多次运行的结果
[root@localhost Pthread]# ./a.out
a = 100
[root@localhost Pthread]# ./a.out
a = 106
[root@localhost Pthread]# ./a.out
a = 100
[root@localhost Pthread]# ./a.out
a = 100
[root@localhost Pthread]# ./a.out
a = 107
[root@localhost Pthread]# ./a.out
a = 200
[root@localhost Pthread]# ./a.out
a = 100
从程序的结果也可以看出没有同步的全局变量自加的得到的答案与预期的200有很多偏差, 结果是一个不确定的值.
互斥量
- 优点 :
- 用
pthread
的互斥接口来保护接口, 确保在同一时间内只有一个线程访问数据. - 当其他线程访问该数据时, 会被阻塞.
- 缺点 :
- 每次只有一个线程可以运行, 其他线程会被阻塞.
关于线程动态分配互斥量 :
互斥量初始化
#include <pthread.h>
// 在开始, 动态分配互斥量调用pthread_mutex_inti函数
// attr = NULL, 默认属性初始化互斥量
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t
*restrict attr)
// 释放互斥量之前, 一定要调用pthread_mutex_destroy函数
int pthread_mutex_destroy(pthread_mutex_t *mutex)
// 上述两个函数返回值, 成功, 返回 0; 失败, 返回错误编码.
锁
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex) // 加锁
// 如果本身没有加锁, 该函数会对其进行加锁, 并返回 0;
// 如果本身加锁了, 该函数就不会对其加锁, 比返回 EBUSY
int pthread_mutex_trylock(pthread_mutex_t *mutex) // 避免死锁的加锁
int pthread_mutex_unlock(pthread_mutex_t *mutex) // 解锁
将上面的自增代码进行加锁的修改. 当然, 下面的程序很大一部分是关于错误的判断.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
pthread_mutex_t lock;
static int a;
void *Fun1(void *)
{
sleep(1);
// 进行加锁
if((errno = pthread_mutex_lock(&lock)) != 0)
fprintf(stderr, "pthread_mutex_lock error : %s", strerror(errno));
for(int i = 0; i < 100; i++)
a++;
// 进行解锁
if((errno = pthread_mutex_unlock(&lock)) != 0)
fprintf(stderr, "pthread_mutex_unlock error : %s", strerror(errno));
pthread_exit((void *)0);
}
void *Fun2(void *)
{
sleep(1);
// 进行加锁
if((errno = pthread_mutex_trylock(&lock)) != 0)
fprintf(stderr, "pthread_mutex_lock error : %s", strerror(errno));
for(int i = 0; i < 100; i++)
a++;
// 进行解锁
if((errno = pthread_mutex_unlock(&lock)) != 0)
fprintf(stderr, "pthread_mutex_unlock error : %s", strerror(errno));
pthread_exit((void *)0);
}
int main(void)
{
pthread_t tid1, tid2;
a = 0;
// 分配锁的空间
if((errno = pthread_mutex_init(&lock, NULL)) != 0)
fprintf(stderr, "pthread_mutex_init error : %s", strerror(errno));
if((errno = pthread_create(&tid1, NULL, Fun1, NULL)) != 0)
fprintf(stderr, "pthread_create error : %s", strerror(errno));
if((errno = pthread_create(&tid2, NULL, Fun2, NULL)) != 0)
fprintf(stderr,"pthread_create error : %s", strerror(errno));
sleep(2);
printf("a = %d\n", a);
// 释放锁
if((errno = pthread_mutex_destroy(&lock)) != 0)
fprintf(stderr,"pthread_mutex_destroy error : %s", strerror(errno));
exit(0);
}
多次运行的结果. 加锁之后, 结果不再改变,而且与我们预想的结果一样了.
[root@localhost Pthread]# ./a.out
a = 200
[root@localhost Pthread]# ./a.out
a = 200
[root@localhost Pthread]# ./a.out
a = 200
关于锁的阻塞超时函数
#include <pthread.h>
#include <time.h>
// tsptr : 愿意阻塞时等待的时间
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const struct timespec *restrict tsptr)
// 成功, 返回 0; 失败, 返回错误编码
下面是书上的一个例子, 我也就不过多的解释了, 这一个函数我几弄也有点晕.
/*************************************************************************
> File Name: pthread_mutex_timedlock.cpp
> Author: Function_Dou
> Mail:
> Created Time: 2018年02月08日 星期四 12时10分23秒
************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include "apue.h"
int main(void)
{
int err;
struct timespec tout;
struct tm *tmp;
char buf[64];
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&lock);
printf("mutex is lock\n");
clock_gettime(CLOCK_REALTIME, &tout);
tmp = localtime(&tout.tv_sec);
strftime(buf, sizeof(buf), "%r", tmp);
printf("crrent time is %s\n", buf);
tout.tv_sec += 10;
err = pthread_mutex_timedlock(&lock, &tout);
clock_gettime(CLOCK_REALTIME, &tout);
tmp = localtime(&tout.tv_sec);
strftime(buf, sizeof(buf), "%r", tmp);
printf("the time is now %s\n", buf);
if(err == 0)
printf("mutex locked again\n");
else
printf("can't lock mvtex again:%s\n", strerror(err));
exit(0);
}