APUE 学习笔记(八) 线程同步
1. 进程的所有信息对该进程内的所有线程都是共享的
包括 可执行的程序文本、程序全局内存、堆内存以及文件描述符
#include <unistd.h>
#include <stdio.h>
#include <pthread.h> void printid(const char* str)
{
pid_t pid = getpid();
pthread_t tid = pthread_self();
fprintf(stdout, "%s pid:%u,tid:%u\n", str, (unsigned int)pid, (unsigned int)tid);
} void* thread_func(void* arg)
{
printid("new thread: ");
return (void*);
} int main(int argc, char* argv[])
{
pthread_t tid;
int ret = pthread_create(&tid, NULL, thread_func, NULL);
if (ret != ) {
fprintf(stderr, "pthread_create error\n");
return -;
}
printid("main thread: ");
sleep();
return ;
}
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h> #define NTHREADS 5 void* PrintHello(void* threadId)
{
int tid = ((int)threadId);
fprintf(stdout, "Hello world, thread %d\n", tid);
pthread_exit(NULL);
} int main(int argc, char* argv[])
{
pthread_t threads[NTHREADS];
int rc = ;
for (int i = ; i < NTHREADS; ++i) {
fprintf(stdout, "In main: creating thread %d\n", i);
rc = pthread_create(&threads[i], NULL, PrintHello, (void*)i);
if (rc != ) {
fprintf(stderr, "error:return code from pthread_create is %d\n", rc);
exit(-);
}
}
pthread_exit(NULL);
}
上述代码,我们创建了5个线程,每个线程打印一条包含线程编号的语句
可以预想到:每次运行程序时,结果不尽相同。因为 线程创建时并不能保证哪个线程会先执行,不能在线程调度上做出任何假设


假如我们将上述代码中
rc = pthread_create(&threads[i], NULL, PrintHello, (void*)i);
void* PrintHello(void* threadId)
{
int tid = ((int)threadId);
fprintf(stdout, "Hello world, thread %d\n", tid);
pthread_exit(NULL);
}
改为以下:
rc = pthread_create(&threads[i], NULL, PrintHello, (void*)&i);
void* PrintHello(void* threadId)
{
int tid = *((int*)threadId);
fprintf(stdout, "Hello world, thread %d\n", tid);
pthread_exit(NULL);
}
仅有的差别就是线程执行函数的参数传递不同,执行改过之后的程序:


我们可以看到程序执行结果完全不同并且不正确
对于修改前:直接传递 变量i 的值,这是值语义,之后线程操作的只是 变量i 的副本,跟原来的 变量i 没有任何关系,没有竞争出现
对于修改后:传递的是 变量i 的地址(地址),这是引用语义,之后线程操作的是 原变量i,这时多个线程就出现了竞争,
因为这时 变量i 的地址是共享内存,对所有线程可见,其余5个线程通过共享内存在读这个变量i,而主线程通过 i++在写这个变量值
这之间并没有任何同步,所以5个线程读取的值并不正确
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h> #define NTHREADS 5 void* busywork(void* ptr)
{
int tid = (int)ptr;
fprintf(stdout, "Thread %d starting...\n", tid);
double result = 0.0;
for (int i = ; i < ; ++i) {
result = result + sin(i) * tan(i);
}
fprintf(stdout, "Thread %d done. Result = %e\n", tid, result);
pthread_exit((void*)ptr);
} int main(int argc, char* argv[])
{
pthread_t thread[NTHREADS];
pthread_attr_t attr; /* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for (int i = ; i < NTHREADS; ++i) {
fprintf(stdout, "Main: creating thread %d\n", i);
int rc = pthread_create(&thread[i], &attr, busywork, (void*)i);
if (rc != ) {
fprintf(stderr, "error:return code from pthread_create is %d\n", rc);
exit(-);
}
} /* Free attribute and wait for the other threads */
void* status;
pthread_attr_destroy(&attr);
for (int i = ; i < NTHREADS; ++i) {
int rc = pthread_join(thread[i], &status);
if (rc != ) {
fprintf(stderr, "error:return code from pthread_join id %d\n", rc);
exit(-);
}
fprintf(stdout, "Main:completed join with thread %d having a status of %d\n", i, (int)status);
}
fprintf(stdout, "Main: program completed. Exiting\n");
pthread_exit(NULL);
}
2.线程可以通过phread_cancel函数来请求取消同一进程中的其它线程
3.进程原语和线程原语的比较

3.线程同步

为了解决这个问题,线程必须使用锁,在同一时间只允许一个线程访问该变量


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> struct foo {
int f_count;
pthread_t f_lock;
/* more stuff here... */
}; struct foo* foo_alloc(void)
{
struct foo* fp = malloc(sizeof(struct foo));
if (fp != NULL) {
fp->f_count = ;
int ret = pthread_mutex_init(&fp->f_lock, NULL);
if (ret != ) {
free(fp);
return NULL;
}
}
return fp;
} /* increase a reference to the object */
void foo_increase(struct foo* fp)
{
assert(fp != NULL);
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
}
/* decrease a reference to the object */
void foo_decrease(struct foo* fp)
{
assert(fp != NULL);
pthread_mutex_lock(&fp->f_lock);
if (--fp->f_count == ) {
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_destroy(&fp->f_lock);
free(fp);
} else {
pthread_mutex_unlock(&fp->f_lock);
}
}
4.避免死锁
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h> #define NHASH 29
#define HASH(fp) (((unsigned long)(fp)) % NHASH) pthread_mutex_t hashlock = PTHREAD_MUTEX_INITIALIZER; struct foo {
struct foo* f_next;
int f_count;
pthread_mutex_t f_lock;
int f_id;
/* more stuff here... */
}; struct foo* fh[NHASH]; struct foo* foo_alloc(void)
{
struct foo* fp = malloc(sizeof(struct foo));
if (fp != NULL) {
fp->f_count = ;
int ret = pthread_mutex_init(&fp->f_lock, NULL);
if (ret != ) {
free(fp);
return NULL;
}
int idx = HASH(fp);
pthread_mutex_lock(&hashlock);
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
pthread_mutex_lock(&fp->f_lock);
pthread_mutex_unlock(&hashlock); /* continue initialization...... */
pthread_mutex_unlock(&fp->f_lock);
}
return fp;
} /* increase a reference to the object */
void foo_increase(struct foo* fp)
{
assert(fp != NULL);
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
} /* find an existing object */
struct foo* foo_find(int id)
{
struct foo* fp;
int idx = HASH(fp);
pthread_mutex_lock(&hashlock);
for (fp = fh[idx]; fp != NULL; fp = fp->f_next) {
if (fp->f_id == id) {
foo_increase(fp);
break;
}
}
pthread_mutex_unlock(&hashlock);
return fp;
} /* decrease a reference to the object */
void foo_decrease(struct foo* fp)
{
assert(fp != NULL);
struct foo* tfp = NULL;
int idx = ;
pthread_mutex_lock(&fp->f_lock);
if (fp->f_count == ) {
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_lock(&hashlock);
pthread_mutex_lock(&fp->f_lock);
/* need to recheck the condition */
if (fp->f_count != ) {
fp->f_count--;
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_unlock(&hashlock);
return;
} /* remove from list */
idx = HASH(fp);
tfp = fh[idx];
if (tfp == fp) {
fh[idx] = fp->f_next;
} else {
while (tfp->f_next != fp) {
tfp = tfp->f_next;
}
tfp->f_next = fp->f_next;
}
pthread_mutex_unlock(&hashlock);
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_destroy(&fp->f_lock);
free(fp); } else {
fp->f_count--;
pthread_mutex_unlock(&fp->f_lock);
}
}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h> #define NHASH 29
#define HASH(fp) (((unsigned long)(fp)) % NHASH) struct foo {
struct foo* f_next; /* protected by hashlock */
int f_count; /* protected by hashlock */
pthread_mutex_t f_lock;
int f_id;
/* more stuff here */
}; struct foo* fh[NHASH];
pthread_mutex_t hashlock = PTHREAD_MUTEX_INITIALIZER; struct foo* foo_alloc(void)
{
int idx = ;
struct foo* fp = malloc(sizeof(struct foo));
if (fp != NULL) {
fp->f_count = ;
int ret = pthread_mutex_init(&fp->f_lock, NULL);
if (ret != ) {
free(fp);
return NULL;
}
idx = HASH(fp);
pthread_mutex_lock(&hashlock);
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
pthread_mutex_lock(&fp->f_count);
pthread_mutex_unlock(&hashlock);
/* continue initialization */
}
return fp;
} void foo_increase(struct foo* fp)
{
assert(fp != NULL);
pthread_mutex_lock(&hashlock);
fp->f_count++;
pthread_mutex_unlock(&hashlock);
} struct foo* foo_find(int id)
{
struct foo* fp = NULL;
int idx = HASH(fp);
pthread_mutex_lock(&hashlock);
for (fp = fh[idx]; fp != NULL; fp = fp->f_next) {
if (fp->f_id == id) {
fp->f_count++;
break;
}
}
pthread_mutex_unlock(&hashlock);
return fp;
} void foo_decrease(struct foo* fp)
{
assert(fp != NULL);
struct foo* tfp = NULL;
int idx = ; pthread_mutex_lock(&hashlock);
if (--fp->f_count == ) {
idx = HASH(fp);
tfp = fh[idx];
if (tfp == fp) {
fh[idx] = fp->f_next;
} else {
while (tfp->f_next != fp) {
tfp = tfp->f_next;
}
tfp->f_next = fp->f_next;
}
pthread_mutex_unlock(&hashlock);
pthread_mutex_destroy(&fp->f_lock);
free(fp);
} else {
pthread_mutex_unlock(&hashlock);
}
}
5.读写锁
6.条件变量
7. 线程属性
#include<pthread.h>
int pthread_attr_init(pthread_attr_t* attr);
int pthread_attr_destroy(pthread_attr_t* attr);

