RE: [Dev-C++] Multi-Threading locks up
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ed C. <em...@to...> - 2003-09-17 18:28:48
|
Per,
It seems like that's probably the root of my problem... This is my first
multi-threaded app in a language without a garbage collector / built-in
memory management (like Java).=20
So, does this mean I need to allocate all memory for the threads from =
within
the main thread? For example, one of the thread-level variables for each
thread is a memory buffer of 50k for batching SQL statements. If I
understand you, then, I need to allocate that memory from the main =
thread
and only let one thread access it. Is that what you're saying?
-Ed
P.S. How would I do the memory locking you describe, under Win32? I'm =
plenty
familiar with the concept, but don't know how to implement it in C.
=09
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]=20
Sent: Wednesday, September 17, 2003 1:48 PM
To: Ed Cottrell
Cc: dev...@li...
Subject: Re: [Dev-C++] Multi-Threading locks up
I think you have a problem with shared memory. While each new thread =
shares
the same memory space with the parent, they should - must - not access =
the
same memory at the same time.
1 - are you linking with a thread-safe RTL, i.e. if the threads =
allocates
memory new/malloc/calloc will break if not thread-safe. The same goes =
for
the calls to open the connection to the database.
2 - All memory used in the threads should either be on the stack,
dynamically allocated or indexed into an array - one entry for each =
thread.
3 - If you have to access the same global variable from the different
threads, or for a child thread and the main thread, you have to protect =
the
access with a critical section, mutex, spinlock or similar to assert
serialized access.
/Per W
On Wed, 17 Sep 2003, Ed Cottrell wrote:
> Hi,
>
> I'm working on a multi-threaded database app, which connects to=20
> multiple databases and does data-mining. The main thread (which=20
> includes the Win32
> GUI) launches off a user-specified number of threads (say, 10), which =
then
> do the actually database queries.
>
> I've hit a weird snag, though. As written, the threads all start=20
> properly, but then pause themselves, immediately, or almost=20
> immediately (after updating 0 to maybe 500 records, out of several=20
> hundred thousand). The problem isn't on the database server end; it=20
> works fine, but reports that the threads have stopped running queries. =
> Pseudo-code:
>
> ...
> int i, MyThreads=3D10;
> for(i =3D 0; i < MyThreads; i++)
> {
> //Splits up the work, here, into MyThreads chunks
> _beginthread( [the arguments] )
> }
> ...
>
> This is where it gets weird... IF I create a message box first, the=20
> threads start properly AND run until finished! Pseudo-code:
>
> ...
> int i, MyThreads=3D10;
> char mymsg[10]
> for(i =3D 0; i < MyThreads; i++)
> {
> //Splits up the work, here, into MyThreads chunks
> _beginthread( [the arguments] )
> sprintf(mymsg, "%d", i);
> MessageBox(0, mymsg, mymsg, MB_OK);
> }
> ...
>
> The ONLY difference is in whether or not I show a message box. Anybody =
> know what that's about? Thanks a million!
>
> -Ed
>
>
|