マルチスレッドで動作するCUPのコーディング例を示します。この例は,CUPから送信したメッセージをエコーバックするSPPを呼び出すプログラムです。
000010 #include <stdio.h>
000020 #include <dcvclt.h>
000030 #include <dcvrpc.h>
000040 #include <pthread.h>
000050 #include <sys/errno.h>
000060 #define BUFSIZE 512
000070 #define SERVICE "spp01"
000080 #define THDMAX 5
000090
000100 void *CUP_thread(void *arg)
000110 {
000120 char in[BUFSIZE];
000130 DCULONG in_len;
000140 char out[BUFSIZE];
000150 DCULONG out_len;
000160 int rc = DC_OK;
000170 DCCLT_ID cltid;
000180 int myid;
000190
000200 myid = (int)arg;
000210
000220 /*--- クライアントユーザの認証要求 ---*/
000230 if ((rc = dc_clt_cltin_s(NULL, &cltid, NULL, NULL,
000240 "user01", "puser01", NULL, DCNOFLAGS)) != DC_OK) {
000250 printf("cup%d: dc_clt_cltinに失敗しました。CODE=%d¥n", myid, rc);
000260 goto PROG_EXIT;
000270 }
000280
000290 /*--- RPC-OPEN (RPC環境の初期設定) ---*/
000300 if ((rc = dc_rpc_open_s(cltid, DCNOFLAGS)) != DC_OK) {
000310 printf("cup%d: dc_rpc_openに失敗しました。CODE=%d¥n", myid, rc);
000320 goto PROG_END;
000330 }
000340
000350 /*--- RPC-CALL (RPCの実行) ---*/
000360 strcpy(in, "HELLO SPP !!");
000370 in_len = strlen(in) + 1;
000380 out_len = sizeof(out);
000390 if ((rc = dc_rpc_call_s(cltid, SERVICE, "echo", in, &in_len,
000400 out, &out_len, DCNOFLAGS)) != DC_OK) {
000410 printf("cup%d: dc_rpc_callに失敗しました。CODE=%d¥n", myid, rc);
000420 goto PROG_END;
000430 }
000440 printf("%s¥n", out);
000450 PROG_END:
000460
000470 /*--- RPC-CLOSE(RPC環境の解除)---*/
000480 dc_rpc_close_s(cltid, DCNOFLAGS);
000490
000500 PROG_EXIT:
000510 /*--- クライアントユーザの認証解除 ---*/
000520 dc_clt_cltout_s(cltid, DCNOFLAGS);
000530
000540 /*--- スレッドを終了させる ---*/
000550 pthread_exit(arg);
000560 }
000570
000580 main()
000590 {
000600 int i;
000610 int rc;
000620 int exit_value;
000630 pthread_t threads[THDMAX];
000640
000650 /*--- スレッドを生成する ---*/
000660 for (i = 1; i < THDMAX; i++) {
000670 rc = pthread_create((pthread_t *)&threads[i],
000680 NULL,
000690 CUP_thread,
000700 (void *)i);
000710 if (rc < 0) {
000720 printf("cup0: pthread_createに失敗しました。CODE=%d¥n", errno);
000730 }
000740 }
000750
000760 /*--- スレッドの終了を待ち合わせる ---*/
000770 for (i = 1; i < THDMAX; i++) {
000780 rc = pthread_join(threads[i], (void **)&exit_value);
000790 if (rc < 0) {
000800 printf("cup0: pthread_joinに失敗しました。CODE=%d¥n", errno);
000810 }
000820 }
000830 }