【Linux】线程同步(一)

news2024/11/15 8:28:18

概念

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

作用

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

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

线程同步的方式

  • 互斥锁(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/697066.html

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

相关文章

javaee session的创建

当访问服务器时&#xff0c;服务器就会创建一个session TestSession.java package com.yyy.servlet;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import java…

String、反射、枚举、lambda表达式以及泛型进阶(数据结构系列16)

目录 前言&#xff1a; 1. String 1.1 字符串常量池 1.1.1 创建对象的思考 1.1.2 字符串常量池&#xff08;StringTable&#xff09; 1.1.3 再谈String对象创建 1.1.4 intern方法 2. 反射 2.1 反射的定义 2.2 反射的用途 2.3 反射的基本信息 2.4 反射相关的类 2.4.…

Android实现发送短信功能

Android发送短信 效果图代码实现先添加权限.java由用户输入手机号 自定义短信内容跳转系统发送短信页面 自定义手机号短信内容全部代码 .xml 效果图 代码实现 先添加权限 <!-- 允许程序发送SMS短信 --><uses-permission android:name"android.permission.SEND_S…

nodejs线上环境远程调试

将nodejs部署到线上环境之后&#xff0c;这个时候想要去调试还是有点困难的&#xff0c;但是好在nodejs提供了一个可以让你进行调试的开关&#xff1a;--inspect 我们可以使用这个开关来启动一个可以调试的服务&#xff0c;使用非常简单&#xff0c;在启动服务的时候加上这个指…

高性能计算怎么入门?

如果入门没有专业方向&#xff0c;自学摸索的话&#xff0c;不容易有自己的核心竞争力。国内目前生态尚不完善&#xff0c;学习资料也比较少~如果想要系统学习的话&#xff0c;一定不要错过国内首家专业做高性能计算的系统学习课程。 ~我们是国内首家做高性能计算人才培养和推…

微型逆变器会不会迎来CC2340时代

光伏领域的朋友对微型逆变器并不陌生。而CC2340&#xff0c;对大家而言则都是陌生的主。它是何方神仙&#xff1f;和微型逆变器能有什么关系&#xff1f;你还说它可能引领微型逆变器的下一个时代&#xff1f;不急&#xff0c;我们一起来看看。 在全球为双碳愿景努力以及俄乌战…

VR虚拟展会——打造商企展厅线上展示新模式

近期的大雨磅礴&#xff0c;不知道大家都感受过吗&#xff1f;就连在武汉展开的国际连锁加盟展产业博览会也都受到了一定的影响&#xff0c;为期三天的展会&#xff0c;接连下了三天的雨&#xff0c;导致很多客户无缘展会。这就是实体展会的限制之一&#xff0c;加上实体展会的…

vscode修改markdown侧边预览pdf字体等设置

文章目录 1.按CtrlShiftP打开命令窗口2.在命令窗口出输入Markdown Preview Enhanced: Customize Css&#xff0c;打开style.less文件 1.按CtrlShiftP打开命令窗口 2.在命令窗口出输入Markdown Preview Enhanced: Customize Css&#xff0c;打开style.less文件 然后在文件内加…

【网站维护】网络杂谈(7)之web网站的维护

涉及知识点 如何进行web网站的维护&#xff0c;如何进行web网站的更新与测试&#xff0c;搭建web网站后期如何管理维护。 原创于&#xff1a;CSDN博主-《拄杖盲学轻声码》&#xff0c;更多内容可去其主页关注下哈&#xff0c;不胜感激 文章目录 涉及知识点前言1&#xff0e;网…

仿牛客社区——7.19生成长图

采用异步方式&#xff0c;通过任务方式&#xff0c;将次命令丢到消息队列中进行执行 关于wkhtmltopdf的配置&#xff08;applaction.properties&#xff09; #关于wk的配置&#xff08;生成长图 wk.image.commandD:/wkhtmltopdf/bin/wkhtmltoimage wk.image.storageD:/wkData/w…

婴儿摇篮音乐芯片 N9300-S16:为宝宝带来高品质的音乐体验

​对于父母来说&#xff0c;给婴儿提供一个安稳舒适的睡眠环境是至关重要的。宝宝的睡眠品质对于其健康和发展至关重要。在成长过程中&#xff0c;音乐对婴儿的情绪、认知和智力发展都有积极的影响。因此&#xff0c;厂家在婴儿摇篮中选择一款合适的婴儿摇篮音乐芯片尤为重要。…

共见·价值成就|亚马逊云科技中国峰会宣布三大举措全面升级

2023年6月27日&#xff0c;亚马逊云科技举办一年一度的中国合作伙伴峰会。本届峰会以“共见价值成就”为主题&#xff0c;面向合作伙伴发布智荟出海计划、可持续发展伙伴计划、合作伙伴解决方案工厂以及获客激励计划等多项计划&#xff0c;以进一步强化合作伙伴“33战略”&…

ASEMI代理ST可控硅BTA16的工作原理与应用分析

编辑-Z 本文将对可控硅BTA16的工作原理与应用进行详细的分析。首先&#xff0c;我们将介绍可控硅BTA16的基本概念和工作原理&#xff0c;然后&#xff0c;我们将探讨其在电力电子设备中的应用&#xff0c;接着&#xff0c;我们将分析其在电力调节中的作用&#xff0c;最后&…

edge自带断网游戏

在没有网络时你会不会很无聊&#xff1f;博主告诉你一个edge浏览器自带的断网小游戏&#xff0c;让你在断网时也能玩游戏&#xff01; 网址&#xff1a; 打开edge://surf这个断网游戏网站即可游玩&#xff1a; 作弊码既隐藏模式&#xff1a; 输入microsoft&#xff08;意思就…

C语言学习(二十八)---字符串相关函数

在上一节的内容结束后&#xff0c;有关指针的内容就告一段落了&#xff0c;指针是开发中非常重要的一环&#xff0c;大家务必要对其深入理解并且掌握&#xff0c;今天我们将继续往下学习&#xff0c;主要学习字符串操作相关的函数&#xff0c;分为不限制长度和限制长度两种&…

vetcor使用移动构造取代拷贝构造实现push_back

昨天说到&#xff1a;vector变量push_back一个对象或变量的时候&#xff0c;本质上是执行拷贝构造&#xff0c;但我想使用移动构造&#xff0c;而不是拷贝构造&#xff0c;本文就修改调试过程&#xff0c;详细分析如何实现移动构造。 昨天的代码如下&#xff1a;(如果有人想测…

Keil5 创建工程

一、 在桌面新建一个 TEST 的文件夹&#xff0c;然后在 TEST 文件夹里面新建 USER 文件夹&#xff0c;将工程名 字设为 test&#xff0c;保存在这个 USER 文件夹里面,选择对应芯片的安装包 启动代码作用&#xff1a; 1、堆栈&#xff08;SP&#xff09;的初始化&#xff1b; 2…

java.util.concurrent.Executionexception 异常

报错截图&#xff1a; 今天运行时发生了如下报错。自己捣鼓半天也没发现问题出在哪儿&#xff0c;感谢大佬的帮助&#xff0c;记录下来防止再犯。。 caused by org.apache.flink.client.program.programInvocationException: Job failed。程序调用异常。网上找了很多解决方法…

Qt 中线程池的使用

1. 线程池的原理 我们使用线程的时候就去创建一个线程&#xff0c;这样实现起来非常简便&#xff0c;但是就会有一个问题&#xff1a;如果并发的线程数量很多&#xff0c;并且每个线程都是执行一个时间很短的任务就结束了&#xff0c;这样频繁创建线程就会大大降低系统的效率&…

第14章-Python-人工智能-语言识别-调用百度语音识别

百度语音识别API是可以免费试用的&#xff0c;通过百度账号登录到百度智能云&#xff0c;在语音技术页面创建的应用&#xff0c;生成一个语音识别的应用&#xff0c;这个应用会给你一个APIKey和一个Secret Key&#xff0c;如图14.1所示。 我们在自己的程序中用 API Key 和 Secr…