skeeto / lstack

C11 Lock-free Stack
The Unlicense
180 stars 27 forks source link

Bus error #9

Open damon-kwok opened 6 years ago

damon-kwok commented 6 years ago

OS: Ubuntu 16.04 GCC:5.5.0 20171010

main.c: `

include "lstack.h"

include

include

lstack_t *ff; int aaa = 0; int bbb = 0;

void thread_fn(void arg) { for (int i = 0; i < 100; i++) { lstack_push(ff, "123456"); aaa++; } return ((void *)0); }

void thread_fn2(void arg) { for (int i = 0; i < 100; i++) { void data = lstack_pop(ff); if(data==NULL) continue; printf("%s\n", (char )data); bbb++; } return ((void *)0); }

int main(int argc, char *argv[]) { lstack_t s; lstack_init(&s, 200); ff = &s; pthread_t ntid, ntid2; pthread_create(&ntid, NULL, thread_fn, NULL); pthread_create(&ntid2, NULL, thread_fn2, NULL);

void ret, ret2; pthread_join(ntid, ret); pthread_join(ntid2, ret2);

printf("aaa=%d,bbb=%d\n", aaa, bbb); return 0; }

`

gcc main.c lstack.c -latomic -lpthread

./a.out

output: 123456 123456 123456 123456 123456 Bus error

skeeto commented 6 years ago

You're using pthread_join() incorrectly, passing garbage pointers for retval. You to either pass the addresses of ret and ret2, or just NULL since you're not using the result anyway.

I don't know if that's the actual cause of the bus error you're seeing, but it could be. With the pthread_join() issue fixed, I can't reproduce this problem. What architecture are you using? x86-64?

damon-kwok commented 6 years ago

x86-64 void ret=NULL, ret2=NULL; now, it's ok, thanks.