how to invoke thread-safe glibc functions?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

how to invoke thread-safe glibc functions?

Jiayuan Meng
Hi all,

I am implementing a multi-threaded API manually on a hardware simulator. We don't have pthread support, so all we do to create a thread is just creating a new stack and assign the PC. However, we have problems with glibc functions such as malloc. How to invoke glibc to be in thread-safe mode?

As an example:
void thread_main()
    int* x = new int[100];
    delete [] x;
void main()
    for(int n=0; n<4; n++)        
I am expecting that the "x"s in each thread is assigned to a different address in the heap. But I found that the 4 threads allocate exactly thesame address to the 4 "x"s, thus, when they try to deallocate "x", it is freed four times and the application invokes an error:
*** glibc detected *** double free or corruption (fasttop):0x00000001201d8a40 ***