【Linux】线程同步(互斥锁和读写锁)

news2024/11/18 8:24:24

概念

线程同步是指多个线程之间协调和管理彼此的执行顺序,以避免竞态条件和不确定的结果。线程同步的目的是确保共享资源的正确访问和保护临界区的完整性。

作用

  • 避免竞态条件:当多个线程同时访问和修改共享资源时,可能会导致竞态条件的发生。竞态条件指的是多个线程之间的执行顺序和时序不确定,导致结果的不确定性和不正确性。使用线程同步机制可以避免竞态条件的发生,保证共享资源的正确访问顺序,从而避免不确定的结果。
  • 保护临界区:临界区是指在多线程环境中访问共享资源的代码段。在临界区中,只允许一个线程访问共享资源,其他线程需要等待。通过使用线程同步机制,比如互斥锁或读写锁,可以保护临界区的完整性,确保同时只有一个线程可以进入临界区,避免数据的不一致和错误的操作。
  • 实现线程间的协作和通信:在某些情况下,多个线程需要相互协作完成某个任务,或者进行线程间的通信和数据交换。线程同步机制,如条件变量、信号量和屏障,可以提供线程间的等待和通知机制,确保线程在适当的时机等待或继续执行,从而实现线程间的协作和通信。
  • 提高性能和效率:在某些情况下,合理使用线程同步机制可以提高多线程程序的性能和效率。例如,在读写操作频繁的场景中,使用读写锁可以允许多个线程同时进行读操作,提高并发性能。此外,使用原子操作可以避免显式的锁竞争,提高程序的执行效率。

使用线程同步机制可以确保多线程程序的正确性、安全性和可靠性。它能够解决多线程并发访问共享资源时可能出现的问题,并提供了一种有效的方式来协调和管理多个线程的执行。

线程同步的方式

  • 互斥锁(Mutex):

    互斥锁用于保护临界区资源,确保在任意时刻只有一个线程可以访问被保护的资源。当一个线程获得互斥锁后,其他线程需要等待直到该线程释放锁。

  • 读写锁(Read-Write Lock):

    读写锁允许多个线程同时对共享资源进行读操作,但只允许一个线程进行写操作。这样可以提高读操作的并发性能。

  • 条件变量(Condition Variable):

    条件变量用于线程之间的等待和通知机制。一个线程可以在条件变量上等待,直到其他线程满足某个条件后通知等待的线程。

  • 信号量(Semaphore):

    信号量用于控制对共享资源的访问和线程的并发数量。它可以用于限制并发线程的数量或者实现互斥和同步。

互斥锁

互斥锁是线程同步最常用的一种方式,通过互斥锁可以锁定一个代码块,被锁定的这个代码块,所有的线程只能顺序执行(不能并行处理),这样多线程访问共享资源数据混乱的问题就可以被解决了,需要付出的代价就是执行效率的降低,因为默认临界区多个线程是可以并行处理的,现在只能串行处理。

互斥锁的使用

  • 在 Linux 中互斥锁的类型为 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<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<pthread.h>
#include <typeinfo>
#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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<pthread.h>
#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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<pthread.h>

#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

在这里插入图片描述
可以看到,读操作可以同步进行,写操作只能串行进行。

读线程可以同时进行读取操作,但不能与写线程同时进行写操作,以保证数据的一致性和线程的安全性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/700089.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

初创企业办公室租赁现状

概述&#xff1a; 随着创业生态的不断发展&#xff0c;越来越多的初创企业开始涌现。租赁办公室是初创企业成立和运营的必要条件之一&#xff0c;然而&#xff0c;由于各种原因&#xff0c;租赁办公室对于初创企业来说仍然存在一些挑战和难点。本文将探讨初创企业租赁办公室的…

功能测试的技术

目录 前言&#xff1a; 1) 基于最终用户/系统测试 2) 等价测试 3) 边界值测试 4) 基于决策的测试 5) 备用流量测试 6) 临时测试 前言&#xff1a; 功能测试是软件测试中最常见的一种测试类型&#xff0c;它旨在验证系统的功能是否符合设计要求和预期行为。在进行功能测…

a==1a==2a==3 与 a===1a===2a===3如何实现?

前言 首先&#xff0c;我们来看个demo let a {value: 1,toString() {// console.log("toString")return this.value;} }看一下输出结果&#xff1a; console.log(a 1 && a 2 && a 3) // falseconsole.log(a 1 && a 2 && a …

PHP 旅游信息管理系统mysql数据库web结构apache计算机软件工程网页wamp

一、源码特点 PHP 旅游信息管理系统 是一套完善的web设计系统&#xff0c;对理解php编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为PHP APACHE&#xff0c;数据库 为mysql5.0&#xff0c;使用php语言开发。 …

nerf-studio初体验

前言 整个安装过程非常简单&#xff0c;运行得也很丝滑&#xff0c;最大的难点在于中文资料太少&#xff0c;英文看得头痛。本文也是参考了许多链接&#xff0c;有些链接比我写得详细多了&#xff0c;各位选择性看吧 安装 安装过程参考github链接&#xff0c;按顺序一步一步…

使用 ZBrush 和 Substance 3D中创建逼真的露西(p2)

今天瑞云渲染小编给带来了Rafael Benedicto 分享的 Lucy 项目背后工作流程&#xff0c;解释了头发是如何设置的&#xff0c;并回顾了在 Substance 3D Painter 中完成的纹理化过程。篇幅较长&#xff0c;分上下两篇来阐述幕后花絮。接下来接着讲述Lucy项目的纹理和渲染方面内容。…

PS 设置图片背景透明

