TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Keith Bennett
from: Russell Coker
date: 1994-09-10 21:29:20
subject: _Beginthread() Data Ptr

KB>This question concerns the 4th parameter passed to 
KB>_beginthread(). This is the void * that can point to 
KB>anything the programmer wants it to.

KB>I have a function that loops and starts 'n' separate 
KB>threads (pls see example below).  In order to be safe, do I 
KB>need to guarantee that the memory pointed to by that 
KB>pointer I pass is active and dedicated exclusively to that 
KB>thread for the thread's lifetime?  (Assuming I don't *want* 
KB>any interference from other threads.)

  If you don't want any interferance then you must use a seperate memory
region for each thread.  However there is no reason as far as the OS/2 API
is concerned for this to be the case, it is just dependant on your design
of your application.

KB>For example, I assume that the code below will not work 
KB>properly because the contents pointed to by &n may change 
KB>before the newly started thread can read it:

KB>void _Optlink my_thread_func( void *args)
KB>{
KB>        int the_number_passed = * (int *) args;  /* ... */
KB>}

KB>void my_thread_starting_func()
KB>{
KB>        for( int n = 0; n < 4; n++)
KB>         _beginthread( my_thread_func, 0, 32 * 1024, &n);
KB>}

   Only if the design of your program requires that the data not change.  A
program that I've written uses multiple threads with the same shared memory
area.  In my program it is possible that the data may change before the
newly created thread can read it, but that is OK, it fits in perfectly with
the design of my program.

KB>But how about if I change this to:

KB>void my_thread_starting_func()
KB>{
KB>        int n[4];

KB>        n[0] = 0; n[1] = 1; n[2] = 2; n[3] = 3;

KB>        for( int i = 0; i < 4; i++)
KB>         _beginthread( my_thread_func, 0, 32 * 1024, &n[i]);}

KB>What if 'my_thread_starting_func()' finishes before one or 
KB>more of my 'my_thread_func()''s reads the value, and that 
KB>stack area is already overwritten by another function?

   I'm not sure about that.  In that case I wouldn't use stack memory for
that, I'd allocate some memory with DosAllocMem().....

KB>So, am I correct in assuming that in this situation I must absolutely 
KB>guarantee that this memory will be used exclusively by that 
KB>thread for as long as the thread might need it?  And 
KB>therefore, that I should probably use a static int array?

   Not unless the algorithms in your program require non-shared memory.  In
my program I use this memory area for setting control flags that other
threads can read (yes I know that using shared memory regions in this way
makes race conditions possible, but such race conditions don't cause
problems for my application for reasons I won't bother explaining - if they
did then I'd use one of the many thread synchronisation mechanisms to avoid
this).


   cya
___
 X MR/2 2.0 NR X (((((((YOU))))(((MUST))((WRITE))((IN)))((((LISP))))))))

--- Maximus/2 2.01wb

* Origin: Multi - 61-3-739-7145 (3:633/363)
SEEN-BY: 12/2442 54/54 620/243 624/50 632/301 341 348 365 386 998 633/104 252
SEEN-BY: 633/260 357 363 371 373 379 634/384 635/210 502 503 636/100 638/100
SEEN-BY: 640/820 690/660 711/409 410 413 430 807 808 809 934 942 712/353 623
SEEN-BY: 713/888 800/1
@PATH: 633/363 260 371 635/503 632/348 711/409 54/54 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™.