TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Phil Crown
from: Peter Fitzsimmons
date: 1995-10-22 16:21:04
subject: _beginthread too fast

PC> However, I have another problem with _beginthread().  In my PM terminal
 PC> program, if I don't call DosSleep(400) after calling _beginthread(), the

I think you are assuming that the new thread is up and running before the
_beginthread() function returns to the calling thread -- this is usually
NOT the case,  and you shouldn't start using something that is 
created/opened/alloced in the new thread without using an event semaphore.

For example:

mainthread:  _beginthread(thread2)
             DosWaitEventSem(hevIMAlive, -1);
             // it is now safe to use the things created by thread 2

thread2:     initialize data
             allocate data
             open data
             DosPostEventSem(hevIMAlive);


 PC> case IDM_SCRIPT_RUN:
 PC>     if(tidScr != 0) {
 PC>       Msg(0, ProgName, "Script already running\nAbort 
 PC> current script first");
 PC>       break;
 PC>     }
 PC>     rc = SelectRexxScript(pdata, fname); // FileDlg to select *.cmd file
 PC>     if(rc == TRUE) {
 PC>       tidScr = _beginthread(RunRexxScript, 16384, (PVOID)fname);
 PC>       if(tidScr == 0xffff) {
 PC>         Msg(0, ProgName, "%s %d\nError starting script thread",
 PC>         __FILE__, __LINE__);
 PC>         tidScr = 0;
 PC>       }
 PC>       else
 PC>         DosSleep(400);  // without this the thread won't start
 PC>     }
 PC>     break;

In general,  it is usually a better design to have the thread running
before you need it (ie: you start up your oft used secondary threads before
creating any windows,  and they never end).  You send them messages (not
nec PM messages) when you want them to do something.


--- Maximus/2 3.00
* Origin: Sol 3 * Toronto * V.32 * (905)858-8488 (1:259/414)
SEEN-BY: 270/101 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407
SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809
@PATH: 259/414 400 99 229/2 3615/50 396/1 270/101 712/515 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.