找魔棒工具&#xff1a;编辑-工具栏。可设置w改为Z。&#xff08;方便使用&#xff09; 魔棒工具选择背景&#xff08;w&#xff09; 删除即可 如果我们导入的是“智能对象”&#xff0c;使用左侧的“矩形选区”删除框选区域的时候&#xff0c;就会弹出“无法完成请求&#…

pytorch量化库使用(1)

量化简介 量化是指以低于浮点精度的位宽执行计算和存储张量的技术。量化模型以降低的精度而不是全精度&#xff08;浮点&#xff09;值对张量执行部分或全部运算。这允许更紧凑的模型表示以及在许多硬件平台上使用高性能矢量化操作。与典型的 FP32 模型相比&#xff0c;PyTorc…

OnlyOffice7.3.2.8 和 7.3.3.40 Docker魔改版搭建教程

写在前面&#xff1a; 搭建项目所需环境只需查看此篇文档即可&#xff0c;中间有不清楚的地方评论即可&#xff0c;看到会回复&#xff01; ⼀ 、安装docker&#xff1a; 具体步骤请参照docker官网: https://docs.docker.com/get-docker/ 注&#xff1a; 不同操作系统安装方…

今天分享:配音软件哪个好

在数字化时代&#xff0c;视频内容的需求愈发增长。越来越多的人们创作和分享各种类型的视频&#xff0c;而其中一个重要的元素是声音。然而&#xff0c;有时候我们可能面临着一种情况&#xff1a;我们拍摄了一段令人惊艳的视频&#xff0c;但缺乏适合的配音或原声录音。这时&a…

人工智能时代,前端如何抓住机会

自从 2022 年底 OpenAI 推出了 ChatGPT3.5 后&#xff0c;GPT 的活跃用户数快速突破一亿&#xff0c;打破了互联网应用发展的历史记录。ChatGPT是一种基于人工智能技术的聊天机器人&#xff0c;它可以理解人类的自然语言&#xff0c;模拟人类的语言和思维方式&#xff0c;与人类…

介绍几款在线编程工具(Python)

阅读原文 有时候个人电脑不在身边&#xff0c;又需要处理一些工作&#xff0c;这时候可能需要在朋友的电脑或者公用电脑上操作数据。又或者要将自己写的代码以 notebook 的形式分享给 co-worker&#xff0c;这时就需要用到以下总结的几个直接在浏览器里进行 Python 编程的工具。…

怎么学习数据库连接与操作? - 易智编译EaseEditing

学习数据库连接与操作可以按照以下步骤进行&#xff1a; 理解数据库基础知识&#xff1a; 在学习数据库连接与操作之前&#xff0c;首先要了解数据库的基本概念、组成部分和工作原理。 学习关系型数据库和非关系型数据库的区别&#xff0c;了解常见的数据库管理系统&#xff…

为何及如何使用数据结构提升算法效率和问题解决能力?

数据结构是计算机科学中的一个重要概念&#xff0c;它是一种组织和存储数据的方式。数据结构提供了一种在计算机程序中有效地组织和操作数据的方法。 数据结构的主要目的是解决问题和优化算法。它们帮助我们在计算机内存中存储和组织数据&#xff0c;以便能够高效地访问和操作这…

Flink 自定义源算子之 读取MySQL

1、功能说明&#xff1a; 在Flink 自定义源算子中封装jdbc来读取MySQL中的数据 2、代码示例 Flink版本说明&#xff1a;flink_1.13.0、scala_2.12 自定义Source算子&#xff0c;这里我们继承RichParallelSourceFunction&#xff0c;因为要使用open方法来初始化数据库连接对…

Egg.js阿里JS后端框架,可以放心用。

目录 一、快速开始 二、尝试创建一个controll,修改路由&#xff0c;然后检查测试单元。 一、快速开始 npm install -g yarn yarn create egg --typesimple cd egg yarn install yarn devhttp://127.0.0.1:7001 二、尝试创建一个controll,修改路由&#xff0c;然后检查测试单…

PDF怎么转图片?PDF转图片的方法分享!​

PDF怎么转图片呢&#xff1f;相信很多人都会觉得PDF的非常的好用&#xff0c;小编也是被身边很多朋友推荐过后用了这个软件。但很多人在使用的时候有疑问&#xff0c;比如说PDF如何转图片&#xff1f;这难倒不少人&#xff0c;那么今天这篇文章就带你解析PDF怎么转图片&#xf…

LiangGaRy-学习笔记-Day25

1、Apache web相关 1.1、curl命令 作用&#xff1a;用来与服务器之间传输数据的工具 官网&#xff1a;https://curl.se支持很多种协议 语法&#xff1a;curl选项网址 选项&#xff1a; -A&#xff1a;设置代理给服务器-I&#xff08;大写i&#xff09;&#xff1a;输出返…

Spring Boot 中的 WebMvc 是什么,原理,如何使用

Spring Boot 中的 WebMvc 是什么&#xff0c;原理&#xff0c;如何使用 介绍 在 Spring Boot 中&#xff0c;WebMvc 是非常重要的一个模块。它提供了一系列用于处理 Web 请求的组件和工具。在本文中&#xff0c;我们将介绍 Spring Boot 中的 WebMvc 是什么&#xff0c;其原理…

Python+ddt+Excel实现接口自动化测试生成完美测试报告

接口自动化测试是指通过编写代码或使用工具&#xff0c;模拟用户发送请求&#xff0c;验证接口是否符合设计规范和功能需求的过程。” 如何用 python ddtexcel 实现接口自动化测试 接口自动化测试可以提高测试效率和质量&#xff0c;节省测试成本和时间&#xff0c;保证测试覆…