Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with multi-threading on a dual CPU's

22 views
Skip to first unread message

Sean Kneller

unread,
Sep 6, 2002, 10:48:05 AM9/6/02
to
Hi,

I have a simple test app. which creates multiple threads. The app
works fine on a single CPU PC, but fails randomly (but frequently) on
a dual CPU PC. Both PCs are running W2K. The errors are 5333's,
GPF's, Dr Watsons, etc. :

DEFINE THREADCOUNT := 20
DEFINE LOOPCOUNT := 1000000
//
FUNCTION Start()

LOCAL DIM aThreadHandles[ THREADCOUNT ] AS PTR
LOCAL i, dwID AS DWORD

FOR i := 1 UPTO THREADCOUNT
aThreadHandles[ i ] := CreateVOThread( NULL, 0,
@ThreadProc(), i, 0, @dwID )
NEXT i

DevPos( 24, 0 )
?? "Waiting for threads"

WaitForMultipleObjects( THREADCOUNT, @aThreadHandles[ 1 ], TRUE,
INFINITE )

DevPos( 24, 78 )
?? _chr( ASC_BELL )+"."

DevPos( 24, 0 )
?? "Press enter to exit :"
inkey( 5 )

FOR i := 1 UPTO THREADCOUNT
CloseHandle( aThreadHandles[ i ] )
NEXT i

RETURN

STATIC FUNCTION ThreadProc( dwNum AS DWORD ) AS DWORD PASCAL

LOCAL oThread AS TestThread

