Skip to content

13. Multithreaded Applications#

Multithreaded Applications#

APRE can be used to author multi-threaded applications. This chapter explains how to use embedded SQL statements in multi-threaded programs, and highlights some considerations to keep in mind when doing so.

Configuring an Application as a Multithreaded Application#

When precompiling a multithreaded application, APRE needs to be informed that the application is a multithreaded application. This can be accomplished in either of the following two ways:

  • By setting the -mt option on the command line:

    < Example >

    $ apre -mt sample1.sc
    
  • Using the OPTION statement in the file to be precompiled:

    < Example >

    EXEC SQL OPTION (THREADS = TRUE);
    

Description#

  • Each thread must have its own database connection. In other words, it is impossible for multiple threads to share a single database connection.
  • The name of each connection must be unique within the application. If an attempt is made to establish a connection using the name of an established connection, an error will be raised, indicating that an established connection with that name already exists.
  • Only one connection that does not have a name (i.e. the default connection) is allowed.
  • The name of the connection to use must be specified within each embedded SQL statement. For more information about the use of connection names, please refer to Chapter 12: Applications with Multiple Database Connections.

Note: In previous versions of APRE (i.e. SESC versions 3.5.5 and earlier), it was necessary to use the EXEC SQL THREADS statement in all functions that contained embedded SQL statements and were intended for use in a multithreaded environment. In versions subsequent to version 3.5.5, this is not necessary, but there is no need to remove the EXEC SQL THREADS command from existing programs as its presence does not cause any compatibility problems.

Sample Program#

mt1.sc#

This sample can be found at $ALTIBASE_HOME/sample/APRE/mt1.sc.

Result of Execution#
$ is -f schema/schema.sql
$ make mt1
$ ./mt1
<MULTI THREADS 1>

mt2.sc#

This sample can be found at $ALTIBASE_HOME/sample/APRE/mt2.sc 
Result of Execution#
$ is -f schema/schema.sql
$ make mt2
$ ./mt2
<MULTI THREADS 2>
-----------------------------------------------------------
ORDER_DATE              ENO   GNO                                 
-----------------------------------------------------------
2000/11/29 00:00:00     12    A111100002
2000/11/29 00:00:00     12    E111100001
2000/11/29 00:00:00     19    E111100001
2000/12/10 00:00:00     19    D111100008
2000/12/01 00:00:00     19    D111100004
2000/12/29 00:00:00     12    C111100001
2000/12/29 00:00:00     20    E111100002
2000/12/30 00:00:00     20    D111100002
2000/12/30 00:00:00     19    D111100008
2000/12/30 00:00:00     20    A111100002
2000/12/30 00:00:00     12    D111100002
2000/12/30 00:00:00     20    D111100011
2000/12/30 00:00:00     20    D111100003
2000/12/30 00:00:00     19    D111100010
2000/12/30 00:00:00     20    C111100001
2000/12/30 00:00:00     12    E111100012
2000/12/30 00:00:00     20    C111100001
2000/12/30 00:00:00     12    F111100001