linux c 线程间同步(通信)的几种方法--互斥锁,条件变量,信号量,读写锁

2023-05-16

Linux下提供了多种方式来处理线程同步,最常用的是互斥锁、条件变量、信号量和读写锁。
下面是思维导图:
这里写图片描述

一、互斥锁(mutex)
  锁机制是同一时刻只允许一个线程执行一个关键部分的代码。

1 . 初始化锁

int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex_attr_t *mutexattr);

其中参数 mutexattr 用于指定锁的属性(见下),如果为NULL则使用缺省属性。
互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性,不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。当前有四个值可供选择:
(1)PTHREAD_MUTEX_TIMED_NP,这是缺省值,也就是普通锁。当一个线程加锁以后,其余请求锁的线程将形成一个等待队列,并在解锁后按优先级获得锁。这种锁策略保证了资源分配的公平性。
(2)PTHREAD_MUTEX_RECURSIVE_NP,嵌套锁,允许同一个线程对同一个锁成功获得多次,并通过多次unlock解锁。如果是不同线程请求,则在加锁线程解锁时重新竞争。
(3)PTHREAD_MUTEX_ERRORCHECK_NP,检错锁,如果同一个线程请求同一个锁,则返回EDEADLK,否则与PTHREAD_MUTEX_TIMED_NP类型动作相同。这样就保证当不允许多次加锁时不会出现最简单情况下的死锁。
(4)PTHREAD_MUTEX_ADAPTIVE_NP,适应锁,动作最简单的锁类型,仅等待解锁后重新竞争。

2 . 阻塞加锁

 int pthread_mutex_lock(pthread_mutex *mutex);

3 . 非阻塞加锁

  int pthread_mutex_trylock( pthread_mutex_t *mutex);

该函数语义与 pthread_mutex_lock() 类似,不同的是在锁已经被占据时返回 EBUSY 而不是挂起等待。
4 . 解锁(要求锁是lock状态,并且由加锁线程解锁)

 int pthread_mutex_unlock(pthread_mutex *mutex);

5 . 销毁锁(此时锁必需unlock状态,否则返回EBUSY)

int pthread_mutex_destroy(pthread_mutex *mutex);

  示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int gn;

