This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

how to invoke thread-safe glibc functions?


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++)        
       my_thread_create(&thread_main);
}
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 ***

Thanks!

Jiayuan

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]