// oThread := TestThread{ dwNum }
oThread := CreateInstanceStatic( #TestThread, dwNum )

oThread:Start()

oThread := NULL_OBJECT

ExitVOThread( 0 )

RETURN 0

CLASS TestThread

PROTECT dwThreadNum AS DWORD

METHOD Init( nNum ) CLASS TestThread

SELF:dwThreadNum := nNum

METHOD Start() CLASS TestThread

LOCAL dwCt AS DWORD

dwCt := 0

WHILE dwCt <= LOOPCOUNT
++dwCt
Sleep( 0 )
END WHILE


Any ideas ? If I move the test loop out of ThreadTest:Start() into
ThreadProc() it works fine. This suggests that its not possible to
create or send messages to VO objects when using dual CPU's. I don't
think this is a GC error, because I have also tried bracketing the
entire app using DynLock()/DynUnlock().

One other question, should the LOCAL DIM statment aThreadHandles[
THREADCOUNT ] be AS or IS PTR ?

Thanks, Sean

Ginny Caughey

unread,
Sep 6, 2002, 11:50:57 AM9/6/02
to
Hi Sean,

Congratulations on having a dual CPU PC to test on! I wish I did. I do believe
that it is the only way to really test multithreading.

Here are a few thoughts -

The WaitForMultipleObjects call should be WaitForMultipleObjects(THREADCOUNT,
@aThreadHandles, TRUE,
INFINITE )

Don't call ExitVOThread - just return from the threaded function. I don't think
this is causing your problem, but the ExitVOThread call isn't needed.

"AS PTR" is fine for thread handles. "IS" is really only needed with structures
that you don't want to specifically MemAlloc() and MemFree().

I don't see where you're sending any messages in your code, but if you are, be
sure to use PostMessage rather than SendMessage or VOSendMessage.

I haven't tried CreateInstanceStatic myself. Just creating dynamic VO objects is
supposed to be safe in worker threads (as long as the object isn't GUI related)
in VO 2.5+ - but again I never had the opportunity to test this on a dual
processor machine and I don't know if the CA guys who wrote the threading
support in VO 2.5 had one either.

Please let me know if you find out what is causing the problem.

Ginny


"Sean Kneller" <sean_k...@lycos.com> wrote in message
news:c4a1a9bc.02090...@posting.google.com...

Ginny Caughey

unread,
Sep 6, 2002, 11:55:44 AM9/6/02
to
Sean,

I also forgot to ask if you're testing by running within the IDE or from an EXE?
I've found multithreading to be more stable if you're not running from the IDE
for some reason.

Ginny

"Ginny Caughey" <ginny....@wasteworks.com> wrote in message
news:alaird$1nfc3l$1...@ID-144704.news.dfncis.de...

Sean Kneller

unread,
Sep 7, 2002, 6:46:23 AM9/7/02
to
Hi Ginny

Thanks for the reply.

If I told you the spec. of my dev. PC you might never speak to me
again !

I will try your suggestions on Monday - I've tried both running from
the IDE & EXE, with simular results - But to really test the
reliability I run the EXE 20 times in a row, using a batch file (from
memory) :

FOR /L %%i IN (1,1,20) DO START /WAIT THREADTEST.EXE

And then go for a cup of tea !

No, I'm not sending any messages - I made this is app is as cut-down
as possible.

Sean

"Ginny Caughey" <ginny....@wasteworks.com> wrote in message news:<alaj4c$1mip2a$1...@ID-144704.news.dfncis.de>...

&gt; > > GPF's, Dr Watsons, etc. :

Ed Ratcliffe

unread,
Sep 7, 2002, 11:54:17 AM9/7/02
to
Sean,

> If I told you the spec. of my dev. PC you might never speak to me
> again !

Some of us might like to know what we "should" be asking for. <BG>

Ed

Sean Kneller

unread,
Sep 8, 2002, 6:56:03 AM9/8/02
to
Hi Ed,

Ok, dual 1ghz PIII's, 512mb RAM, 3 displays

Sean


"Ed Ratcliffe" <Gryphon...@telus.net> wrote in message news:<Jipe9.2294$_J3.1...@news0.telusplanet.net>...

Ed Ratcliffe

unread,
Sep 8, 2002, 10:30:25 AM9/8/02
to
Drool!

"Sean Kneller" <sean_k...@lycos.com> wrote in message

news:c4a1a9bc.0209...@posting.google.com...

Sean Kneller

unread,
Sep 9, 2002, 4:45:25 AM9/9/02
to
Hi Ginny

Tried your suggestions, but it had no noticable effects.

I searched the SDK looking for 'EnterCriticalSection', and found 6
ref's in CSession class of Internet SDK, and I'm wondering if VO
developers use critical sections in the internal code ?

(For those of your who've not attended Ginny class on Multithreading,
Critical sections do not work across different CPU's).

Anyway, its not what I would call a fix, but its a work-around : By
using the SetProcessAffinityMask() api call its possible to restrict a
process (and its threads) to a single processor. This code restricts
the process to the first CPU :

_DLL FUNC SetProcessAffinityMask( hProcess AS PTR,
dwProcessAffinityMask ) AS LOGIC
PASCAL:KERNEL32.SetProcessAffinityMask

SetProcessAffinityMask( GetCurrentProcess(), 1 )

(The prototype is not in the Win32 API lib)

I tested this using CPU 1 and it worked 100% (20 out of 20), but
failed once out of 20 using CPU 2, which is still significantly better
than before.

Note that dwProcessAffinityMask is a bitmap of valid processors, so a
value of 3 would allow the process (and threads) to run and both CPU's
1 & 2.

I hope this is of some help. DO you think I should notify Brian/Grafx
about this directly ?

Sean

"Ginny Caughey" <ginny....@wasteworks.com> wrote in message news:<alaj4c$1mip2a$1...@ID-144704.news.dfncis.de>...

Stephen Quinn

unread,
Sep 9, 2002, 8:13:29 AM9/9/02
to
Sean

Check out the
SetThreadIdealProcessor()
function to force a thread to use only 1 CPU.

For complete info on threads in Windows check out the following book (230+
pages)

Programming Application for M$ Windows 4th Ed.
Jeffrey Richter
ISBN 1-57231-996-8

--
HTH
Steve Quinn
http://www.tuxedo.org/~esr/faqs/smart-questions.html
'I want to move to Theory...Everything works in Theory'


Ginny Caughey

unread,
Sep 9, 2002, 10:01:23 AM9/9/02
to
Sean,

I will speak to you anyway, but I do feel a little sick at the moment. <BG>

Ginny

"Sean Kneller" <sean_k...@lycos.com> wrote in message

news:c4a1a9bc.0209...@posting.google.com...

Ginny Caughey

unread,
Sep 9, 2002, 10:20:45 AM9/9/02
to
Sean,

I do use critical sections in my code on occasion when I want to be sure that
only one thread at a time is updating a variable or writing to a log file. I am
not aware that critical sections don't work across multiple processors, although
they only work within a single application and not across multiple processes.
Have you found that they do not work in your tests? If yes, I think you should
send a test case to Brian.

Sean Kneller

unread,
Sep 10, 2002, 3:57:02 AM9/10/02
to
Hi Stephen

Thanks for your reply. I looked at SetThreadIdealProcessor(), but it
only sets the preferred processor and does not guarantee to restrict
it to a single CPU.

Sean

"Stephen Quinn" <squ...@brutecom.com.au> wrote in message news:<ali482$1q3vbb$1...@ID-88745.news.dfncis.de>...

David Earl

unread,
Sep 10, 2002, 3:57:19 AM9/10/02
to
>If I told you the spec. of my dev. PC you might never speak to me
>again !

Oh, I dunno, if I was working with threads, I might be inclined to
speak to you more often..."Could you test this for me?"...at least
until a lottery or two have been won.

David.

On 7 Sep 2002 03:46:23 -0700, sean_k...@lycos.com (Sean Kneller)
wrote:

Sean Kneller

unread,
Sep 10, 2002, 4:19:42 AM9/10/02
to
Hi Ginny

I could have sworn you or Rod said critical sections don't work across
CPU's at DevFest02 in London. Maybe I miss-heard process for
processor <duh>.

I will investigate a bit futher.

Thanks, Sean

"Ginny Caughey" <ginny....@wasteworks.com> wrote in message news:<aliam7$1qctaj$1...@ID-144704.news.dfncis.de>...

Ginny Caughey

unread,
Sep 10, 2002, 9:41:31 AM9/10/02
to
Sean,

I think that's exactly what happened. If critical sections didn't work across
different processors, they'd be pretty useless. But since I don't have a nice
machine like yours, I can't test it myself and I'm very interested in your
results.

Ginny

"Sean Kneller" <sean_k...@lycos.com> wrote in message

news:c4a1a9bc.02091...@posting.google.com...

Sean Kneller

unread,
Sep 11, 2002, 5:10:50 AM9/11/02
to
Hi Ginny

I spent a couple of hours trying to break the critical sections on
dual CPU's without any 'success', so that was all a storm in a tea
cup.

But the original issue still stands with my test app not working on
dual CPU's.

Thanks again for your help, Sean

"Ginny Caughey" <ginny....@wasteworks.com> wrote in message news:<alksol$1rgorl$1...@ID-144704.news.dfncis.de>...

Ginny Caughey

unread,
Sep 11, 2002, 10:35:47 AM9/11/02
to
Sean,

Thanks for the test results. At least that worked as expected.

0 new messages