摘要: 同步: //account.h #ifndef _ACCOUNT_H #define _ACCOUNT_H #include <pthread.h> #include <semaphore.h> typedef struct{ int code; double balance; //定义一把互斥锁, 阅读全文
posted @ 2023-03-26 23:23 踏浪而来的人 阅读(9) 评论(0) 推荐(0) 编辑
摘要: //reader_writer.c #include <pthread.h> #include <stdio.h> #include <unistd.h> /* 写者写完通知读者去读 读者读完通知写者去写 */ typedef struct{ int value; int r_wait; pthre 阅读全文
posted @ 2023-03-26 22:45 踏浪而来的人 阅读(10) 评论(0) 推荐(0) 编辑
摘要: //account.h #ifndef _ACCOUNT_H #define _ACCOUNT_H #include <pthread.h> typedef struct{ int code; double balance; //定义一把互斥锁,用来对多线程操作的银行账户(共享资源)进行加锁(保护) 阅读全文
posted @ 2023-03-26 18:27 踏浪而来的人 阅读(10) 评论(0) 推荐(0) 编辑
摘要: //account.h #ifndef _ACCOUNT_H #define _ACCOUNT_H #include <pthread.h> typedef struct{ int code; double balance; //定义一把互斥锁,用来对多线程操作的银行账户(共享资源)进行加锁(保护) 阅读全文
posted @ 2023-03-26 17:14 踏浪而来的人 阅读(24) 评论(0) 推荐(0) 编辑
摘要: CPU对变量的计算分为三个步骤: 1. 从内存读取变量到寄存器(如add寄存器) 2. 将读取到的变量进行计算 3. 将寄存器中计算的值写入内存 如有两个线程(线程1和线程2)对共享资源(全局变量)a=0进行自增操作(a++),可能出现如下情况: 线程1进行a++时,会将变量a读取寄存器,在对变量a 阅读全文
posted @ 2023-03-26 10:46 踏浪而来的人 阅读(75) 评论(0) 推荐(0) 编辑