Rational Developer for System z
COBOL for Windows バージョン 7.5 プログラミング・ガイド


例: マルチスレッド環境での COBOL の使用

このマルチスレッド化のサンプルは、1 つの C メインプログラムと 2 つの COBOL プログラムで構成されています。

thrcob.c
2 つの COBOL スレッドを作成し、それらが完了してから終了する C メインプログラム
subd.cbl
thrcob.c で作成されたスレッドによって実行される COBOL プログラム
sube.cbl
thrcob.c で作成されたスレッドによって実行される 2 番目の COBOL プログラム

マルチスレッド化の例を作成および実行するには、コマンド・プロンプトで次のコマンドを入力します。

thrcob.c のソース・コード

#define LINKAGE __stdcall
#include <windows.h>
#include  <stdio.h>
#include  <setjmp.h>
#include  <stdlib.h>
#pragma handler(SUBD)
#pragma handler(SUBE)

typedef int (LINKAGE *PRN) (long *);

long done;
jmp_buf Jmpbuf;

extern void _iwzCOBOLInit(int fcode, PFN StopFun, int *err_code, void *StopArg);
extern void _iwzCOBOLTerm(int fcode, int *err_code);

extern unsigned long LINAGE SUBD(void *);
extern unsigned long LINAGE SUBE(void *);

int LINKAGE StopFun(long *stoparg)
{
  printf(“inside StopFun. Got stoparg = %d\n”, *stoparg);
  *stoparg = 123;
  longjmp(Jmpbuf,1);
}

long StopArg = 0;

void LINKAGE testrc(int rc, const char *s)
{
  if (rc != 0){
    printf(“%s: Fatal error rc=%d\n”,s,rc);
    exit(-1);
  }
}

void LINKAGE pgmy(void)
{
  int rc;
  int parm1, parm2;

  DWORD t1, t2;
  HANDLE hThread1;
  HANDLE hThread2;

  parm1 = 20;
  parm2 = 10;
  _iwzCOBOLInit(1, StopFun, &rc, &StopArg);
  printf( “_iwzCOBOLinit got %d\n”,rc);

  hThread1 = CreateThread(
        NULL,                                  // no security attributes
        0,                                     // use default stack size
        SUBD,                                  // thread function
        &parml,                                // argument to thread function
        0,                                     // use default creation flags
        &tl);                                  // returns the thread identifier
  
  // Check the return value for success.
  if (hThread1 == NULL)
      exit(-1);

  testrc(rc,“create 1”);

  hThread1 = CreateThread(
        NULL,                                  // no security attributes
        0,                                     // use default stack size
        SUBE,                                  // thread function
        &parm2,                                // argument to thread function
        0,                                     // use default creation flags
        &t2);                                  // returns the thread identifier
  // Check the return value for success.
  if (hThread2 == NULL)
      exit(-1);

  testrc(rc,“create 2”);

  printf(“threads are %x and %x\n”,t1, t2);

  WaitForSingleObject( hThread2, INFINITE );
  
  WaitForSingleObject( hThread1, INFINITE );


  CloseHandle( hThread1 );
  CloseHandle( hThread2 );

  printf(“test gets done = %d \n”,done );
  _iwzCOBOLTerm(1, &rc);
  printf( “_iwzCOBOLTerm expects rc=0, got rc=%d\n”,rc);
}

main(char *argv,int argc)
{
  if (setjmp(Jmpbuf) ==0) (
    pgmy();
})

subd.cbl のソース・コード

PROCESS PGMNAME(MIXED)
       IDENTIFICATION DIVISION
       PROGRAM-ID. “SUBD”.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SPECIAL NAMES.
           DECIMAL-POINT IS COMMA.
       INPUT-OUTPUT SECTION. 
       DATA DIVISION.
       FILE SECTION.
       Working-Storage SECTION.

       Local-Storage Section.
       01 n2 pic 9(8) comp-5 value 0.
       
       Linkage Section.
       01 n1 pic 9(8) comp-5.
       
       PROCEDURE DIVISION using by Reference n1.
           Display “In SUBD ”

           perform n1 times 
              compute n2 = n2 + 1
              Display “From Thread 1: ” n2
              CALL “Sleep” Using by value 1000 
           end-perform
            
           GOBACK.

sube.cbl のソース・コード

PROCESS PGMNAME(MIXED)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. “SUBE”.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SPECIAL-NAMES.
           DECIMAL-POINT IS COMMA.
       INPUT-OUTPUT SECTION.
       DATA DIVISION.
       FILE SECTION.
       Working-Storage SECTION.

       Local-Storage Section.
       01 n2 pic 9(8) comp-5 value 0.
        
       Linkage Section.
       01 n1 pic 9(8) comp-5.
        
       PROCEDURE DIVISION using by reference n1.
      
           perform n1 times 
              compute n2 = n2 + 1
              Display “From Thread 2: ” n2
      *Go to sleep for 3/4 sec.
              CALL “Sleep” Using by value 750 
           end-perform
             
           GOBACK.

ご利用条件 | フィードバック

Copyright IBM Corporation 1996, 2008.
このインフォメーション・センターでは Eclipse テクノロジーが採用されています。(http://www.eclipse.org)