-
-
Save hpcx82/3027568 to your computer and use it in GitHub Desktop.
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include<iostream> | |
using namespace std; | |
static pthread_mutex_t foo_mutex; | |
void* get_maokeng(void* param) | |
{ | |
fprintf(stderr,"i like to paifang shit! %d\n",*(int*)param); | |
pthread_mutex_lock(&foo_mutex); | |
fprintf(stderr,"i first get maokeng! haha! %d\n",*(int*)param); | |
} | |
void* go_maokeng(void* param) | |
{ | |
fprintf(stderr,"i like to paifang shit! %d\n",*(int*)param); | |
pthread_mutex_lock(&foo_mutex); | |
fprintf(stderr,"i get maokeng! %d\n",*(int*)param); | |
pthread_mutex_unlock(&foo_mutex); | |
}; | |
int main() | |
{ | |
pthread_mutex_init(&foo_mutex, NULL); | |
pthread_t* thread = (pthread_t*) malloc( 10 *sizeof( pthread_t)); | |
int token[10]={0,1,2,3,4,5,6,7,8,9}; | |
pthread_create(&thread[0],NULL,get_maokeng,&token[0]); | |
sleep(1); | |
for(int i=1;i<10;++i) | |
{ | |
pthread_create(&thread[i],NULL,go_maokeng,&token[i]); | |
sleep(1); | |
} | |
pthread_mutex_unlock(&foo_mutex); | |
for(int i=0;i<10;++i) | |
{ | |
pthread_join(thread[i],NULL); | |
} | |
free(thread); | |
return 0; | |
} |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include<iostream> | |
using namespace std; | |
static pthread_spinlock_t foo_spinlock; | |
void* get_maokeng(void* param) | |
{ | |
fprintf(stderr,"i like to paifang shit! %d\n",*(int*)param); | |
pthread_spin_lock(&foo_spinlock); | |
fprintf(stderr,"i first get maokeng! haha! %d\n",*(int*)param); | |
} | |
void* go_maokeng(void* param) | |
{ | |
fprintf(stderr,"i like to paifang shit! %d\n",*(int*)param); | |
pthread_spin_lock(&foo_spinlock); | |
fprintf(stderr,"i get maokeng! %d\n",*(int*)param); | |
pthread_spin_unlock(&foo_spinlock); | |
}; | |
int main() | |
{ | |
pthread_spin_init(&foo_spinlock, PTHREAD_PROCESS_PRIVATE); | |
pthread_t* thread = (pthread_t*) malloc( 10 *sizeof( pthread_t)); | |
int token[10]={0,1,2,3,4,5,6,7,8,9}; | |
pthread_create(&thread[0],NULL,get_maokeng,&token[0]); | |
sleep(1); | |
for(int i=1;i<10;++i) | |
{ | |
pthread_create(&thread[i],NULL,go_maokeng,&token[i]); | |
sleep(1); | |
} | |
pthread_spin_unlock(&foo_spinlock); | |
for(int i=0;i<10;++i) | |
{ | |
pthread_join(thread[i],NULL); | |
} | |
free(thread); | |
return 0; | |
} | |
Test Result - mutex:
i like to paifang shit! 0
i first get maokeng! haha! 0
i like to paifang shit! 1
i like to paifang shit! 2
i like to paifang shit! 3
i like to paifang shit! 4
i like to paifang shit! 5
i like to paifang shit! 6
i like to paifang shit! 7
i like to paifang shit! 8
i like to paifang shit! 9
i get maokeng! 1
i get maokeng! 2
i get maokeng! 3
i get maokeng! 4
i get maokeng! 5
i get maokeng! 6
i get maokeng! 7
i get maokeng! 8
i get maokeng! 9
Test Result - spinlock:
i like to paifang shit! 0
i first get maokeng! haha! 0
i like to paifang shit! 1
i like to paifang shit! 2
i like to paifang shit! 3
i like to paifang shit! 4
i like to paifang shit! 5
i like to paifang shit! 6
i like to paifang shit! 7
i like to paifang shit! 8
i like to paifang shit! 9
i get maokeng! 2
i get maokeng! 9
i get maokeng! 6
i get maokeng! 8
i get maokeng! 7
i get maokeng! 5
i get maokeng! 3
i get maokeng! 1
i get maokeng! 4
一篇关于pthread mutex和spinlock分析的文章:http://www.searchtb.com/2011/01/pthreads-mutex-vs-pthread-spinlock.html
版权所有: 梁斌-http://blog.sina.com.cn/s/blog_593af2a701015p9s.html