线程同步是指多个线程之间协调和管理彼此的执行顺序,以避免竞态条件和不确定的结果。线程同步的目的是确保共享资源的正确访问和保护临界区的完整性。
使用线程同步机制可以确保多线程程序的正确性、安全性和可靠性。它能够解决多线程并发访问共享资源时可能出现的问题,并提供了一种有效的方式来协调和管理多个线程的执行。
互斥锁(Mutex):
互斥锁用于保护临界区资源,确保在任意时刻只有一个线程可以访问被保护的资源。当一个线程获得互斥锁后,其他线程需要等待直到该线程释放锁。
读写锁(Read-Write Lock):
读写锁允许多个线程同时对共享资源进行读操作,但只允许一个线程进行写操作。这样可以提高读操作的并发性能。
条件变量(Condition Variable):
条件变量用于线程之间的等待和通知机制。一个线程可以在条件变量上等待,直到其他线程满足某个条件后通知等待的线程。
信号量(Semaphore):
信号量用于控制对共享资源的访问和线程的并发数量。它可以用于限制并发线程的数量或者实现互斥和同步。
互斥锁是线程同步最常用的一种方式,通过互斥锁可以锁定一个代码块,被锁定的这个代码块,所有的线程只能顺序执行(不能并行处理),这样多线程访问共享资源数据混乱的问题就可以被解决了,需要付出的代价就是执行效率的降低,因为默认临界区多个线程是可以并行处理的,现在只能串行处理。
pthread_mutex_t
,创建一个这种类型的变量就得到了一把互斥锁:pthread_mutex_t mutex;
一个互斥锁变量只能被一个线程锁定,被锁定之后其他线程再对互斥锁变量加锁就会被阻塞,直到这把互斥锁被解锁,被阻塞的线程才能被解除阻塞。
一般情况下,每一个共享资源对应一个把互斥锁,锁的个数和线程的个数无关。
初始化互斥锁
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
restrict 是 C 语言中的一个关键字,用于指示指针是唯一访问某个数据对象的指针,这意味着在指针的生命周期内,没有其他别名指针可以访问相同的数据对象。
释放互斥锁
int pthread_mutex_destroy(pthread_mutex_t *mutex);
其中mutex
是互斥锁变量的地址。
加锁和解锁
int pthread_mutex_lock(pthread_mutex_t *mutex);
修改互斥锁的状态, 将其设定为锁定状态, 这个状态被写入到参数 mutex
中。
该函数首先会判断参数 mutex 互斥锁中的状态是不是锁定状态:
没有被锁定,是打开的,这个线程可以加锁成功,这个这个锁中会记录是哪个线程加锁成功了
如果被锁定了,其他线程加锁就失败了,这些线程都会阻塞在这把锁上
当这把锁被解开之后,这些阻塞在锁上的线程就解除阻塞了,并且这些线程是通过竞争的方式对这把锁加锁,没抢到锁的线程继续阻塞。
当某一线程加上互斥锁后,可以看作该互斥锁已被这个线程持有,该线程可以对锁中的全局变量进行修改访问,其他线程无法获取互斥锁,即无法对共享资源进行访问。
不是所有的线程都可以对互斥锁解锁,哪个线程加的锁,哪个线程才能解锁成功。
下面给出一段代码,创建两个线程、两线程交替数数,每个线程数50个数,交替数到100。
#include
#include
#include
#include
#include
#include
#include
#define MAX 50
int number;
pthread_mutex_t mutex;
void* fun1(void *arg)
{
for(int i=0;i<MAX;++i)
{
int cur=number;
cur++;
usleep(10);
number=cur;
printf("Thread A,id = %lu,number=%d\n",pthread_self(),number);
}
return NULL;
}
void* fun2(void *arg)
{
for(int i=0;i<MAX;++i)
{
int cur=number;
cur++;
number=cur;
usleep(10);
printf("Thread B,id = %lu,number=%d\n",pthread_self(),number);
}
return NULL;
}
int main(int argc,const char* argv[])
{
pthread_mutex_init(&mutex,NULL);
pthread_t p1,p2;
pthread_create(&p1,NULL,fun1,NULL);
pthread_create(&p2,NULL,fun2,NULL);
pthread_join(p1,NULL);
pthread_join(p2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
结果如下所示:
Thread A,id = 139713615181568,number=1
Thread B,id = 139713606788864,number=1
Thread B,id = 139713606788864,number=2
Thread A,id = 139713615181568,number=2
Thread B,id = 139713606788864,number=2
Thread A,id = 139713615181568,number=3
Thread B,id = 139713606788864,number=3
Thread A,id = 139713615181568,number=4
Thread B,id = 139713606788864,number=4
Thread A,id = 139713615181568,number=5
Thread B,id = 139713606788864,number=5
Thread A,id = 139713615181568,number=6
Thread A,id = 139713615181568,number=7
Thread B,id = 139713606788864,number=7
Thread B,id = 139713606788864,number=8
Thread A,id = 139713615181568,number=8
Thread A,id = 139713615181568,number=9
Thread B,id = 139713606788864,number=9
Thread B,id = 139713606788864,number=10
Thread A,id = 139713615181568,number=10
Thread A,id = 139713615181568,number=11
Thread B,id = 139713606788864,number=11
Thread B,id = 139713606788864,number=12
Thread A,id = 139713615181568,number=12
Thread A,id = 139713615181568,number=13
Thread B,id = 139713606788864,number=13
Thread B,id = 139713606788864,number=14
Thread A,id = 139713615181568,number=14
Thread A,id = 139713615181568,number=15
Thread B,id = 139713606788864,number=15
Thread B,id = 139713606788864,number=16
Thread A,id = 139713615181568,number=16
Thread A,id = 139713615181568,number=17
Thread B,id = 139713606788864,number=17
Thread A,id = 139713615181568,number=18
Thread B,id = 139713606788864,number=18
Thread B,id = 139713606788864,number=19
Thread A,id = 139713615181568,number=19
Thread B,id = 139713606788864,number=19
Thread A,id = 139713615181568,number=20
Thread A,id = 139713615181568,number=21
Thread B,id = 139713606788864,number=21
Thread A,id = 139713615181568,number=22
Thread B,id = 139713606788864,number=22
Thread B,id = 139713606788864,number=23
Thread A,id = 139713615181568,number=23
Thread A,id = 139713615181568,number=24
Thread B,id = 139713606788864,number=24
Thread B,id = 139713606788864,number=25
Thread A,id = 139713615181568,number=25
Thread A,id = 139713615181568,number=26
Thread B,id = 139713606788864,number=26
Thread B,id = 139713606788864,number=27
Thread A,id = 139713615181568,number=27
Thread A,id = 139713615181568,number=28
Thread B,id = 139713606788864,number=28
Thread B,id = 139713606788864,number=29
Thread A,id = 139713615181568,number=29
Thread A,id = 139713615181568,number=30
Thread B,id = 139713606788864,number=30
Thread B,id = 139713606788864,number=31
Thread A,id = 139713615181568,number=31
Thread A,id = 139713615181568,number=32
Thread B,id = 139713606788864,number=32
Thread B,id = 139713606788864,number=33
Thread A,id = 139713615181568,number=33
Thread A,id = 139713615181568,number=34
Thread B,id = 139713606788864,number=34
Thread B,id = 139713606788864,number=35
Thread A,id = 139713615181568,number=35
Thread A,id = 139713615181568,number=36
Thread B,id = 139713606788864,number=36
Thread B,id = 139713606788864,number=37
Thread A,id = 139713615181568,number=37
Thread A,id = 139713615181568,number=38
Thread B,id = 139713606788864,number=38
Thread B,id = 139713606788864,number=39
Thread A,id = 139713615181568,number=39
Thread A,id = 139713615181568,number=40
Thread B,id = 139713606788864,number=40
Thread B,id = 139713606788864,number=41
Thread A,id = 139713615181568,number=41
Thread A,id = 139713615181568,number=42
Thread B,id = 139713606788864,number=42
Thread B,id = 139713606788864,number=43
Thread A,id = 139713615181568,number=43
Thread A,id = 139713615181568,number=44
Thread B,id = 139713606788864,number=44
Thread B,id = 139713606788864,number=45
Thread A,id = 139713615181568,number=45
Thread A,id = 139713615181568,number=46
Thread B,id = 139713606788864,number=46
Thread B,id = 139713606788864,number=47
Thread A,id = 139713615181568,number=47
Thread A,id = 139713615181568,number=48
Thread B,id = 139713606788864,number=48
Thread B,id = 139713606788864,number=49
Thread A,id = 139713615181568,number=49
Thread A,id = 139713615181568,number=50
Thread B,id = 139713606788864,number=50
在代码中,两个线程fun1和fun2都对number进行了读取和修改操作,而没有进行同步控制,导致它们可能同时访问和修改number,从而引发竞态条件(Race Condition)问题。
为了解决这个问题,可以在对number的读取和修改操作之前加上互斥锁的加锁和解锁操作,以确保每次只有一个线程在访问和修改number
。
#include
#include
#include
#include
#include
#include
#include
#define MAX 50
int number;
pthread_mutex_t mutex;
void print_mutex_type() {
int type;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_gettype(&attr, &type);
if (type == PTHREAD_MUTEX_NORMAL)
printf("Mutex Type: Normal Mutex\n");
else if (type == PTHREAD_MUTEX_RECURSIVE)
printf("Mutex Type: Recursive Mutex\n");
else if (type == PTHREAD_MUTEX_ERRORCHECK)
printf("Mutex Type: Error-Checking Mutex\n");
else
printf("Mutex Type: Unknown\n");
pthread_mutexattr_destroy(&attr);
}
void* fun1(void *arg)
{
for(int i=0;i<MAX;++i)
{
pthread_mutex_lock(&mutex);
int cur=number;
cur++;
usleep(10);
number=cur;
printf("Thread A,id = %lu,number=%d\n",pthread_self(),number);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void* fun2(void *arg)
{
for(int i=0;i<MAX;++i)
{
pthread_mutex_lock(&mutex);
int cur=number;
cur++;
number=cur;
usleep(10);
printf("Thread B,id = %lu,number=%d\n",pthread_self(),number);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc,const char* argv[])
{
print_mutex_type();
pthread_mutex_init(&mutex,NULL);
pthread_t p1,p2;
pthread_create(&p1,NULL,fun1,NULL);
pthread_create(&p2,NULL,fun2,NULL);
pthread_join(p1,NULL);
pthread_join(p2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果如下所示:
Thread A,id = 139723581155072,number=1
Thread A,id = 139723581155072,number=2
Thread A,id = 139723581155072,number=3
Thread A,id = 139723581155072,number=4
Thread A,id = 139723581155072,number=5
Thread A,id = 139723581155072,number=6
Thread A,id = 139723581155072,number=7
Thread A,id = 139723581155072,number=8
Thread A,id = 139723581155072,number=9
Thread A,id = 139723581155072,number=10
Thread A,id = 139723581155072,number=11
Thread A,id = 139723581155072,number=12
Thread A,id = 139723581155072,number=13
Thread A,id = 139723581155072,number=14
Thread A,id = 139723581155072,number=15
Thread A,id = 139723581155072,number=16
Thread A,id = 139723581155072,number=17
Thread A,id = 139723581155072,number=18
Thread A,id = 139723581155072,number=19
Thread A,id = 139723581155072,number=20
Thread A,id = 139723581155072,number=21
Thread A,id = 139723581155072,number=22
Thread A,id = 139723581155072,number=23
Thread A,id = 139723581155072,number=24
Thread A,id = 139723581155072,number=25
Thread A,id = 139723581155072,number=26
Thread A,id = 139723581155072,number=27
Thread A,id = 139723581155072,number=28
Thread A,id = 139723581155072,number=29
Thread A,id = 139723581155072,number=30
Thread A,id = 139723581155072,number=31
Thread A,id = 139723581155072,number=32
Thread A,id = 139723581155072,number=33
Thread A,id = 139723581155072,number=34
Thread A,id = 139723581155072,number=35
Thread A,id = 139723581155072,number=36
Thread A,id = 139723581155072,number=37
Thread A,id = 139723581155072,number=38
Thread A,id = 139723581155072,number=39
Thread A,id = 139723581155072,number=40
Thread A,id = 139723581155072,number=41
Thread A,id = 139723581155072,number=42
Thread A,id = 139723581155072,number=43
Thread A,id = 139723581155072,number=44
Thread A,id = 139723581155072,number=45
Thread A,id = 139723581155072,number=46
Thread A,id = 139723581155072,number=47
Thread A,id = 139723581155072,number=48
Thread A,id = 139723581155072,number=49
Thread A,id = 139723581155072,number=50
Thread B,id = 139723572762368,number=51
Thread B,id = 139723572762368,number=52
Thread B,id = 139723572762368,number=53
Thread B,id = 139723572762368,number=54
Thread B,id = 139723572762368,number=55
Thread B,id = 139723572762368,number=56
Thread B,id = 139723572762368,number=57
Thread B,id = 139723572762368,number=58
Thread B,id = 139723572762368,number=59
Thread B,id = 139723572762368,number=60
Thread B,id = 139723572762368,number=61
Thread B,id = 139723572762368,number=62
Thread B,id = 139723572762368,number=63
Thread B,id = 139723572762368,number=64
Thread B,id = 139723572762368,number=65
Thread B,id = 139723572762368,number=66
Thread B,id = 139723572762368,number=67
Thread B,id = 139723572762368,number=68
Thread B,id = 139723572762368,number=69
Thread B,id = 139723572762368,number=70
Thread B,id = 139723572762368,number=71
Thread B,id = 139723572762368,number=72
Thread B,id = 139723572762368,number=73
Thread B,id = 139723572762368,number=74
Thread B,id = 139723572762368,number=75
Thread B,id = 139723572762368,number=76
Thread B,id = 139723572762368,number=77
Thread B,id = 139723572762368,number=78
Thread B,id = 139723572762368,number=79
Thread B,id = 139723572762368,number=80
Thread B,id = 139723572762368,number=81
Thread B,id = 139723572762368,number=82
Thread B,id = 139723572762368,number=83
Thread B,id = 139723572762368,number=84
Thread B,id = 139723572762368,number=85
Thread B,id = 139723572762368,number=86
Thread B,id = 139723572762368,number=87
Thread B,id = 139723572762368,number=88
Thread B,id = 139723572762368,number=89
Thread B,id = 139723572762368,number=90
Thread B,id = 139723572762368,number=91
Thread B,id = 139723572762368,number=92
Thread B,id = 139723572762368,number=93
Thread B,id = 139723572762368,number=94
Thread B,id = 139723572762368,number=95
Thread B,id = 139723572762368,number=96
Thread B,id = 139723572762368,number=97
Thread B,id = 139723572762368,number=98
Thread B,id = 139723572762368,number=99
Thread B,id = 139723572762368,number=100
读写锁是互斥锁的升级版,在做读操作的时候可以提高程序的执行效率,如果所有的线程都是做读操作, 那么读是并行的,但是使用互斥锁,读操作也是串行的。
读写锁是一把锁,锁的类型为 pthread_rwlock_t,有了类型之后就可以创建一把读写锁了,其创建、销毁、加锁解锁方式与互斥锁类似:
pthread_rwlock_t rwlock;
读写锁类型为pthread_rwlock_t
,它并不能分为读锁和写锁,但是可以进行读操作锁定和写操作锁定。
读写锁初始化及释放
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
加读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
调用这个函数,如果读写锁是打开的,那么加锁成功;如果读写锁已经锁定了读操作,调用这个函数依然可以加锁成功,因为读锁是共享的;如果读写锁已经锁定了写操作,调用这个函数的线程会被阻塞
加写锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
调用这个函数,如果读写锁是打开的,那么加锁成功;如果读写锁已经锁定了读操作或者锁定了写操作,调用这个函数的线程会被阻塞。
解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
不管锁定了读还是写都可用该函数进行解锁。
8 个线程操作同一个全局变量,3 个线程不定时写同一全局资源,5 个线程不定时读同一全局资源。
#include
#include
#include
#include
#include
#include
#include
#define MAX 50
int number;
pthread_rwlock_t rw;
void* read_num(void* arg)
{
for(int i=0;i<MAX;i++)
{
pthread_rwlock_rdlock(&rw);
printf("Thread read,id=%lu,number=%d\n",pthread_self(),number);
pthread_rwlock_unlock(&rw);
usleep(rand()%5);
}
return NULL;
}
void* write_num(void* arg)
{
for(int i=0;i<MAX;i++)
{
pthread_rwlock_wrlock(&rw);
int cur=number;
cur++;
usleep(15);
number=cur;
printf("Thread write,id = %lu,number=%d\n",pthread_self(),number);
pthread_rwlock_unlock(&rw);
usleep(rand()%5);
}
return NULL;
}
int main(int argc,const char* argv[])
{
pthread_rwlock_init(&rw,NULL);
pthread_t p1[5],p2[3];
for(int i=0;i<5;i++)
{
pthread_create(&p1[i],NULL,read_num,NULL);
}
for(int i=0;i<3;i++)
{
pthread_create(&p2[i],NULL,write_num,NULL);
}
for(int i=0;i<3;i++)
{
pthread_join(p2[i],NULL);
}
for(int i=0;i<5;i++)
{
pthread_join(p1[i],NULL);
}
pthread_rwlock_destroy(&rw);
return 0;
}
运行结果如下所示:
Thread read,id=139752767919872,number=0
Thread read,id=139752759527168,number=0
Thread read,id=139752751134464,number=0
Thread read,id=139752742741760,number=0
Thread read,id=139752734349056,number=0
Thread write,id = 139752725956352,number=1
Thread write,id = 139752717563648,number=2
Thread write,id = 139752709170944,number=3
Thread write,id = 139752725956352,number=4
Thread write,id = 139752717563648,number=5
Thread write,id = 139752709170944,number=6
Thread write,id = 139752725956352,number=7
Thread write,id = 139752717563648,number=8
Thread write,id = 139752725956352,number=9
Thread write,id = 139752709170944,number=10
Thread write,id = 139752717563648,number=11
Thread write,id = 139752725956352,number=12
Thread write,id = 139752709170944,number=13
Thread write,id = 139752717563648,number=14
Thread write,id = 139752725956352,number=15
Thread write,id = 139752709170944,number=16
Thread write,id = 139752717563648,number=17
Thread write,id = 139752725956352,number=18
Thread write,id = 139752709170944,number=19
Thread write,id = 139752717563648,number=20
Thread write,id = 139752725956352,number=21
Thread write,id = 139752709170944,number=22
Thread write,id = 139752717563648,number=23
Thread write,id = 139752725956352,number=24
Thread write,id = 139752709170944,number=25
Thread write,id = 139752717563648,number=26
Thread write,id = 139752725956352,number=27
Thread write,id = 139752709170944,number=28
Thread write,id = 139752717563648,number=29
Thread write,id = 139752725956352,number=30
Thread write,id = 139752709170944,number=31
Thread write,id = 139752717563648,number=32
Thread write,id = 139752725956352,number=33
Thread write,id = 139752709170944,number=34
Thread write,id = 139752725956352,number=35
Thread write,id = 139752717563648,number=36
Thread write,id = 139752709170944,number=37
Thread write,id = 139752725956352,number=38
Thread write,id = 139752717563648,number=39
Thread write,id = 139752709170944,number=40
Thread write,id = 139752725956352,number=41
Thread write,id = 139752717563648,number=42
Thread write,id = 139752709170944,number=43
Thread write,id = 139752725956352,number=44
Thread write,id = 139752717563648,number=45
Thread write,id = 139752709170944,number=46
Thread write,id = 139752725956352,number=47
Thread write,id = 139752717563648,number=48
Thread write,id = 139752709170944,number=49
Thread write,id = 139752725956352,number=50
Thread write,id = 139752717563648,number=51
Thread write,id = 139752725956352,number=52
Thread write,id = 139752709170944,number=53
Thread write,id = 139752717563648,number=54
Thread write,id = 139752725956352,number=55
Thread write,id = 139752709170944,number=56
Thread write,id = 139752717563648,number=57
Thread write,id = 139752725956352,number=58
Thread write,id = 139752709170944,number=59
Thread write,id = 139752717563648,number=60
Thread write,id = 139752725956352,number=61
Thread write,id = 139752709170944,number=62
Thread write,id = 139752717563648,number=63
Thread write,id = 139752709170944,number=64
Thread write,id = 139752717563648,number=65
Thread write,id = 139752725956352,number=66
Thread write,id = 139752709170944,number=67
Thread write,id = 139752717563648,number=68
Thread write,id = 139752725956352,number=69
Thread write,id = 139752709170944,number=70
Thread write,id = 139752717563648,number=71
Thread write,id = 139752725956352,number=72
Thread write,id = 139752709170944,number=73
Thread write,id = 139752717563648,number=74
Thread write,id = 139752725956352,number=75
Thread write,id = 139752709170944,number=76
Thread write,id = 139752717563648,number=77
Thread write,id = 139752725956352,number=78
Thread write,id = 139752709170944,number=79
Thread write,id = 139752717563648,number=80
Thread write,id = 139752725956352,number=81
Thread write,id = 139752709170944,number=82
Thread write,id = 139752717563648,number=83
Thread write,id = 139752725956352,number=84
Thread write,id = 139752709170944,number=85
Thread write,id = 139752717563648,number=86
Thread write,id = 139752725956352,number=87
Thread write,id = 139752709170944,number=88
Thread write,id = 139752717563648,number=89
Thread write,id = 139752725956352,number=90
Thread write,id = 139752709170944,number=91
Thread write,id = 139752717563648,number=92
Thread write,id = 139752725956352,number=93
Thread write,id = 139752709170944,number=94
Thread write,id = 139752717563648,number=95
Thread write,id = 139752725956352,number=96
Thread write,id = 139752709170944,number=97
Thread write,id = 139752717563648,number=98
Thread write,id = 139752725956352,number=99
Thread write,id = 139752709170944,number=100
Thread write,id = 139752717563648,number=101
Thread write,id = 139752725956352,number=102
Thread write,id = 139752709170944,number=103
Thread write,id = 139752717563648,number=104
Thread write,id = 139752725956352,number=105
Thread write,id = 139752709170944,number=106
Thread write,id = 139752717563648,number=107
Thread write,id = 139752725956352,number=108
Thread write,id = 139752709170944,number=109
Thread write,id = 139752717563648,number=110
Thread write,id = 139752725956352,number=111
Thread write,id = 139752717563648,number=112
Thread write,id = 139752709170944,number=113
Thread write,id = 139752725956352,number=114
Thread write,id = 139752717563648,number=115
Thread write,id = 139752709170944,number=116
Thread write,id = 139752725956352,number=117
Thread write,id = 139752709170944,number=118
Thread write,id = 139752717563648,number=119
Thread write,id = 139752725956352,number=120
Thread write,id = 139752709170944,number=121
Thread write,id = 139752717563648,number=122
Thread write,id = 139752725956352,number=123
Thread write,id = 139752709170944,number=124
Thread write,id = 139752717563648,number=125
Thread write,id = 139752725956352,number=126
Thread write,id = 139752709170944,number=127
Thread write,id = 139752717563648,number=128
Thread write,id = 139752725956352,number=129
Thread write,id = 139752709170944,number=130
Thread write,id = 139752717563648,number=131
Thread write,id = 139752725956352,number=132
Thread write,id = 139752709170944,number=133
Thread write,id = 139752717563648,number=134
Thread write,id = 139752725956352,number=135
Thread write,id = 139752709170944,number=136
Thread write,id = 139752717563648,number=137
Thread write,id = 139752725956352,number=138
Thread write,id = 139752709170944,number=139
Thread write,id = 139752717563648,number=140
Thread write,id = 139752725956352,number=141
Thread write,id = 139752709170944,number=142
Thread write,id = 139752717563648,number=143
Thread write,id = 139752725956352,number=144
Thread write,id = 139752709170944,number=145
Thread write,id = 139752717563648,number=146
Thread write,id = 139752725956352,number=147
Thread write,id = 139752709170944,number=148
Thread write,id = 139752717563648,number=149
Thread read,id=139752759527168,number=149
Thread read,id=139752751134464,number=149
Thread read,id=139752767919872,number=149
Thread read,id=139752742741760,number=149
Thread read,id=139752734349056,number=149
Thread write,id = 139752709170944,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752767919872,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752742741760,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752751134464,number=150
Thread read,id=139752759527168,number=150
Thread read,id=139752734349056,number=150
Thread read,id=139752767919872,number=150
读线程可以同时进行读取操作,但不能与写线程同时进行写操作,以保证数据的一致性和线程的安全性。