Skip to content

Instantly share code, notes, and snippets.

@mtharrison
Last active November 20, 2017 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtharrison/e26e1db197d758479717b82f7849bb0f to your computer and use it in GitHub Desktop.
Save mtharrison/e26e1db197d758479717b82f7849bb0f to your computer and use it in GitHub Desktop.
Simulate MT unsafe issues https://github.com/libuv/libuv/issues/271
#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;
}
@mtharrison
Copy link
Author

If I run this with master I'll get following logged to console:

Incorrect result Neizona after 1859 iterations

Running with the branch in my PR, it will complete (10k iterations) without error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment