Click to See Complete Forum and Search --> : Threads crashing during memory allocation


lechoo
February 14th, 2006, 02:32 PM
First of all I'm using Borland C++ Builder 6 and it's TThread objects.
My application is simple image converter. I have list of images and each of them is converted in a thread. I lauch only as many threads as there are procesors in system. When one of them has finished I create next until all images are converted.

At some point threads are allocating memory using new operator. This is where they crash (not instantly, but after some images are converted). This happens only on dual procesor machines. If I use multiple threads on single procesor machine everything works nice. My guess is that threads are trying to allocate same area of memory at same time. I know I can use TThread's Synchronize method or critical sections but since I use new in few places in different functions I'd like to keep my code clean.

Here's my question: is it possible to create area in memory for thread to use it? For example when new is called in thread, memory will be allocated inside this area. That way every thread will have it's own area and there will be no need for synchronization. Is it possible at all?

thanks in advance

Siddhartha
February 14th, 2006, 02:55 PM
Here's my question: is it possible to create area in memory for thread to use it?Placement New is one way of pre-allocating memory to be used by an object later.
(But this may not be what you need... )
For example when new is called in thread, memory will be allocated inside this area. That way every thread will have it's own area and there will be no need for synchronization. You see, so long as the two threads in question are allocating their own objects and are using their own objects, there is essentially little need for synchronization. One needs to synchronize threads when there exists situations where more than one thread access a (shared)
resource. this resource may be as simple as a string. So long as it is shared across threads you need to synchronize access (and this in itself can be as simple as using a CRITICAL_SECTION object that ensures thread-safety of the code that writes or reads a string.

However, if you are in a situation where you face a crash in spite of not sharing a resouce, ensure that you are using a Multithreaded version of the CRT (i.e. linking release mode with added option /MT, and debug mode with /MTd).

A relevant read -

How to link with the correct C Run-Time (CRT) library (http://support.microsoft.com/default.aspx?scid=kb;en-us;140584)

lechoo
February 15th, 2006, 03:46 PM
However, if you are in a situation where you face a crash in spite of not sharing a resouce, ensure that you are using a Multithreaded version of the CRT (i.e. linking release mode with added option /MT, and debug mode with /MTd).


Thanks Siddhartha. I'll try that.

Siddhartha
February 15th, 2006, 03:55 PM
You are welcome... Let us know if that linker switch solved your problem... ;)

lechoo
February 17th, 2006, 10:13 AM
I have bit of a problem, I have no idea where to add /MT in Borland C++ Builder.
BTW I already have switch (-tWM) which tells compiler to create multi-threaded output.

Siddhartha
February 17th, 2006, 10:18 AM
I have bit of a problem, I have no idea where to add /MT in Borland C++ Builder.
BTW I already have switch (-tWM) which tells compiler to create multi-threaded output.You need to consult the documentation of the Borland Linker. What I said above was wrt VC++ Linker.

If your CRT is already multithreaded, are you sure you are not sharing any objects between the threads?