Re: [Dev-C++] Multi-Threading locks up
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-09-17 17:48:56
|
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 multiple
> databases and does data-mining. The main thread (which 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 properly,
> but then pause themselves, immediately, or almost immediately (after
> updating 0 to maybe 500 records, out of several hundred thousand). The
> problem isn't on the database server end; it works fine, but reports that
> the threads have stopped running queries. Pseudo-code:
>
> ...
> int i, MyThreads=10;
> for(i = 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 threads
> start properly AND run until finished! Pseudo-code:
>
> ...
> int i, MyThreads=10;
> char mymsg[10]
> for(i = 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
>
>
|