Jump to content

Template:OS2API Example CritSec: Difference between revisions

From EDM2
Ak120 (talk | contribs)
Created page with "This example shows how a thread enters and exits a critical section of code. <code> #define INCL_DOSPROCESS Process values: #define INCL_DOSERRORS /* Error v..."
 
Ak120 (talk | contribs)
mNo edit summary
 
Line 7: Line 7:
   
   
  int main(VOID)
  int main(VOID)
  {
{
   APIRET  rc = NO_ERROR;    /* Return code */
   APIRET  rc = NO_ERROR;    /* Return code */
   
   
    rc = DosEnterCritSec();
  rc = DosEnterCritSec();
   
   
    if (rc != NO_ERROR) {
  if (rc != NO_ERROR) {
        printf("DosEnterCritSec error: return code = %u\n",rc);
      printf("DosEnterCritSec error: return code = %u\n",rc);
        return 1;
      return 1;
    }
  }
      /***********************************************/
    /***********************************************/
      /* Add critical section code here.  While this */
    /* Add critical section code here.  While this */
      /* code is running, all other threads are      */
    /* code is running, all other threads are      */
      /* stopped.  CALL NO LIBRARY OR OS/2 FUNCTIONS */
    /* stopped.  CALL NO LIBRARY OR OS/2 FUNCTIONS */
      /* HERE UNLESS YOU KNOW THEY DO NOT REQUIRE    */
    /* HERE UNLESS YOU KNOW THEY DO NOT REQUIRE    */
      /* ACTION BY ANOTHER THREAD IN THE PROCESS.    */
    /* ACTION BY ANOTHER THREAD IN THE PROCESS.    */
      /***********************************************/
    /***********************************************/
   
   
    rc =  DosExitCritSec();
  rc =  DosExitCritSec();
   
   
    if (rc != NO_ERROR) {
  if (rc != NO_ERROR) {
        printf("DosExitCritSec error: return code = %u\n",rc);
      printf("DosExitCritSec error: return code = %u\n",rc);
        return 1;
      return 1;
    }
  }
   
   
    return NO_ERROR;
  return NO_ERROR;
    }
}
</code>
</code>

Latest revision as of 18:28, 2 May 2020

This example shows how a thread enters and exits a critical section of code.

#define INCL_DOSPROCESS      /* Process values */
#define INCL_DOSERRORS       /* Error values */
#include <os2.h>
#include <stdio.h>

int main(VOID)
{
  APIRET  rc = NO_ERROR;    /* Return code */

  rc = DosEnterCritSec();

  if (rc != NO_ERROR) {
      printf("DosEnterCritSec error: return code = %u\n",rc);
      return 1;
  }
    /***********************************************/
    /* Add critical section code here.  While this */
    /* code is running, all other threads are      */
    /* stopped.  CALL NO LIBRARY OR OS/2 FUNCTIONS */
    /* HERE UNLESS YOU KNOW THEY DO NOT REQUIRE    */
    /* ACTION BY ANOTHER THREAD IN THE PROCESS.    */
    /***********************************************/

  rc =  DosExitCritSec();

  if (rc != NO_ERROR) {
      printf("DosExitCritSec error: return code = %u\n",rc);
      return 1;
  }

  return NO_ERROR;
}