8.互斥量属性
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t* attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t* attr);
9.重入、线程安全
APUE 学习笔记(八) 线程同步的更多相关文章
- APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量
线程同步 同属于一个进程的不同线程是共享内存的,因而在执行过程中需要考虑数据的一致性. 假设:进程有一变量i=0,线程A执行i++,线程B执行i++,那么最终i的取值是多少呢?似乎一定 ...
- APUE学习笔记6——线程和线程同步
1 概念 线程是程序执行流的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的 ...
- linux学习笔记之线程同步机制
一.基础知识. 1:线程同步机制:互斥量,读写锁,条件变量,自旋锁,屏障. 1,互斥量:每个进程访问被互斥量保护的资源时,都需要先对互斥量进行判断. 1)互斥量重要属性:进程共享属性,健壮属性,类型属 ...
- C#学习笔记之线程 - 同步上下文
同步上下文(Synchronization Contexts) 手动使用锁的一个替代方案是去声明锁.通过派生ContextBoundObject和应用Synchronization属性,你告诉CLR自 ...
- Linux学习笔记21——线程同步的两种方式
一 用信号量同步 1 信号量函数的名字都以sem_开头,线程中使用的基本信号量函数有4个 2 创建信号量 #include<semaphore.h> int sem_init(sem_t ...
- APUE学习笔记——11 线程基础
线程标识 线程由线程号进行标识.线程号仅在线程所属的进程环境中有效.也就是说属于不同进程的两个线程可能线程号一样. 线程标识用结构体pthread_t tid表示.与线程Id相关的函数如下: 比较两个 ...
- 操作系统学习笔记----进程/线程模型----Coursera课程笔记
操作系统学习笔记----进程/线程模型----Coursera课程笔记 进程/线程模型 0. 概述 0.1 进程模型 多道程序设计 进程的概念.进程控制块 进程状态及转换.进程队列 进程控制----进 ...
- 【opencv学习笔记八】创建TrackBar轨迹条
createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...
- Java IO学习笔记八:Netty入门
作者:Grey 原文地址:Java IO学习笔记八:Netty入门 多路复用多线程方式还是有点麻烦,Netty帮我们做了封装,大大简化了编码的复杂度,接下来熟悉一下netty的基本使用. Netty+ ...
随机推荐
- python之函数的传参形参的第三种动态参数*args和**kwargs
1. 位置/关键字传参的缺点 当给函数传入的参数数目不定时,之前的传参方式解决不了问题. def eat(food1,food2,food3): print(f'我请你吃:{food1},{food2 ...
- IDEA安装及破解
一.下载(IDEA 2019.1.2) 1.下载地址:https://www.jetbrains.com/idea/download/#section=windows 2.选择版本,并选择最终版(.e ...
- ps基础实例
一:合并多个图片 1.先新件一个图片)CTRL+N),大小定成你想要的大小 2.把你要放入的照片用PS打开 3.把放入的照片用移动工具(V)拉到新件的图片里面 4.用CTRL+T调整大小(按住SHIF ...
- B. Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...
- classpath、WEB-INF
classpath是指 WEB-INF文件夹下的classes目录(war包),对于springboot项目打包出来的jar包,里面的就是BOOT-INF: 这个demo的源码结构如下: 可见,jav ...
- 【windows】共享文件夹设置
控制面板\网络和 Internet\网络和共享中心\高级共享设置 在当前的域\公用\家庭网络 下文件和打印机共享开关打开 现在可以在 计算机-网络 里面看到共享的计算机啦 选中自己想要分享的文件,右键 ...
- 编译与安装 OpenSSL
编译与安装 OpenSSL prefix 是安装目录,openssldir 是配置文件目录,另外建议安装两次,shared 作用是生成动态连接库.linux版的OpenSSL下载地址为:https:/ ...
- poj 3050 地图5位数问题 dfs算法
题意:一个5*5地图上面,从任意位置上下左右跳五次,组成一个数.问:不重复的数有多少个? 思路:dfs 从任意位置跳5次,说明每个位置都需要遍历. 组成一个数:number*10+map[dx][dy ...
- Java学习——面对对象的思想入门
本文是看过<head first Java>之后的一点感悟,写点东西帮忙以后回忆,Java目前在我的工作中用到还不多,而我又对面对对象的编程非常的感兴趣.曾经在MFC平台上写过 ...
- 光学字符识别OCR-5 文本切割
经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...