This is due to the fact that I discussed here here and here , but as my investigation led me from the STL as a potential problem, and to the "new" as to my nemisis, I thought it best to start a new thread.
I repeat, I am using the arm-linux cross compiler (version 2.95.2) provided by the vendor of the embedded platform.
When I run the application below on my Linux PC, it certainly will never work. However, when I run on the embedded device every time I get segmentation errors. The use of "malloc" is never interrupted. Syncing the “new” distribution using the mutex will stop the problem, but this is not practical in my main application.
Can anyone suggest why this might happen, or have any ideas how I can get around this?
Thank.
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t _logLock = PTHREAD_MUTEX_INITIALIZER;
static void* Thread(void *arg)
{
int i = 0;
while (i++ < 500)
{
char* myDyn = (char*) new char[1023];
delete[] myDyn;
}
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
int threads = 50;
pthread_t _rx_thread[threads];
for (int i = 0; i < threads; i++)
{
printf("Start Thread: %i\n", i);
pthread_create(&_rx_thread[i], NULL, Thread, NULL);
}
for (int i = 0; i < threads; i++)
{
pthread_join(_rx_thread[i], NULL);
printf("End Thread: %i\n", i);
}
}
source
share