• Linux 原子操作CAS与锁实现


    1、互斥锁(mutex)

    #include 
    #include 
    #include 
    
    #define __USE_GNU
    
    #include 
    #include 
    #include 
    #include 
    
    #define THREAD_COUNT 30
    
    pthread_mutex_t mutex;
    pthread_spinlock_t spinlock;
    
    void* callback(void* data){
        int* pdata = (int*)data;
        for(int i=0;i<100000;i++){
    #if 1
            pthread_mutex_lock(&mutex);
            (*pdata)++;
            pthread_mutex_unlock(&mutex);
    #else
        pthread_spin_lock(&spinlock);
        (*pdata)++;
        pthread_spin_unlock(&spinlock);
    #endif
        }
        return NULL;
    }
    
    int main()
    {
        pthread_mutex_init(&mutex,NULL);
        pthread_spin_init(&spinlock,PTHREAD_PROCESS_SHARED);
        pthread_t tid[THREAD_COUNT] = {0};
        int ptcount = 0;
        for(int i=0;i<THREAD_COUNT;i++){
            pthread_create(&tid[i],NULL,callback,(void*)&ptcount);
        }
        
        for(int i=0;i<100;i++){
            printf("ptcount:%d\n",ptcount);
            sleep(1);
        }
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    2、自旋锁(spinlock)

    #include 
    #include 
    #include 
    
    #define __USE_GNU
    
    #include 
    #include 
    #include 
    #include 
    
    #define THREAD_COUNT 30
    
    pthread_mutex_t mutex;
    pthread_spinlock_t spinlock;
    
    void* callback(void* data){
        int* pdata = (int*)data;
        for(int i=0;i<100000;i++){
    #if 0
            pthread_mutex_lock(&mutex);
            (*pdata)++;
            pthread_mutex_unlock(&mutex);
    #else
        pthread_spin_lock(&spinlock);
        (*pdata)++;
        pthread_spin_unlock(&spinlock);
    #endif
        }
        return NULL;
    }
    
    int main()
    {
        pthread_mutex_init(&mutex,NULL);
        pthread_spin_init(&spinlock,PTHREAD_PROCESS_SHARED);
        pthread_t tid[THREAD_COUNT] = {0};
        int ptcount = 0;
        for(int i=0;i<THREAD_COUNT;i++){
            pthread_create(&tid[i],NULL,callback,(void*)&ptcount);
        }
        
        for(int i=0;i<100;i++){
            printf("ptcount:%d\n",ptcount);
            sleep(1);
        }
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    3、原子操作

    int inc(int *value, int add) {
    
    	int old;
    	__asm__ volatile (
    		"lock; xaddl %2, %1;"
    		: "=a" (old)
    		: "m" (*value), "a" (add)
    		: "cc", "memory"
    	);
    	return old;
    
    }
    
    void *callback(void *data){
        int* pdata = (int*)data;
        for(int i=0;i<100000;i++){
            inc(pdata,1);
    #if 0
            pthread_mutex_lock(&mutex);
            (*pdata)++;
            pthread_mutex_unlock(&mutex);
    #elif 0
        pthread_spin_lock(&spinlock);
        (*pdata)++;
        pthread_spin_unlock(&spinlock);
    #endif
        }
        return NULL;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    4、线程私有空间(pthread_key)

    1、pthread_key_create() 用来创建线程私有数据。该函数从 TSD 池中分配一项,将其地址值赋给 key 供以后访问使用,第2个参数,是指定销毁函数,可以设置为NULL,设置为NULL的情况下,系统使用默认的销毁函数对数据进行销毁,不为空的时候,当线程退出时(pthread_exit()),将key对应的数据调出来,去释放。
    2、不论哪个线程调用了 pthread_key_create(),所创建的 key 都是所有线程可以访问的,但各个线程可以根据自己的需要往 key 中填入不同的值,相当于提供了一个同名而不同值的全局变量(这个全局变量相对于拥有这个变量的线程来说)。
    3、注销一个 TSD 使用 pthread_key_delete() 函数。该函数并不检查当前是否有线程正在使用该 TSD,也不会调用清理函数(destructor function),而只是将 TSD 释放以供下一次调用 pthread_key_create() 使用。在 LinuxThread 中,它还会将与之相关的线程数据项设置为 NULL。

    key被创建之后,因为是全局变量,所以所有的线程都可以访问。各个线程可以根据需求往key中,填入不同的值,这就相当于提供了一个同名而值不同的全局变量,即一键多值。一键多值依靠的一个结构体数组,即

    static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] ={{0,NULL}};
    
    // 定义
    struct pthread_key_struct
    {
      /* Sequence numbers.  Even numbers indicated vacant entries.  Note
         that zero is even.  We use uintptr_t to not require padding on
         32- and 64-bit machines.  On 64-bit machines it helps to avoid
         wrapping, too.  */
      uintptr_t seq;
    
      /* Destructor for the data.  */
      void (*destr) (void *);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    #include 
    #include 
    #include 
    
    #define __USE_GNU
    
    #include 
    #include 
    #include 
    #include 
    
    #define THREAD_COUNT 3
    pthread_key_t key;
    
    typedef void* (*thread_cb)(void*);
    
    void *func1(void *data){
        int i = 9;
        pthread_setspecific(key,&i);
    
        int *p = pthread_getspecific(key);
        printf("p=%d\n",*p);
        return NULL;
    }
    
    void *func2(void *data){
        char *str = "hello world";
        pthread_setspecific(key,str);
    
        char *ptr = pthread_getspecific(key);
        printf("ptr=%s\n",ptr);
        return NULL;
    }
    
    struct pair {
    	int x;
    	int y;
    };
    
    void *func3(void *data){
        struct pair p1 = {1, 2};
    	pthread_setspecific(key, &p1);
    
        struct pair *p = (struct pair *)pthread_getspecific(key);
        printf("pair x=%d,y=%d\n",p->x,p->y);
        return NULL;
    }
    
    
    
    int main()
    {
        thread_cb callback_cb[THREAD_COUNT] = {
            func1,
            func2,
            func3
        };
        pthread_key_create(&key,NULL);
    
        pthread_t tid[THREAD_COUNT] = {0};
        int count = 0;
        for(int i=0;i<THREAD_COUNT;i++){
            pthread_create(&tid[i],NULL,callback_cb[i],&count);
        }
    
        for(int i=0;i<THREAD_COUNT;i++){
            pthread_join(tid[i],NULL);
        }
        pthread_key_delete  ( key );
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    5、共享内存
    待续

    6、cpu的亲缘性(affinity)

    #include 
    #include 
    #include 
    
    #define __USE_GNU
    
    #include 
    #include 
    #include 
    #include 
    #define THREAD_COUNT 3
    
    void process_affinity(int num) {
    	//gettid();
    	pid_t selfid = syscall(__NR_gettid);
        printf("pid=%d\n",selfid);
    	cpu_set_t mask;
    	CPU_ZERO(&mask);
        // cpu编号从0开始
    	CPU_SET(num, &mask);
    
    	// 也可以填入selfid
    	sched_setaffinity(0, sizeof(mask), &mask);
    
    	while(1) ;
    }
    
    int main()
    {
        int num = sysconf(_SC_NPROCESSORS_CONF);
        int i = 0;
        pid_t pid = 0;
        for(int i=0;i<num/2;i++){
            pid = fork();
            if(pid<0)
                break;
        }
        if(pid == 0){
            printf("main pid=%d\n",pid);
            process_affinity(num);
        }
    
        while (1) usleep(1);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    7、setjmp/longjmp

    int setjmp(jmp_buf env);
    调用setjmp将堆栈上下文保存在jmp_buf结构体中(入栈),供longjmp稍后使用(出栈)。如果直接调用返回0,如果使用保存的上下文从longjmp返回,则返回保存值非零。
    void longjmp(jmp_buf env, int val);
    调用longjmp程序跳转到最后一次使用相应env参数调用setjmp处,调用longjmp后setjmp不能返回0,如果longjmp第二个参数设置0,则将返回1

    #include 
    #include 
     
    int main(int argc, char *argv[]) {
     
            int idx = 0;
            jmp_buf env;
            int count = 0;
     
     
            count = setjmp(env);
            if (count == 0) {
                    printf("count:%d\n", count);
                    longjmp(env, ++idx);
            } else if (count == 1) {
                    printf("count:%d\n", count);
                    longjmp(env, ++idx);
            } else if (count == 2) {
                    printf("count:%d\n", count);
                    longjmp(env, ++idx);
            } else {
                    printf("other count:%d\n",count);
            }
     
            return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    
    
    #include 
    #include 
    #include 
    
    #include 
    
    #include 
    #include 
    
    
    
    #define ntyThreadData		pthread_key_t
    #define ntyThreadDataSet(key, value)	pthread_setspecific((key), (value))
    #define ntyThreadDataGet(key)		pthread_getspecific((key))
    #define ntyThreadDataCreate(key)	pthread_key_create(&(key), NULL)
    
    
    #define EXCEPTIN_MESSAGE_LENGTH		512
    
    typedef struct _ntyException {
    	const char *name;
    } ntyException; 
    
    ntyException SQLException = {"SQLException"};
    ntyException TimeoutException = {"TimeoutException"};
    
    ntyThreadData ExceptionStack;
    
    
    typedef struct _ntyExceptionFrame {
    	jmp_buf env;
    
    	int line;
    	const char *func;
    	const char *file;
    
    	ntyException *exception;
    	struct _ntyExceptionFrame *prev;
    	
    	char message[EXCEPTIN_MESSAGE_LENGTH+1];
    
    } ntyExceptionFrame;
    
    #define ntyExceptionPopStack	\
    	ntyThreadDataSet(ExceptionStack, ((ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack))->prev)
    
    #define ReThrow					ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL)
    #define Throw(e, cause, ...) 	ntyExceptionThrow(&(e), __func__, __FILE__, __LINE__, cause, ##__VA_ARGS__, NULL)
    
    
    enum {
    	ExceptionEntered = 0,
    	ExceptionThrown,
    	ExceptionHandled,
    	ExceptionFinalized
    };
    
    
    #define Try do {							\
    			volatile int Exception_flag;	\
    			ntyExceptionFrame frame;		\
    			frame.message[0] = 0;			\
    			frame.prev = (ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack);	\
    			ntyThreadDataSet(ExceptionStack, &frame);	\
    			Exception_flag = setjmp(frame.env);			\
    			if (Exception_flag == ExceptionEntered) {	
    			
    
    #define Catch(e) \
    				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
    			} else if (frame.exception == &(e)) { \
    				Exception_flag = ExceptionHandled;
    
    
    #define Finally \
    				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
    			} { \
    				if (Exception_flag == ExceptionEntered)	\
    					Exception_flag = ExceptionFinalized; 
    
    #define EndTry \
    				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
    			} if (Exception_flag == ExceptionThrown) ReThrow; \
            	} while (0)	
    
    
    static pthread_once_t once_control = PTHREAD_ONCE_INIT;
    
    static void init_once(void) { 
    	ntyThreadDataCreate(ExceptionStack); 
    }
    
    
    void ntyExceptionInit(void) {
    	pthread_once(&once_control, init_once);
    }
    
    
    void ntyExceptionThrow(ntyException *excep, const char *func, const char *file, int line, const char *cause, ...) {
    
    	va_list ap;
    	ntyExceptionFrame *frame = (ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack);
    
    	if (frame) {
    
    		frame->exception = excep;
    		frame->func = func;
    		frame->file = file;
    		frame->line = line;
    
    		if (cause) {
    			va_start(ap, cause);
    			vsnprintf(frame->message, EXCEPTIN_MESSAGE_LENGTH, cause, ap);
    			va_end(ap);
    		}
    
    		ntyExceptionPopStack;
    
    		longjmp(frame->env, ExceptionThrown);
    		
    	} else if (cause) {
    
    		char message[EXCEPTIN_MESSAGE_LENGTH+1];
    
    		va_start(ap, cause);
    		vsnprintf(message, EXCEPTIN_MESSAGE_LENGTH, cause, ap);
    		va_end(ap);
    
    		printf("%s: %s\n raised in %s at %s:%d\n", excep->name, message, func ? func : "?", file ? file : "?", line);
    		
    	} else {
    
    		printf("%s: %p\n raised in %s at %s:%d\n", excep->name, excep, func ? func : "?", file ? file : "?", line);
    		
    	}
    
    }
    
    
    /* ** **** ******** **************** debug **************** ******** **** ** */
    
    ntyException A = {"AException"};
    ntyException B = {"BException"};
    ntyException C = {"CException"};
    ntyException D = {"DException"};
    
    void *thread(void *args) {
    
    	pthread_t selfid = pthread_self();
    
    	Try {
    
    		Throw(A, "A");
    		
    	} Catch (A) {
    
    		printf("catch A : %ld\n", selfid);
    		
    	} EndTry;
    
    	Try {
    
    		Throw(B, "B");
    		
    	} Catch (B) {
    
    		printf("catch B : %ld\n", selfid);
    		
    	} EndTry;
    
    	Try {
    
    		Throw(C, "C");
    		
    	} Catch (C) {
    
    		printf("catch C : %ld\n", selfid);
    		
    	} EndTry;
    
    	Try {
    
    		Throw(D, "D");
    		
    	} Catch (D) {
    
    		printf("catch D : %ld\n", selfid);
    		
    	} EndTry;
    
    	Try {
    
    		Throw(A, "A Again");
    		Throw(B, "B Again");
    		Throw(C, "C Again");
    		Throw(D, "D Again");
    
    	} Catch (A) {
    
    		printf("catch A again : %ld\n", selfid);
    	
    	} Catch (B) {
    
    		printf("catch B again : %ld\n", selfid);
    
    	} Catch (C) {
    
    		printf("catch C again : %ld\n", selfid);
    		
    	} Catch (D) {
    	
    		printf("catch B again : %ld\n", selfid);
    		
    	} EndTry;
    	
    }
    
    
    #define THREADS		50
    
    int main(void) {
    
    	ntyExceptionInit();
    
    	Throw(D, NULL);
    
    	Throw(C, "null C");
    
    	printf("\n\n=> Test1: Try-Catch\n");
    
    	Try {
    
    		Try {
    			Throw(B, "recall B");
    		} Catch (B) {
    			printf("recall B \n");
    		} EndTry;
    		
    		Throw(A, NULL);
    
    	} Catch(A) {
    
    		printf("\tResult: Ok\n");
    		
    	} EndTry;
    
    	printf("=> Test1: Ok\n\n");
    
    	printf("=> Test2: Test Thread-safeness\n");
    #if 1
    	int i = 0;
    	pthread_t threads[THREADS];
    	
    	for (i = 0;i < THREADS;i ++) {
    		pthread_create(&threads[i], NULL, thread, NULL);
    	}
    
    	for (i = 0;i < THREADS;i ++) {
    		pthread_join(threads[i], NULL);
    	}
    #endif
    	printf("=> Test2: Ok\n\n");
    
    } 
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
  • 相关阅读:
    PTA天梯赛训练 7-8 查验身份证 (15分)
    [附源码]Python计算机毕业设计Djangospringboot作业管理系统
    【论文阅读】SPARK:针对视觉跟踪的空间感知在线增量攻击
    tiny4412编译出来的zImage不能开机
    c51定时器
    【jackson解析复杂对象】
    【Vue】环境搭建
    解决No Feign Client for loadBalancing defined,修改Maven依赖
    【java筑基】IO流基础之文件的常见操作
    Mysql 事物与存储引擎
  • 原文地址:https://blog.csdn.net/weixin_45715405/article/details/133690623