Przemek:

any comments ?

David Macias


I found in documentation included in OW a sample for multithreading

Entirely page is below
Przemek, can you check page ?

They talk about DosEnterCritSec() and DosExitCritSec() to prevent
collisions between threads

I tested using
  wcl386 -bt=os2 -bm -l=os2v2 mthread

and results are:
------------
Initial thread id = 1
Hi from thread 5
Hi from thread 4
Hi from thread 3
Hi from thread 2
------------

There are this info in doc too

------------
There is a limit to the number of threads an application can create
under 16-bit OS/2.  The default limit is 32.  This limit can be adjusted
by statically initializing the unsigned global variable __MaxThreads.

Under 32-bit OS/2, there is no limit to the number of threads an
application can create.  However, due to the way in which multiple
threads are supported in the Open Watcom libraries, there is a small
performance penalty once the number of threads exceeds the default limit
of 32 (this number includes the initial thread).  If you are creating
more than 32 threads and wish to avoid this performance penalty, you can
redefine the threshold value of 32.  You can statically initialize the
global variable __MaxThreads.

By adding the following line to your multi-threaded application, the new
threshold value will be set to 48.


     unsigned __MaxThreads = { 48 };
------------

David Macias



Let us create a simple multi-threaded application.  The source code for
this example can be found in \watcom\samples\os2.


     #include <process.h>
     #include <stdio.h>
     #include <stddef.h>
     #define INCL_DOS
     #include <os2.h>

     static  volatile int    NumThreads;
     static  volatile int    HoldThreads;

     #define NUM_THREADS     5
     #define STACK_SIZE      32768

     static void a_thread( void *arglist )
     /***********************************/
     {
         while( HoldThreads ) {
             DosSleep( 1 );
         }
         printf( "Hi from thread %d\n", *_threadid );
         DosEnterCritSec();
         --NumThreads;
         DosExitCritSec();
         _endthread();
     }

     int main( void )
     /**************/
     {
         int         i;

         printf( "Initial thread id = %d\n", *_threadid );
         NumThreads = 0;
         HoldThreads = 1;
         /* initial thread counts as 1 */
         for( i = 2; i <= NUM_THREADS; ++i ) {
             if( _beginthread( a_thread, NULL, STACK_SIZE, NULL ) == -1
)
             {
                 printf( "creation of thread %d failed\n", i );
             } else {
                 ++NumThreads;
             }
         }

         HoldThreads = 0;
         while( NumThreads != 0 ) {
             DosSleep( 1 );
         }
         return( 0 );
     }

Note:

 1.In the function a_thread, DosEnterCritSec and DosExitCritSec are
called when we modify the variable NumThreads.  This ensures that the
action of extracting the value of NumThreads from memory, incrementing
the value, and storing the new result into memory, occurs without
interruption.  If these functions were not called, it would be possible
for two threads to extract the value of NumThreads from memory before an
update occurred.

Let us assume that the file mthread.c contains the above example.
Before compiling the file, make sure that the WATCOM environment
variable is set to the directory in which you installed Open Watcom
C/C++.  Also, the INCLUDE environment variable must include the
\watcom\h\os2 and \watcom\h directories ("\WATCOM" is the directory in
which Open Watcom C/C++ was installed).

We can now compile and link the application by issuing the following
command.


     [C:\]wcl386 -bt=os2 -bm -l=os2v2 mthread

The "bm" option must be specified since we are creating a multi-threaded
application.  If your multi-threaded application contains more than one
module, each module must be compiled using the "bm" switch.

The "l" option specifies the target system for which the application is
to be linked.  The system name os2v2 is defined in the file wlsystem.lnk
which is located in the "BINW" subdirectory of the directory in which
you installed Open Watcom C/C++.

The multi-threaded application is now ready to be run.


_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to