221 lines
12 KiB
HTML
221 lines
12 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Example: Work with local SQL databases in multithreaded Pthread programs" />
|
|
<meta name="abstract" content="This example shows how to work with local SQL databases in multithreaded Pthread programs." />
|
|
<meta name="description" content="This example shows how to work with local SQL databases in multithreaded Pthread programs." />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahwdbcco.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahwdbcco.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="rzahwe22-e22rx" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Example: Work with local SQL databases in multithreaded
|
|
Pthread programs</title>
|
|
</head>
|
|
<body id="rzahwe22-e22rx"><a name="rzahwe22-e22rx"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Work with local SQL databases in multithreaded
|
|
Pthread programs</h1>
|
|
<div><p>This example shows how to work with local SQL databases in multithreaded
|
|
Pthread programs.</p>
|
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
<div class="p"> <pre>/*********************************************************/
|
|
/* Testcase: SQLEXAMPLE */
|
|
/* */
|
|
/* Function: */
|
|
/* Demonstrate how Embedded SQL can be used within a */
|
|
/* threaded ILE C program. This program creates and */
|
|
/* populates an SQL Table with data. Then, threads are */
|
|
/* created which each open a cursor and read all the */
|
|
/* data from the table. A semaphore is used to show */
|
|
/* how threads execution can be controlled. */
|
|
/* */
|
|
/* To compile program: */
|
|
/* CRTSQLCI OBJ(QGPL/SQLEXAMPLE) SRCFILE(QGPL/QCSRC) */
|
|
/* COMMIT(*NONE) RDB(*NONE) OBJTYPE(*MODULE) */
|
|
/* OUTPUT(*PRINT) DBGVIEW(*SOURCE) */
|
|
/* */
|
|
/* To bind program: */
|
|
/* CRTPGM PGM(QGPL/SQLEXAMPLE) */
|
|
/* MODULE(QGPL/SQLEXAMPLE) ACTGRP(*CALLER) */
|
|
/* */
|
|
/* To invoke program: */
|
|
/* SPAWN QGPL/SQLEXAMPLE */
|
|
/* */
|
|
/*********************************************************/
|
|
#define _MULTI_THREADED
|
|
#include <pthread.h>
|
|
#include <sys/sem.h>
|
|
#include <sys/types.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
static int semid;
|
|
static struct sembuf op_try1[1] = {0,0,0};
|
|
#define MAXTHREADS 2
|
|
|
|
void *threadrtn(void *parm);
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
int rc;
|
|
int *status;
|
|
pthread_t thids[MAXTHREADS];
|
|
EXEC SQL BEGIN DECLARE SECTION;
|
|
int i, j;
|
|
char insert[200];
|
|
EXEC SQL END DECLARE SECTION;
|
|
|
|
EXEC SQL INCLUDE SQLCA;
|
|
EXEC SQL INCLUDE SQLDA;
|
|
|
|
/* create a new semaphore */
|
|
semid=semget(IPC_PRIVATE,1,S_IRUSR|S_IWUSR);
|
|
|
|
printf("\nsemaphore created\n");
|
|
rc=semctl(semid, 0, SETVAL, 1);
|
|
|
|
printf("semaphore inited\n");
|
|
EXEC SQL WHENEVER SQLERROR CONTINUE;
|
|
EXEC SQL CREATE TABLE QGPL/SQLEXMP (COL1 INT,COL2 INT);
|
|
|
|
printf("SQL table created\n");
|
|
EXEC SQL WHENEVER SQLERROR GO TO :mainerror;
|
|
|
|
for (i=1,j=100;i<10;i++,j++) {
|
|
(void) sprintf(insert, "INSERT INTO QGPL/SQLEXMP \
|
|
VALUES(%d, %d)", i, j);
|
|
EXEC SQL EXECUTE IMMEDIATE :insert;
|
|
}
|
|
|
|
printf("Table primed with data\n");
|
|
for (i=0;i<MAXTHREADS;i++) {
|
|
pthread_create(&thids[i], NULL, threadrtn, NULL);
|
|
}
|
|
printf("Threads created\n");
|
|
|
|
rc=semctl(semid, 0, SETVAL, 0);
|
|
|
|
printf("Threads turned loose\n");
|
|
for (i=0;i<MAXTHREADS;i++) {
|
|
pthread_join(thids[i], &status);
|
|
}
|
|
|
|
printf("Threads joined\n");
|
|
return;
|
|
|
|
mainerror:
|
|
printf("ERROR: sqlcode = %d sqlstate = %d\n", SQLCODE, SQLSTATE);
|
|
}
|
|
|
|
/**********************************************************/
|
|
/* This thread will do the following: */
|
|
/* - Declare a cursor for the example table */
|
|
/* - Block on a semaphore until initial thread */
|
|
/* is ready for us to run */
|
|
/* - Open the cursor */
|
|
/* - Fetch data one row at a time in a loop until */
|
|
/* End of File is reached or an error occurs */
|
|
/* - Close the cursor and return */
|
|
/**********************************************************/
|
|
void *threadrtn(void *parm)
|
|
{
|
|
EXEC SQL INCLUDE SQLCA;
|
|
EXEC SQL INCLUDE SQLDA;
|
|
|
|
EXEC SQL BEGIN DECLARE SECTION;
|
|
long HV1, HV2;
|
|
EXEC SQL END DECLARE SECTION;
|
|
|
|
EXEC SQL WHENEVER SQLERROR GO TO :thderror;
|
|
EXEC SQL WHENEVER NOT FOUND GO TO :thdeof;
|
|
|
|
EXEC SQL DECLARE C1 CURSOR FOR SELECT * FROM QGPL/SQLEXMP;
|
|
|
|
/* block on semaphore */
|
|
semop(semid,&op_try1[0],1);
|
|
|
|
EXEC SQL OPEN C1;
|
|
printf("thid:%.8x %.8x: cursor open\n",pthread_getthreadid_np());
|
|
|
|
/* Loop until End of File (EOF) */
|
|
for (;;) {
|
|
EXEC SQL FETCH C1 INTO :HV1, :HV2;
|
|
printf("thid:%.8x %.8x: fetch done... COL1=%d COL2=%d\n",
|
|
pthread_getthreadid_np(), HV1, HV2);
|
|
}
|
|
|
|
thderror:
|
|
printf("thid:%.8x %.8x: sqlcode = %d sqlstate = %d\n",
|
|
pthread_getthreadid_np(), SQLCODE, SQLSTATE);
|
|
EXEC SQL CLOSE C1;
|
|
return;
|
|
|
|
thdeof:
|
|
printf("thid:%.8x %.8x: Done!\n",
|
|
pthread_getthreadid_np());
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
Testcase output:
|
|
semaphore created
|
|
semaphore inited
|
|
SQL table created
|
|
Table primed with data
|
|
Threads created
|
|
Threads turned loose
|
|
thid:00000000 00000022: cursor open
|
|
thid:00000000 00000023: cursor open
|
|
thid:00000000 00000023: fetch done... COL1=1 COL2=100
|
|
thid:00000000 00000022: fetch done... COL1=1 COL2=100
|
|
thid:00000000 00000023: fetch done... COL1=2 COL2=101
|
|
thid:00000000 00000022: fetch done... COL1=2 COL2=101
|
|
thid:00000000 00000023: fetch done... COL1=3 COL2=102
|
|
thid:00000000 00000022: fetch done... COL1=3 COL2=102
|
|
thid:00000000 00000023: fetch done... COL1=4 COL2=103
|
|
thid:00000000 00000022: fetch done... COL1=4 COL2=103
|
|
thid:00000000 00000023: fetch done... COL1=5 COL2=104
|
|
thid:00000000 00000022: fetch done... COL1=5 COL2=104
|
|
thid:00000000 00000023: fetch done... COL1=6 COL2=105
|
|
thid:00000000 00000022: fetch done... COL1=6 COL2=105
|
|
thid:00000000 00000023: fetch done... COL1=7 COL2=106
|
|
thid:00000000 00000022: fetch done... COL1=7 COL2=106
|
|
thid:00000000 00000023: fetch done... COL1=8 COL2=107
|
|
thid:00000000 00000022: fetch done... COL1=8 COL2=107
|
|
thid:00000000 00000023: fetch done... COL1=9 COL2=108
|
|
thid:00000000 00000022: fetch done... COL1=9 COL2=108
|
|
thid:00000000 00000023: Done!
|
|
thid:00000000 00000022: Done!
|
|
Threads joined </pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwdbcco.htm" title="You should consider these items when using databases in multithreaded programs.">Database considerations for multithreaded programming</a></div>
|
|
</div>
|
|
<div class="relref"><strong>Related reference</strong><br />
|
|
<div><a href="rzahwdbcco.htm" title="You should consider these items when using databases in multithreaded programs.">Database considerations for multithreaded programming</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |