Created
June 21, 2018 16:54
-
-
Save 38/06f69131506d786a3fb551b95df6062b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 这个counter是线程安全的, 使用了 CAS 指令 | |
#include <stdio.h> | |
#include <pthread.h> | |
volatile int counter = 0; | |
void counter_inc() | |
{ | |
int observed; | |
do { | |
observed = counter; | |
} while(!__sync_bool_compare_and_swap(&counter, observed, observed + 1)); | |
} | |
void* thread_main(void* p) | |
{ | |
int i; | |
for(i = 0; i < 100000000; i ++) | |
if(i % 2 == 0) counter_inc(); | |
return NULL; | |
} | |
#define N 4 | |
int main() | |
{ | |
pthread_t t[N]; | |
int i; | |
for(i = 0; i < N; i ++) | |
pthread_create(&t[i], NULL, thread_main, NULL); | |
for(i = 0; i < N; i ++) | |
pthread_join(t[i], NULL); | |
printf("%d\n", counter); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 这个counter在单核CPU上是线程安全的, 使用了 incl 指令 | |
#include <stdio.h> | |
#include <pthread.h> | |
volatile int counter = 0; | |
void counter_inc() | |
{ | |
asm volatile ("incl %1" : "=m" (counter) : "m" (counter)); | |
} | |
void* thread_main(void* p) | |
{ | |
int i; | |
for(i = 0; i < 100000000; i ++) | |
if(i % 2 == 0) counter_inc(); | |
return NULL; | |
} | |
#define N 4 | |
int main() | |
{ | |
pthread_t t[N]; | |
int i; | |
for(i = 0; i < N; i ++) | |
pthread_create(&t[i], NULL, thread_main, NULL); | |
for(i = 0; i < N; i ++) | |
pthread_join(t[i], NULL); | |
printf("%d\n", counter); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 这个counter是线程安全的, 使用了 lock incl 指令 | |
#include <stdio.h> | |
#include <pthread.h> | |
volatile int counter = 0; | |
void counter_inc() | |
{ | |
asm volatile ("lock incl %1" : "=m" (counter) : "m" (counter)); | |
} | |
void* thread_main(void* p) | |
{ | |
int i; | |
for(i = 0; i < 100000000; i ++) | |
if(i % 2 == 0) counter_inc(); | |
return NULL; | |
} | |
#define N 4 | |
int main() | |
{ | |
pthread_t t[N]; | |
int i; | |
for(i = 0; i < N; i ++) | |
pthread_create(&t[i], NULL, thread_main, NULL); | |
for(i = 0; i < N; i ++) | |
pthread_join(t[i], NULL); | |
printf("%d\n", counter); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 这个counter是线程安全的, 使用了pthread_mutex | |
#include <stdio.h> | |
#include <pthread.h> | |
volatile int counter = 0; | |
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; | |
void counter_inc() | |
{ | |
pthread_mutex_lock(&mut); | |
counter ++; | |
pthread_mutex_unlock(&mut); | |
} | |
void* thread_main(void* p) | |
{ | |
int i; | |
for(i = 0; i < 100000000; i ++) | |
if(i % 2 == 0) counter_inc(); | |
return NULL; | |
} | |
#define N 4 | |
int main() | |
{ | |
pthread_t t[N]; | |
int i; | |
for(i = 0; i < N; i ++) | |
pthread_create(&t[i], NULL, thread_main, NULL); | |
for(i = 0; i < N; i ++) | |
pthread_join(t[i], NULL); | |
printf("%d\n", counter); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment