Skip to content

Bus error #9

@damon-kwok

Description

@damon-kwok

OS: Ubuntu 16.04
GCC:5.5.0 20171010

main.c:
`
#include "lstack.h"
#include <pthread.h>
#include <stdio.h>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions