OpenTP1 Version 7 TP1/Client User's Guide TP1/Client/W, TP1/Client/P

[Contents][Index][Back][Next]

3.3.2 Creating a user application program that supports a multi-thread environment

This subsection shows a coding example of a CUP that can operate in a multi-thread environment. This coding example is a program to call an SPP that echoes back a message sent from the CUP.

 
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    /* Client user authentication request */
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 failed. CODE=%d\n", myid, rc);
000260       goto PROG_EXIT;
000270     }
000280
000290     /* RPC-OPEN (RPC environment initialization) */
000300    if ((rc = dc_rpc_open_s(cltid, DCNOFLAGS)) != DC_OK) {
000310        printf("cup%d: dc_rpc_open failed. CODE=%d\n", myid, rc);
000320        goto PROG_END;
000330    }
000340
000350    /* RPC-CALL (RPC execution) */---*/
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 failed. CODE=%d\n", myid, rc);
000420        goto PROG_END;
000430    }
000440    printf("%s\n", out);
000450 PROG_END:
000460
000470    /* RPC-CLOSE (RPC environment release) */
000480    dc_rpc_close_s(cltid, DCNOFLAGS);
000490
000500 PROG_EXIT:
000510    /* Client user authentication release */
000520    dc_clt_cltout_s(cltid, DCNOFLAGS);
000530
000540    /* Thread termination */
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    /* Thread creation */
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 failed. CODE=%d\n", errno);
000730        }
000740    }
000750
000760    /* Wait for thread termination */
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 failed.CODE=%d\n", errno);
000810        }
000820    }
000830 }