POSIX Threads and dynamic memory allocation
I am using POSIX threads and in each thread, I allocated some data dynamically using the 'new' operator in C++. Also, I am using the standard template library. I am facing problems with the dynamic memory allocation part. My code keeps crashing randomly at places where I am allocating memory, deleting memory or manipulating string or other objects provided by the C++ STL. I tried finding out on the internet, but I can't find out for sure if functions like malloc() are thread-safe. I tried to backtrace using gdb and always, the trace ends with free() or malloc(). Sometimes I get a SIGSEGV and sometimes, I get a memory corruption error. What could be the problem?
If there is a function which I pass as an argument to pthread_create, then does the function need to be reentrant? Is it fine to allocate memory dynamically in the function?