Created
May 1, 2015 06:37
-
-
Save depp/cca2e7c5c664d72ab9ee to your computer and use it in GitHub Desktop.
Data race
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
~ $ cc -fPIC -pie -fsanitize=thread -pthread test3.c -Wall -Wextra -g | |
~ $ ./a.out 5 | |
i am thread 1 .created a new thread(132118272) in the itaration 0 | |
i am thread 1 .created a new thread(123725568) in the itaration 1 | |
================== | |
WARNING: ThreadSanitizer: data race (pid=20232) | |
Read of size 4 at 0x7ffcdeefaadc by thread T1: | |
#0 thread /home/depp/test3.c:10 (a.out+0x000000000c14) | |
#1 <null> <null> (libtsan.so.0+0x000000023519) | |
Previous write of size 4 at 0x7ffcdeefaadc by main thread: | |
#0 main /home/depp/test3.c:23 (a.out+0x000000000d67) | |
Location is stack of main thread. | |
Thread T1 (tid=20234, running) created by main thread at: | |
#0 pthread_create <null> (libtsan.so.0+0x0000000273d4) | |
#1 main /home/depp/test3.c:29 (a.out+0x000000000d11) |
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 <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <time.h> | |
#include <unistd.h> | |
void *thread(void *a) { | |
pthread_t myid; | |
myid = pthread_self(); | |
int *i = (int *)a; | |
printf("Hello from thread %u-I was created in iteration %d\n", (unsigned)myid, *i); | |
pthread_exit(NULL); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
printf("Usege <%s number of thread>", argv[0]); | |
exit(1); | |
} | |
pthread_t tid; | |
int i; | |
int itr = atoi(argv[1]); | |
int flag = 1; | |
for (i = 0; i < itr; i++) { | |
if (flag == 5) { | |
printf("Sleeping for 2 sec\n"); | |
sleep(10); | |
flag = 0; | |
} | |
pthread_create(&tid, NULL, thread, (void *)&i); | |
printf("i am thread 1 .created a new thread(%u) in the itaration %d\n", (unsigned)tid, | |
i); | |
flag++; | |
} | |
pthread_join(tid, NULL); | |
pthread_exit(NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment