Skip to content

Instantly share code, notes, and snippets.

@msizanoen1
Last active October 6, 2019 08:13
Show Gist options
  • Save msizanoen1/f47d174334c07dcebe615c6d09c291ed to your computer and use it in GitHub Desktop.
Save msizanoen1/f47d174334c07dcebe615c6d09c291ed to your computer and use it in GitHub Desktop.
Android pthread TLS repro
#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
pthread_key_t key;
extern int getX();
int main() {
key = pthread_key_create(&key, NULL);
int *x = (int *)malloc(sizeof(int));
pthread_setspecific(key, x);
*x = 1234;
printf("%d\n", getX());
}
#include <pthread.h>
#include <stdio.h>
extern pthread_key_t key;
int getX() {
int *x = (int *)pthread_getspecific(key);
if (x == NULL) {
printf("ERROR\n");
return 0;
} else {
printf("OK");
return *x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment