Доступные версии документации

Приложение 4. Пример многопоточной обработки

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <process.h>
#else
#ifdef UNIX
# ifdef LINUX
#  include <pthread/mit/pthread.h>
# endif
#endif
#endif
#ifdef __WATCOMC__
#include <dos.h>
#endif
#ifdef WIN32
void __stdcall th1(void*);
void __stdcall th2(void*);
void __stdcall th3(void*);
#else
#ifdef UNIX
# ifdef LINUX
  void* th1(void*);
  void* th2(void*);
  void* th3(void*);
# endif
#endif
#endif
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
CONTEXT ctx1, ctx2, ctx3;
VARCHAR uid[20], pwd[20];
char name[20];
char city[15];
EXEC SQL END DECLARE SECTION;
#ifdef WIN32
unsigned long id1, id2, id3;
#else
#ifdef UNIX
# ifdef LINUX
  thread_t id1, id2, id3;
# endif
#endif
#endif
int ret;
int Wth1, Wth2, Wth3;
void main()
{
strcpy(uid.arr,"SYSTEM");
uid.len=strlen(uid.arr);
strcpy(pwd.arr,"MANAGER");
pwd.len=strlen(pwd.arr);
EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
EXEC SQL ENABLE THREADS;
EXEC SQL CONTEXT ALLOCATE :ctx1;
EXEC SQL CONTEXT ALLOCATE :ctx2;
EXEC SQL CONTEXT ALLOCATE :ctx3;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
printf ("Thr0 : PCIA_Connect()\n");
Wth1 = Wth2 = Wth3 = 1;
#if defined WIN32
  _beginthreadex(0, 16384,th1, 0, 0, &id1);
  _beginthreadex(0, 16384,th2, 0, 0, &id2);
  _beginthreadex(0, 16384,th3, 0, 0, &id3);
#else
#if defined UNIX
# if defined LINUX
  pthread_create(&id1,NULL,th1,NULL);
  pthread_create(&id2,NULL,th2,NULL);
  pthread_create(&id3,NULL,th3,NULL);
# endif
#endif
#endif
EXEC SQL PREPARE ST FROM select name, city from person where Age <=30;
printf ("Thr0 : PCIA_Prepare()\n");
EXEC SQL DECLARE CR CURSOR_PCI FOR ST;
EXEC SQL OPEN CR;
printf ("Thr0 : PCIA_Open()\n");
for(;;)
{
EXEC SQL WHENEVER NOT FOUND GOTO not_found;
EXEC SQL FETCH CR INTO :name, :city;
printf ("Thr0 : PCIA_Fetch()\n");
printf("\nThr0 Name is : %s; City is : %s;", name,
    city);
}
not_found:
EXEC SQL CLOSE CR;
EXEC SQL COMMIT RELEASE;
printf ("Thr0 : PCIA_CommRoll()\n");
sqlerr:
printf ("Thr0 Error: %ld\n",ErrPCI(CtxPCI_));
while (Wth1 || Wth2 || Wth3)sleep(0);
EXEC SQL CONTEXT FREE :ctx1;
EXEC SQL CONTEXT FREE :ctx2;
EXEC SQL CONTEXT FREE :ctx3;
}
/****************************************************************/
/*************************** THREAD 1 ***************************/
/****************************************************************/
#ifdef WIN32
void __stdcall th1(void*p)
#else
#ifdef UNIX
void * th1(void*p)
#endif
#endif
{
struct sqlca sqlca;
EXEC SQL MODULE M1;
EXEC SQL CONTEXT USE :ctx1;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
printf ("Thr1 : PCIA_Connect()\n");
EXEC SQL PREPARE ST FROM select name, city from person where Age <=30;
EXEC SQL DECLARE CR CURSOR_PCI FOR ST;
EXEC SQL OPEN CR;
printf ("Thr1 : PCIA_Open()\n");
for(;;)
{
 EXEC SQL WHENEVER NOT FOUND GOTO not_found;
 EXEC SQL FETCH CR INTO :name, :city;
 printf("\nThr1: Name is : %s; City is : %s;", name,
     city);
 printf ("\nThr1 : PCIA_Fetch()");
}
not_found:
EXEC SQL CLOSE CR;
printf ("Thr1 : PCIA_Close()\n");
EXEC SQL COMMIT RELEASE;
printf ("Thr1 : PCIA_CommRoll()\n");
sqlerr:
printf ("Thr1 Error: %ld\n",ErrPCI(ctx1));
EXEC SQL END MODULE M1;
Wth1=0;
#ifdef WIN32
  _endthreadex(0);
#else
#ifdef UNIX
  pthread_exit(&Wth1); return Wth1;
#endif
#endif
}
/****************************************************************/
/***************************** THREAD 2 *************************/
/****************************************************************/
#ifdef WIN32
void __stdcall th2(void*p)
#else
#ifdef UNIX
void * th2(void*p)
#endif
#endif
{
struct sqlca sqlca;
EXEC SQL MODULE M2;
EXEC SQL CONTEXT USE :ctx2;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
printf ("Thr2 : PCIA_Connect()\n");
EXEC SQL PREPARE ST FROM select name, city from person where Age <=30;
EXEC SQL DECLARE CR CURSOR_PCI FOR ST;
EXEC SQL OPEN CR;
printf ("Thr2 : PCIA_Open()\n");
for(;;)
{
 EXEC SQL WHENEVER NOT FOUND GOTO not_found;
 EXEC SQL FETCH CR INTO :name, :city;
 printf("\nThr2: Name is : %s; City is : %s;", name,
     city);
 printf ("\nThr2 : PCIA_Fetch()");
}
not_found:
EXEC SQL CLOSE CR;
printf ("Thr2 : PCIA_Close()\n");
EXEC SQL COMMIT RELEASE;
printf ("Thr2 : PCIA_CommRoll()\n");
sqlerr:
printf ("Thr2 Error: %ld\n",ErrPCI(ctx2));
EXEC SQL END MODULE M2;
Wth2=0;
#ifdef WIN32
  _endthreadex(0);
#else
#ifdef UNIX
  pthread_exit(&Wth2); return &Wth2;
#endif
#endif
}
/****************************************************************/
/**************************** THREAD 3 **************************/
/****************************************************************/
#ifdef WIN32
void __stdcall th3(void*p)
#else
#ifdef UNIX
void * th3(void*p)
#endif
#endif
{
struct sqlca sqlca;
EXEC SQL MODULE M3;
EXEC SQL CONTEXT USE :ctx3;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
printf ("Thr3 : PCIA_Connect()\n");
EXEC SQL PREPARE ST FROM select name, city from person where Age <=30;
EXEC SQL DECLARE CR CURSOR_PCI FOR ST;
EXEC SQL OPEN CR;
printf ("Thr3 : PCIA_Open()\n");
for(;;)
{
 EXEC SQL WHENEVER NOT FOUND GOTO not_found;
 EXEC SQL FETCH CR INTO :name, :city;
 printf("\nThr3: Name is : %s; City is : %s;", name,
     city);
 printf ("\nThr3 : PCIA_Fetch()");
}
not_found:
EXEC SQL CLOSE CR;
printf ("Thr3 : PCIA_Close()\n");
EXEC SQL COMMIT RELEASE;
printf ("Thr3 : PCIA_CommRoll()\n");
sqlerr:
printf ("Thr3 Error: %ld\n",ErrPCI(ctx3));
EXEC SQL END MODULE M3;
Wth3=0;
#ifdef WIN32
  _endthreadex(0);
#else
#ifdef UNIX
  pthread_exit(&Wth3); return Wth3;
#endif
#endif
}
/****************************************************************/
/************************ END OF SOURCE**************************/
/****************************************************************/
Заметили ошибку?
Выделите текст и нажмите Ctrl + Enter