void* thread(void *arg)
{
    printf("thread's ID is  %d\n",pthread_self());
    pthread_mutex_lock(&mutex);
    gn = 12;
    printf("Now gn = %d\n",gn);
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main()
{
    pthread_t id;
    printf("main thread's ID is %d\n",pthread_self());
    gn = 3;
    printf("In main func, gn = %d\n",gn);
    if (!pthread_create(&id, NULL, thread, NULL)) {
        printf("Create thread success!\n");
    } else {
        printf("Create thread failed!\n");
    }
    pthread_join(id, NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}

二、条件变量(cond)

  条件变量是利用线程间共享全局变量进行同步的一种机制。条件变量上的基本操作有:触发条件(当条件变为 true 时);等待条件,挂起线程直到其他线程触发条件。

1 . 初始化条件变量 

   int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);
 尽管POSIX标准中为条件变量定义了属性,但在Linux中没有实现,因此cond_attr值通常为NULL,且被忽略。

2 . 有两个等待函数
(1)无条件等待

      int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
  (2)计时等待
      int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
  如果在给定时刻前条件没有满足,则返回ETIMEOUT,结束等待,其中abstime以与time()系统调用相同意义的绝对时间形式出现,0表示格林尼治时间1970年1月1日0时0分0秒。

 
无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求(用 pthread_cond_wait() 或 pthread_cond_timedwait() 请求)竞争条件(Race Condition)。mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。

3 . 激发条件
(1)激活一个等待该条件的线程(存在多个等待线程时按入队顺序激活其中一个)  

      int pthread_cond_signal(pthread_cond_t *cond);

(2)激活所有等待线程

  int pthread_cond_broadcast(pthread_cond_t *cond);

4 . 销毁条件变量

   int pthread_cond_destroy(pthread_cond_t *cond);
  只有在没有线程在该条件变量上等待的时候才能销毁这个条件变量,否则返回EBUSY

说明:

  1. pthread_cond_wait 自动解锁互斥量(如同执行了pthread_unlock_mutex),并等待条件变量触发。这时线程挂起,不占用CPU时间,直到条件变量被触发(变量为ture)。在调用 pthread_cond_wait之前,应用程序必须加锁互斥量。pthread_cond_wait函数返回前,自动重新对互斥量加锁(如同执行了pthread_lock_mutex)。

  2. 互斥量的解锁和在条件变量上挂起都是自动进行的。因此,在条件变量被触发前,如果所有的线程都要对互斥量加锁,这种机制可保证在线程加锁互斥量和进入等待条件变量期间,条件变量不被触发。条件变量要和互斥量相联结,以避免出现条件竞争——个线程预备等待一个条件变量,当它在真正进入等待之前,另一个线程恰好触发了该条件(条件满足信号有可能在测试条件和调用pthread_cond_wait函数(block)之间被发出,从而造成无限制的等待)。

  3. 条件变量函数不是异步信号安全的,不应当在信号处理程序中进行调用。特别要注意,如果在信号处理程序中调用 pthread_cond_signal 或 pthread_cond_boardcast 函数,可能导致调用线程死锁

示例代码1:

#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"

pthread_mutex_t mutex;
pthread_cond_t cond;

void hander(void *arg)
{
    free(arg);
    (void)pthread_mutex_unlock(&mutex);
}

void *thread1(void *arg)
{
    pthread_cleanup_push(hander, &mutex);
    while (1) {
        printf("thread1 is running\n");
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread1 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        sleep(4);
    }
    pthread_cleanup_pop(0);
}

void *thread2(void *arg)
{
    while (1) {
        printf("thread2 is running\n");
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread2 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

int main()
{
    pthread_t thid1,thid2;
    printf("condition variable study!\n");
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_create(&thid1,NULL,thread1,NULL);
    pthread_create(&thid2,NULL,thread2,NULL);

    sleep(1);
    do {
        pthread_cond_signal(&cond);
    } while(1);

    sleep(20);
    pthread_exit(0);
    return 0;
}

示例代码2:

#include <pthread.h>
#include <unistd.h>
#include "stdio.h"
#include "stdlib.h"

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

struct node
{
    int n_number;
    struct node *n_next;
}*head = NULL;

static void cleanup_handler(void *arg)
{
    printf("Cleanup handler of second thread.\n");
    free(arg);
    (void)pthread_mutex_unlock(&mtx);
}

static void *thread_func(void *arg)
{
    struct node *p = NULL;
    pthread_cleanup_push(cleanup_handler, p);

    while (1) {
        // 这个mutex主要是用来保证pthread_cond_wait的并发性。
        pthread_mutex_lock(&mtx);
        while (head == NULL) {
            /* 这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何
            * 这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线
            * 程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。
            * 这个时候,应该让线程继续进入pthread_cond_wait
            * pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,
            * 然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立
            * 而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源
            * 用这个流程是比较清楚的。*/

            pthread_cond_wait(&cond, &mtx);
            p = head;
            head = head->n_next;
            printf("Got %d from front of queue\n", p->n_number);
            free(p);
        }
        pthread_mutex_unlock(&mtx); // 临界区数据操作完毕,释放互斥锁。
    }
    pthread_cleanup_pop(0);
    return 0;
}

int main(void)
{
    pthread_t tid;
    int i;
    struct node *p;

    /* 子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,
    * 而不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大。*/
    pthread_create(&tid, NULL, thread_func, NULL);
    sleep(1);
    for (i = 0; i < 10; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p->n_number = i;
        pthread_mutex_lock(&mtx); // 需要操作head这个临界资源,先加锁。
        p->n_next = head;
        head = p;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mtx); //解锁

        sleep(1);
    }

    printf("thread 1 wanna end the line.So cancel thread 2.\n");

    /* 关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,
    * 退出线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。*/

    pthread_cancel(tid);
    pthread_join(tid, NULL);

    printf("All done -- exiting\n");
    return 0;
}

可以看出,等待条件变量信号的用法约定一般是这样的:

...
pthread_mutex_lock(&mutex);
...
pthread_cond_wait (&cond, &mutex);
...
pthread_mutex_unlock (&mutex);
...

相信很多人都会有这个疑问:为什么pthread_cond_wait需要的互斥锁不在函数内部定义,而要使用户定义的呢?现在没有时间研究 pthread_cond_wait 的源代码,带着这个问题对条件变量的用法做如下猜测,希望明白真相看过源代码的朋友不吝指正。

  1. pthread_cond_wait 和 pthread_cond_timewait 函数为什么需要互斥锁?因为:条件变量是线程同步的一种方法,这两个函数又是等待信号的函数,函数内部一定有须要同步保护的数据。
  2. 使用用户定义的互斥锁而不在函数内部定义的原因是:无法确定会有多少用户使用条件变量,所以每个互斥锁都须要动态定义,而且管理大量互斥锁的开销太大,使用用户定义的即灵活又方便,符合UNIX哲学的编程风格(随便推荐阅读《UNIX编程哲学》这本好书!)。
  3. 好了,说完了1和2,我们来自由猜测一下 pthread_cond_wait 函数的内部结构吧:
  int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
   {
      if(没有条件信号)
      {
         (1)pthread_mutex_unlock (mutex); // 因为用户在函数外面已经加锁了(这是使用约定),但是在没有信号的情况下为了让其他线程也能等待cond,必须解锁。
         (2) 阻塞当前线程,等待条件信号(当然应该是类似于中断触发的方式等待,而不是软件轮询的方式等待)... 有信号就继续执行后面。
         (3) pthread_mutex_lock (mutex); // 因为用户在函数外面要解锁(这也是使用约定),所以要与1呼应加锁,保证用户感觉依然是自己加锁、自己解锁。
      }      
      ...
  }

三、 信号量

 如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。
线程使用的基本信号量函数有四个:

  #include <semaphore.h>

1 . 初始化信号量

   int sem_init (sem_t *sem , int pshared, unsigned int value);

参数:
sem - 指定要初始化的信号量;
pshared - 信号量 sem 的共享选项,linux只支持0,表示它是当前进程的局部信号量;
value - 信号量 sem 的初始值。

2 . 信号量值加1
给参数sem指定的信号量值加1。

     int sem_post(sem_t *sem);

3 . 信号量值减1
给参数sem指定的信号量值减1。

     int sem_wait(sem_t *sem);

如果sem所指的信号量的数值为0,函数将会等待直到有其它线程使它不再是0为止。

4 . 销毁信号量
销毁指定的信号量。

  int sem_destroy(sem_t *sem);

 示例代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>

#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!\n", __func__);return;}

typedef struct _PrivInfo
{
    sem_t s1;
    sem_t s2;
    time_t end_time;
}PrivInfo;

static void info_init (PrivInfo* prifo);
static void info_destroy (PrivInfo* prifo);
static void* pthread_func_1 (PrivInfo* prifo);
static void* pthread_func_2 (PrivInfo* prifo);

int main (int argc, char** argv)
{
    pthread_t pt_1 = 0;
    pthread_t pt_2 = 0;
    int ret = 0;
    PrivInfo* prifo = NULL;
    prifo = (PrivInfo* )malloc (sizeof (PrivInfo));

    if (prifo == NULL) {
        printf ("[%s]: Failed to malloc priv.\n");
        return -1;
    }

    info_init (prifo);
    ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, prifo);
    if (ret != 0) {
        perror ("pthread_1_create:");
    }

    ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, prifo);
    if (ret != 0) {
        perror ("pthread_2_create:");
    }

    pthread_join (pt_1, NULL);
    pthread_join (pt_2, NULL);
    info_destroy (prifo);
    return 0;
}

static void info_init (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    prifo->end_time = time(NULL) + 10;
    sem_init (&prifo->s1, 0, 1);
    sem_init (&prifo->s2, 0, 0);
    return;
}

static void info_destroy (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    sem_destroy (&prifo->s1);
    sem_destroy (&prifo->s2);
    free (prifo);
    prifo = NULL;
    return;
}

static void* pthread_func_1 (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    while (time(NULL) < prifo->end_time)
    {
        sem_wait (&prifo->s2);
        printf ("pthread1: pthread1 get the lock.\n");
        sem_post (&prifo->s1);
        printf ("pthread1: pthread1 unlock\n");
        sleep (1);
    }
    return;
}

static void* pthread_func_2 (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    while (time (NULL) < prifo->end_time)
    {
        sem_wait (&prifo->s1);
        printf ("pthread2: pthread2 get the unlock.\n");
        sem_post (&prifo->s2);
        printf ("pthread2: pthread2 unlock.\n");
        sleep (1);
    }
    return;

}

四 读写锁
4.1 注意事项

  • 1.如果一个线程用读锁锁定了临界区,那么其他线程也可以用读锁来进入临界区,这样就可以多个线程并行操作。但这个时候,如果再进行写锁加锁就会发生阻塞,写锁请求阻塞后,后面如果继续有读锁来请求,这些后来的读锁都会被阻塞!这样避免了读锁长期占用资源,防止写锁饥饿!
  • 2.如果一个线程用写锁锁住了临界区,那么其他线程不管是读锁还是写锁都会发生阻塞!

4.2 常用接口
1. 初始化:

int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
  1. 读写加锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);
int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);

3.销毁锁

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

应用实例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

/* 初始化读写锁 */
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
/* 全局资源 */
int global_num = 10;

void err_exit(const char *err_msg)
{
 printf("error:%s\n", err_msg);
 exit(1);
}

/* 读锁线程函数 */
void *thread_read_lock(void *arg)
{
 char *pthr_name = (char *)arg;

 while (1)
 {
     /* 读加锁 */
     pthread_rwlock_rdlock(&rwlock);

     printf("线程%s进入临界区,global_num = %d\n", pthr_name, global_num);
     sleep(1);
     printf("线程%s离开临界区...\n", pthr_name);

     /* 读解锁 */
     pthread_rwlock_unlock(&rwlock);

     sleep(1);
 }

 return NULL;
}

/* 写锁线程函数 */
void *thread_write_lock(void *arg)
{
 char *pthr_name = (char *)arg;

 while (1)
 {
     /* 写加锁 */
     pthread_rwlock_wrlock(&rwlock);

     /* 写操作 */
     global_num++;
     printf("线程%s进入临界区,global_num = %d\n", pthr_name, global_num);
     sleep(1);
     printf("线程%s离开临界区...\n", pthr_name);

     /* 写解锁 */
     pthread_rwlock_unlock(&rwlock);

     sleep(2);
 }

 return NULL;
}

int main(void)
{
 pthread_t tid_read_1, tid_read_2, tid_write_1, tid_write_2;

 /* 创建4个线程,2个读,2个写 */
 if (pthread_create(&tid_read_1, NULL, thread_read_lock, "read_1") != 0)
     err_exit("create tid_read_1");

 if (pthread_create(&tid_read_2, NULL, thread_read_lock, "read_2") != 0)
     err_exit("create tid_read_2");

 if (pthread_create(&tid_write_1, NULL, thread_write_lock, "write_1") != 0)
     err_exit("create tid_write_1");

 if (pthread_create(&tid_write_2, NULL, thread_write_lock, "write_2") != 0)
     err_exit("create tid_write_2");

 /* 随便等待一个线程,防止main结束 */
 if (pthread_join(tid_read_1, NULL) != 0)
     err_exit("pthread_join()");

 return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

linux c 线程间同步(通信)的几种方法--互斥锁,条件变量,信号量,读写锁 的相关文章

  • 【jetson nano、NX、TX2 开机自启动程序,开机自动解除硬件限制,开机默认最大性能工作。】

    jetson nano NX TX2 开机自启动程序 xff0c 开机自动解除硬件限制 xff0c 开机默认最大性能工作 jetson nano NX TX2是英伟达开发的边缘平台 xff0c 其良好的性能 亲民的价格非常适合部署深度学习模
  • 蒙特卡洛法(一)

    蒙特卡洛法也成为统计模拟方法 xff0c 通过从概率模型的随机抽样进行近似数值计算的方法 马尔科夫链蒙特卡洛法则是以马尔科夫链为概率模型的蒙特卡洛法 xff0c 构建一个马尔科夫链 xff0c 使其平稳分布就是要进行抽样的分布 xff0c
  • 芯片测试术语 ,片内测试(BIST),ATE测试

    芯片测试分为如下几类 xff1a 1 WAT xff1a Wafer AcceptanceTest xff0c wafer level 的管芯或结构测试 xff1b 2 CP xff1a chip probing xff0c wafer l
  • SBUS协议及编解码

    1 简介 SBUS本质是一种串口通信协议 xff0c 采用100K的波特率 xff0c 8位数据位 xff0c 两位停止位 xff0c 偶效验 xff0c 即8E2的串口通信 值得注意的有三点 xff1a 1 SBUS采用负逻辑 xff0c
  • Linux内核跨模块函数调用:EXPORT_SYMBOL()宏定义

    一 查看内核驱动代码你会发现很多的函数带有EXPORT SYMBOL 宏定义 二 那么EXPORT SYMBOL的作用是什么 xff1f EXPORT SYMBOL标签内定义的函数或者符号对全部内核代码公开 xff0c 不用修改内核代码就可
  • linux内核I2C子系统详解

    1 I2C通信协议 参考博客 xff1a I2C通信协议详解和通信流程分析 xff1b https csdnimg cn release blogv2 dist pc themesSkin skin3 template images bg
  • 内核驱动中断申请类型及函数分析

    ret 61 request irq chip gt irq xxx intr handler IRQF TRIGGER FALLING IRQF NO THREAD IRQF NO SUSPEND name chip 上面是中断初始化中调
  • I2C设备注册的4种方法

    文章目录 前言一 静态注册二 动态注册三 用户空间注册四 i2c驱动扫描注册 前言 I2C设备的4种添加方法 xff1a 1 xff09 静态注册 2 xff09 动态注册 3 xff09 用户空间注册 4 xff09 i2c驱动扫描注册
  • pm_wakeup.h

    pm wakeup h Power management wakeup interface Copyright C 2008 Alan Stern Copyright C 2010 Rafael J Wysocki Novell Inc T
  • GTK+ Reference Manual

    GTK 43 Reference Manual for GTK 43 2 6 2 Table of Contents I GTK 43 Overview Compiling the GTK 43 libraries How to compi
  • Linux获取进程列表

    实现思路是 xff1a 遍历 proc目录下的所有进程描述文件夹 xff0c 从而获取进程列表 代码如下 xff1a include lt stdio h gt include lt dirent h gt include lt unist
  • ubuntu18.04 下firefox 不能 播放视频,因为默认未安装FLASH插件。(当然只是原因之一)

    ubuntu18 04 下firefox 不能 播放视频 xff0c 默认未安装FLASH插件 终端输入 xff1a sudo apt get install flashplugin nonfree
  • Ubuntu上可使用的15个桌面环境

    Ubuntu上可使用的15个桌面环境 发布者 红黑魂 来自 Ubuntu之家 摘要 Linux下桌面环境很多 xff0c Ubuntu之家给大家总结了比较常用的15个桌面环境 xff0c 并附上Ubuntu 12 10 xff08 Linu
  • C语言数据类型

    数据类型在数据结构中的定义是一个值的集合以及定义在这个值集上的一组操作 数据类型包括原始类型 多元组 记录单元 代数数据类型 抽象数据类型 参考类型以及函数类型 本文主要以51单片机中的数据类型为中心而展开的话题 在keil C51或者ia
  • 《Cortex-M0权威指南》之Cortex-M0技术综述

    Cortex M0权威指南 之Cortex M0技术综述 转载请注明来源 xff1a cuixiaolei的技术博客 Cortex M0 处理器简介 1 Cortex M0 处理器基于冯诺依曼架构 xff08 单总线接口 xff09 xff
  • xos详解5:PendSV_Handler

    PendSV Handler PendSV Handler LDR R2 61 OSTcbCurr 不必关中断 嵌套中断发生时会自动保存 R0 R3 到 MSP 并恢复 LDR R0 R2 如果发生咬尾的多个 PendSV xff0c 上半
  • M0最高优先级的中断设计

    1 Reset 3 Highest Reset 绝大部分处理器设计时 xff0c 将复位中断放在最高优先级 一般来说这样设计是合理的 xff0c 个人认为在某些应用场景这样处理仍有局限性 2 NMI 2 Nonmaskable interr
  • 如何从零开始写一个操作系统?

    首页发现等你来答 登录加入知乎 如何从零开始写一个简单的操作系统 xff1f 关注问题 写回答 操作系统 编程学习 如何从零开始写一个简单的操作系统 xff1f 看了这个 xff1a 从零开始写一个简单的操作系统 求指教 关注者 4 787
  • 每次听到同事跳槽后的薪资,我就像打了鸡血一样

    本文总结了现阶段 34 大龄程序员 34 的职业生存状况 xff0c 内容包含职位需求量 xff0c 议价能力如何以及如何度过传说中的 34 中年危机 34 等等 xff0c 供大家参考 xff01 值此金 三 银四跳槽季 的开端 xff0
  • Lua性能优化—Lua内存优化

    原文链接https blog uwa4d com archives usparkle luaperformance html 这是侑虎科技第236篇原创文章 xff0c 感谢作者舒航供稿 xff0c 欢迎转发分享 xff0c 未经作者授权请

随机推荐

  • Jetson Xavier NX(emmc)烧录系统时可能遇到的问题(避坑)

    目录 Ununtu18虚拟机无法联网 当NX接上电源后 xff0c 指示灯没有亮 xff08 不工作 xff09 在登陆SDK时 xff0c 可能会出现卡在初始界面的情况 在烧录镜像时 xff0c 可能会卡在该处没有变化 Ununtu18虚
  • 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)

    1 首先pthread cond wait 的定义是这样的 The pthread cond wait and pthread cond timedwait functions are used to block on a conditio
  • STAR法则写简历

    STAR 法则是在面试 xff0c 求职 xff0c 写简历时候的常用利器 虽然常用 xff0c 但是我想知道的人一定很少很少 xff0c 不然为什么那么多人面试的时候犯那么低级的错误呢 xff1f STAR法则无法帮你提高你的实力 xff
  • 论文发表为什么不可以一稿多投呢

    论文发表为什么不可以一稿多投呢 很多作者在担心投一家杂志会被拒稿 xff0c 就会选择一篇稿件投多家期刊的方法 xff0c 大家应该或多或少都听过不能一稿多投 xff0c 但具体原因是什么大家知道吗 一稿多投会有什么后果 一稿多投是自稿件发
  • ROS下视频消息发布与订阅

    https download csdn net download v7xyy 10869743 下下来后 1 发布视频消息 rosrun video transport tutorial video publisher xff08 节点 c
  • ros中标志位设计(4)

    由于需要涉及控制权的交接事件 xff0c 需要通过标志位的方式进行设计 首先需要自定一个标志位的信息在ros中用于标志位信息的发布 下面是用于标志位的头文件Flag h Generated by gencpp from file xx ms
  • 全球最贵域名Sex.com将再度出售

    金融时报消息称 xff0c Sex com域名曾位居全球最贵域名前例 xff0c 四年前 xff0c 它以1400万美元成交 xff0c 不过 xff0c 买下此域名的公司面临破产 xff0c 因此Sex com将再度拿来出售 Sex co
  • 微信消息推送消息加解密(golang)

    本篇介绍如何使用golang对微信消息推送进行加解密 xff0c 后续会补充 xff0c 目前先写个原理 xff0c 大概自己看一下 xff0c 其他的自己应该也能写 老套路 xff0c 分为三步 xff0c 为啥写 xff0c 教程 xf
  • C++数据可视化MathGL使用简示

    C 43 43 数据可视化 MathGL 使用指南 效果演示 搭建环境与依赖项 Windows10 64位 VS2017 Zlib1 2 11 xff08 已编译好的可用版本已集成在我后面的项目链接里 xff09 libpng1 6 37
  • 飞思卡尔智能车——舵机及PID控制

    本篇博客已迁移至 xff1a 飞思卡尔智能车 舵机及PID控制 请帮个忙 xff0c 去新地址访问 xff1a xff09 舵机 xff1a 小车转向的控制 机构 也就是控制小车的转向 它的特点是结构紧凑 易安装调试 控制简单 大扭力 成本
  • Github Actions + Docker实现HTML静态前端页面CICD部署

    使用 Github Actions 可以实现 CICD 自动构建部署 简单来说就是你只需要执行 git push 命令 xff0c 你服务器上的网页就可以自动部署更新 xff0c 无需你执行编译指令 前置环境 服务器一台 xff0c 我的是
  • Ubuntu显示“submodule(s) are missing“或“子模块未对路径注册“解决方案

    最近测试openMVG的三维重建效果 xff0c 于是在github下克隆openMVG的库 xff0c git clone过程成功进行 xff0c 但是在build文件夹下cmake的时候error occured 错误显示 34 sub
  • 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(二)

    1 Linux 线程 进程与线程之间是有区别的 xff0c 不过Linux内核只提供了轻量进程的支持 xff0c 未实现线程模型 Linux是一种 多进程单线程 的操作系统 Linux本身只有进程的概念 xff0c 而其所谓的 线程 本质上
  • 网易视频云:流媒体服务器原理和架构解析

    网易视频云 是网易公司旗下的视频云服务产品 xff0c 以Paas服务模式 xff0c 向开发者提供音视频编解码SDK和开放API xff0c 助力APP接入音视频功能 今天 xff0c 网易视频云的技术专家给大家分享一篇流媒体技术性文章
  • MATLAB语言中int函数

    在MATLAB语言中 xff0c 求符号函数的定积分是使用int函数 xff0c 其调用格式如下 xff1a int F x a b a表示定积分的下限 xff1b b表示定积分的上限 xff1b 上式表示 xff0c 被积函数F在区间 a
  • matlab中的subs函数用法

    matlab中subs 是符号计算函数 xff0c 表示将符号表达式中的某些符号变量替换为指定的新的变量 xff0c 常用调用方式为 xff1a subs S OLD NEW 表示将符号表达式S中的符号变量OLD替换为新的值NEW 下面具体
  • Android配置临时ipv6地址

    Google公网DNS 2001 4860 4860 64642001 4860 4860 64 ifconfig wlan0 inet6 add IPV6ADDR ifconfig wlan0 inet6 add 2001 4860 48
  • MFC:pic控件的矩形的left、right、top、bottom 坐标位置

    CRect rect 然后 获取矩形控件 那么这个矩形控件的左上 和右下 分别对应 xff0c left xff0c top xff1b right xff0c bottom left xff0c top为左上角的点坐标 right xff
  • ubuntu下对sd卡 分区和格式化 挂载sd卡

    一 sd卡分区和格式化 1 查看自己的设备号 命令 xff1a mount 可以看到 最后一行即为sd卡的挂载目录 2 umount 由于sd卡插上之后会自动mount xff0c 所以需要unmout 命令 xff1a umount 路径
  • linux c 线程间同步(通信)的几种方法--互斥锁,条件变量,信号量,读写锁

    Linux下提供了多种方式来处理线程同步 xff0c 最常用的是互斥锁 条件变量 信号量和读写锁 下面是思维导图 xff1a 一 互斥锁 xff08 mutex xff09 锁机制是同一时刻只允许一个线程执行一个关键部分的代码 1 初始化锁