Created
July 1, 2012 08:35
-
-
Save hpcx82/3027568 to your computer and use it in GitHub Desktop.
spin lock with 9 threads waiting - who is next is random/mutex with 9 threads waiting, the first one in the queue will be the next
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
#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; | |
} |
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
#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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一篇关于pthread mutex和spinlock分析的文章:http://www.searchtb.com/2011/01/pthreads-mutex-vs-pthread-spinlock.html