When posting code, please enclose it in [code] tags to improve readability.
- Compile the program with debugging symbols
- Set the ulimit so the program creates core files on segfaults (use the command "ulimit -c unlimited" in shell before running the program from that shell).
- Run the program until it crashes
- Load the corefile into gdb
- Do a backtrace
This will give you a starting point - probably you're trying to use a structure which is set to NULL (or is otherwise uninitialized) or whose address has been overwritten somehow. You should try to find a procedure for re-producing the error consistently - if you can, it's probably just a matter of breaking on memory access to the offending pointer, or realizing that it is never set in the first place.
This sort of problem is hard to debug. Threads makes it harder. It is why writing your own container code is not such a good idea if you can avoid it. Why not use a pre-written library for this sort of structure, which has already been debugged?