Last active
November 20, 2017 03:50
-
-
Save mtharrison/e26e1db197d758479717b82f7849bb0f to your computer and use it in GitHub Desktop.
Simulate MT unsafe issues https://github.com/libuv/libuv/issues/271
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 <assert.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <uv.h> | |
static int iterations = 0; | |
void *thread1() { | |
int status = uv_set_process_title("Arizona"); | |
assert(status == 0); | |
return NULL; | |
} | |
void *thread2() { | |
char *buffer = (char *) malloc(sizeof(char) * 100); | |
int status = uv_get_process_title(buffer, 100); | |
assert(status == 0); | |
if ((strcmp(buffer, "Arizona") != 0) && | |
(strcmp(buffer, "New York") != 0)) { | |
printf("Incorrect result %s after %d iterations\n", buffer, iterations); | |
exit(1); | |
} | |
return NULL; | |
} | |
int main(int argc, char** argv) { | |
setbuf(stdout, NULL); | |
uv_setup_args(argc, argv); | |
int status; | |
status = uv_set_process_title("New York"); | |
assert(status == 0); | |
pthread_t t1; | |
pthread_t t2; | |
while(++iterations) { | |
printf("\r%d iterations", iterations); | |
pthread_create(&t2, NULL, thread2, NULL); | |
pthread_create(&t1, NULL, thread1, NULL); | |
pthread_join(t1, NULL); | |
pthread_join(t2, NULL); | |
status = uv_set_process_title("New York"); | |
assert(status == 0); | |
if (iterations == 1e4) { | |
printf("No failures after %f iterations", 1e4); | |
exit(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I run this with master I'll get following logged to console:
Running with the branch in my PR, it will complete (10k iterations) without error.