Created
October 12, 2014 05:48
-
-
Save caigen/9c925a12c4651ba09fb3 to your computer and use it in GitHub Desktop.
lock/unlock and syscall(224)
This file contains 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 <pthread.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
// or syscall(224) | |
#define gettid() syscall(__NR_gettid) | |
void* thread(void* args) | |
{ | |
int tid = syscall(224); | |
int* ptr = (int*)((void**)args)[1]; | |
printf("%d %d\n", tid, *ptr); | |
pthread_mutex_t mutex = *((pthread_mutex_t*)args); | |
pthread_mutex_lock(&mutex); | |
++(*ptr); | |
pthread_mutex_unlock(&mutex); | |
printf("%d %d\n", tid, (*ptr)); | |
return NULL; | |
} | |
int main(int argc, char* argv[]) { | |
pthread_t thread1; | |
pthread_t thread2; | |
pthread_mutex_t mutex; | |
pthread_mutex_init(&mutex, NULL); | |
int a = 1; | |
void* args[2] = {(void*)&mutex, (void*)&a}; | |
printf("%d\n", *(int *)args[1]); | |
pthread_create(&thread1, NULL, thread, args); | |
pthread_create(&thread2, NULL, thread, args); | |
printf("\n"); | |
sleep(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment