diff --git a/threads-bugs/Makefile b/threads-bugs/Makefile index 3e04898..de32462 100644 --- a/threads-bugs/Makefile +++ b/threads-bugs/Makefile @@ -8,7 +8,8 @@ ifeq ($(OS),Linux) endif SRCS := atomicity.c \ - atomicity_fixed.c + atomicity_fixed.c \ + ordering.c OBJS := ${SRCS:c=o} PROGS := ${SRCS:.c=} diff --git a/threads-bugs/ordering.c b/threads-bugs/ordering.c new file mode 100644 index 0000000..15a1f23 --- /dev/null +++ b/threads-bugs/ordering.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +#include "common.h" +#include "common_threads.h" + +#define PR_STATE_INIT (0) + +typedef struct { + pthread_t Tid; + int State; +} pr_thread_t; + +pr_thread_t *PR_CreateThread(void *(*start_routine)(void *)) { + pr_thread_t *p = malloc(sizeof(pr_thread_t)); + if (p == NULL) + return NULL; + p->State = PR_STATE_INIT; + Pthread_create(&p->Tid, NULL, start_routine, NULL); + // turn the sleep off to avoid the fault, sometimes... + sleep(1); + return p; +} + +void PR_WaitThread(pr_thread_t *p) { + Pthread_join(p->Tid, NULL); +} + +pr_thread_t *mThread; + +void *mMain(void *arg) { + printf("mMain: begin\n"); + int mState = mThread->State; + printf("mMain: state is %d\n", mState); + return NULL; +} + + +int main(int argc, char *argv[]) { + printf("ordering: begin\n"); + mThread = PR_CreateThread(mMain); + PR_WaitThread(mThread); + printf("ordering: end\n"); + return 0; +} +