头文件
总之缺什么就包含一下吧
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <unistd.h>
#include <pthread.h>
#include <setjmp.h>
#include <sys/syscall.h>
锁
//适用于临界区中有系统调用的情景
pthread_mutex_t mutex;
//适用于临界区中没有系统调用,比较简单的情景(如链表等操作数据结构)
pthread_spinlock_t spinlock;
CAS
int inc(int *value, int add) {
int old;
__asm__ volatile (
"lock; xaddl %2, %1;"
: "=a" (old)