Jeff, I changed the test program as you suggested but unfortunately it does not help... I also changed the program so that we no longer need mb's. The fact that the problem only occurs when I start the program twice on a system should convince you that it's not a broken program... I attached the changed version of the program. Please try it when you find some time. More ideas? Reto Jeff Sturm wrote: > > Reto Baettig wrote: > > The strange thing is that a single instance of the program runs just > > fine. When I start the program a second time, I get segfaults and/or > > stuck threads. > > Actually I would expect stuck threads: > > mem.delete = 1; > > while (mem.delete) > Th_Delay(1); > > mem.delete must be declared `volatile' to prevent that from looping forever. > However as long you didn't compile with optimizations you can probably get by. > > Also, consider that the "Free" thread is writing: > > mem->buf = NULL; > mem->delete = 0; > // ...loop... > > while the "DoTest" thread is reading: > > // ...loop.... > mb(); > assert(mem.buf == NULL); > assert(mem.delete == 0); > > The writes to mem->buf and mem->delete may be reordered by the compiler and/or > CPU. Despite the memory barrier, DoTest can see a non-NULL mem.buf even after > it sees (mem.delete == 0). > > To get guaranteed sequential behavior, you need to use `volatile' and a few more > membar's, e.g.: > > mem->buf = NULL; > mb(); > mem->delete = 0; > mb(); > > and: > > mem.buf = malloc(len); > assert(mem.buf != NULL); > mb(); > > th_req[Th_Num]++; > > mem.delete = 1; > mb(); > > or save yourself the trouble and use a mutex instead. > > Honestly I didn't try your program, but I wouldn't consider blaming the kernel > or glibc as long as your code is incorrect. > > -- > Jeff Sturm > jeff.sturm@commerceone.com > > _______________________________________________ > Axp-list mailing list > Axp-list@redhat.com > https://listman.redhat.com/mailman/listinfo/axp-list