Multithreaded application

Hi all,
I know that it is a fairly complicated matter and I have read almost all online sources on the question
but still may be I am missing something. I have written an application for a digital camera which
also supports continuous shooting mode(20-30 or more pictures taken very fast after one another).
I decided to handle the image transfer with a multithread approach. The beta version of my
application has been tested for a couple of months and there are many complains that the
application crashes or is stuck at some point, this happens only when images are being transfered in
the camera's continuous mode. Below is the piece of code where the threads are being created and
unleashed to do the work(for the most part I am building the future image files' names and acquiring
the information from the camera which is in another class(CameraAccess)). There are also few calls
to my main thread where the GUI should be updated.
-(void) transferRequest
//Should I use a second run loop, the apple documentation diverges here! Answer: No.
self setReleaseInProgress:YES;<br/> NSThread* currThread = [[NSThread alloc] initWithTarget:self selector: @selector(runTransfer:) object:nil];<br/> self setTransferThread: currThread;<br/> ;<br/> //[currThread release];<br/>}<br/><br/>-(void) runTransfer: (id) sender<br/>{<br/> NSRecursiveLock* theLock = [[NSLock alloc] init];<br/> ;<br/> <br/> NSAutoreleasePool pool = [[NSAutoreleasePool alloc] init];<br/> NSString folderPath = [[[NSString alloc] init] autorelease]; <br/> NSString* fileName = [[[NSString alloc] init] autorelease]; <br/> NSString* saveFileName = [[[NSString alloc] init] autorelease]; <br/> <br/> IOPMAssertionID assertionID;<br/> IOReturn success = IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, &amp;assertionID); <br/> if (success == kIOReturnSuccess)<br/> {<br/> // The lines beneath assure that the system will not go to sleep during the transfer <br/> // kIOPMAssertionTypeNoDisplaySleep prevents display sleep,<br/> // kIOPMAssertionTypeNoIdleSleep prevents idle sleep<br/> numOfActiveThreads++;<br/> <br/> if(sendContinuousReleaseEnd)<br/> {<br/> if(self releaseCam:13)<br/> {<br/> NSNumber* boolean0 = NSNumber numberWithBool: FALSE;<br/> self performSelectorOnMainThread: @selector(setPropertySendContinuousReleaseEnd:) withObject: boolean0 waitUntilDone: YES;<br/> }<br/> }<br/> NSNumber* boolean = NSNumber numberWithBool:TRUE;<br/> self performSelectorOnMainThread: @selector(setPropertyIsTransferInProgress:) withObject: boolean waitUntilDone: YES;<br/> <br/> UInt8* resultPtr = NULL; <br/> resultPtr = malloc(156); <br/> [[self camAccess] copyObjectPropertyDictionary: resultPtr];<br/> UInt32 numPics = 0;<br/> UInt32 tmp3 = 0;<br/> UInt32 tmp4 = 0;<br/> tmp3 = resultPtr[48];<br/> tmp4 = resultPtr[49];<br/> if(tmp3 &gt; 0 || tmp4 &gt; 0)<br/> {<br/> numPics = tmp3 | (tmp4&lt;&lt;8);<br/> tmp3 = resultPtr[50];<br/> numPics = numPics | (tmp3&lt;&lt;16);<br/> tmp3 = resultPtr[51];<br/> numPics = numPics | (tmp3&lt;&lt;24);<br/> }<br/> NSNumber* integer00 = NSNumber numberWithInt: numPics;<br/> self performSelectorOnMainThread: @selector(setPropertyPicsInQueue:) withObject: integer00 waitUntilDone: YES;<br/> <br/> if(picsInQueue == 1)<br/> {<br/> NSNumber* integer0 = NSNumber numberWithInt: 1;<br/> self performSelectorOnMainThread: @selector(setPropertyNumOfPicsToSend:) withObject: integer0 waitUntilDone: YES;<br/> }<br/> else<br/> {<br/> if(numOfPicsToSend &gt;= picsInQueue)<br/> {<br/> <br/> }<br/> else<br/> {<br/> NSNumber* integer = NSNumber numberWithInt: picsInQueue;<br/> self performSelectorOnMainThread: @selector(setPropertyNumOfPicsToSend:) withObject: integer waitUntilDone: YES;<br/> }<br/> }<br/> <br/> if( == nil || [[folderSettings pictureNewFileName] isEqual:@""])<br/> {<br/> NSString* string = [[[NSString alloc] initWithString: @"Picture"] autorelease];<br/> self performSelectorOnMainThread: @selector(setFolderSettingsPictureNewFileName:) withObject: string waitUntilDone: YES;<br/> }<br/><br/> /file name and extension/<br/> NSData* info = [[[NSData alloc] initWithBytes: resultPtr length:156] autorelease]; <br/> NSRange range;<br/> range.length = 27;<br/> range.location = 53;<br/> NSData* subDataInfo = info subdataWithRange: range;//alloc?! autorelease?!<br/> NSString* fileNameFromBytes = [[[NSString alloc] initWithBytes: length: 27 encoding: NSUTF16LittleEndianStringEncoding] autorelease]; length: 27 encoding: NSUTF16LittleEndianStringEncoding];<br/> //Case differentiation, is the picture DNG or JPG<br/> NSArray* listItems = [[[NSArray alloc] init] autorelease]; <br/> listItems = [fileNameFromBytes componentsSeparatedByString:@"."];<br/> NSString* fExtension = [[[NSString alloc] initWithString:[listItems objectAtIndex:[listItems count] - 1]] autorelease]; <br/> if([fExtension caseInsensitiveCompare: @"DNG

artOf... wrote:
The main goal is to leave the GUI active(to some extend) during this image transfer(this is also
done and working) - answer to your question #1, may be I do not specifically need multithreading
may be what you mentioned below 'producer/consumer model' is the way to go. I simply do not
have any idea how to implement it.
My first suggestion is to change your code a bit so that you can easily turn the multithreading on or off, just so you can play around with some non-multithreading options. Basically, that just means adding some code where you call the thread main method directly instead of via NSThread. Then you can comment out one way or the other to play around.
Producer/consumer (and and dozen other names it may be called) is one of the easiest ways to implement multithreading. Actually, it has nothing to do with threading. It is just an architecture to use a queue of object to be worked. There is some producer that adds objects to the queue and a consumer that removes them, and works on them. You can run your consumer in a separate thread and the producer will never get backed up or have to wait on it. The only locking involved is to ensure mutual exclusion when adding to or removing from the queue.
To answer your question #2, there is no specific function call
that is slow or blocking, the application simply crashes at some point(memory generated crashes
not on one place in the code every time, but 3-4 different errors which i do not grasp).
But it is crashing due to the thread problems. What happens if you don't have any multithreading? Then were is the slowness and/or blocking?
Question #3 - I have read about synchronous and asynchronous function calls but that is also something that I never
used before(may be to receive my events from the camera, but this was a ready example and I
used it directly.)
The problem is that most hardware interaction is designed to use asynchronous operations. Otherwise, the OS locks up due to hardware and really bad things start to happen. See MacOS 7 for details. Unfortunately, many hardware interaction examples use synchronous operations because they are easier to show and understand.
Q. #4 - the recursive lock was just a test, normally I have just NSLock at the same
point(the reason for this test was that when I was using NSLock, rarely but still, I receive the crash error:
* -[NSRecursiveLock finalize]: lock (<NSRecursiveLock: 0x1267610> '(null)') finalized while still in use).
That lock, recursive or not, is not a good one. A producer/consumer model would eliminate that lock because you would only ever have a single worker thread.
I just thought that those wrapper
methods are needed since I thought I can't omit the performSelector methods and those need @selector:
so I just invented the approach to communicate with my camera object (refered to by self in the code since
the code is in the Camera.m class). Also I make some calls to a class CameraAccess.m which is my direct
communication with the camera, should I put those in performSelector as well or should I just remove all
occurances of performSelector if possible?
The only time you really need to perform a selector on the main thread is when you need to update the interface. If you are talking with hardware using APIs that may not be reentrant, you just need to make sure you aren't doing that from multiple threads. You don't necessarily have to do it from the main thread.
As I said, the application is working, the images are transfered,
just in rare occasions there are crashes which is bad enough but still...
With threads, getting it working 99% of the time may mean you have done it horribly wrong. That 1% of the time it crashes is often with hardware, which may leave it in a corrupt state.
I am also interested in the 'producer/consumer model' do you have a 'simple' example of an interaction of
such a thread approach?
It is basically just a queue. Create a wrapper to add to or remove from the queue that has some sort of locking mechanism. The built-in @synchronized directive is very handy for that. I may have posted a simple example here a couple of years ago. Do a search for my posts with the word "multithreaded" or something.

Similar Messages

  • Pro*c multithreaded application has memory leak

    Hi there,
    I posted this message a week ago in OCI section, nobody answer me.
    I am really curious if my application has a bug or the pro*c has a bug.
    Anyone can compile the sample code and test it easily.
    I made multithreaded application which queries dynamic SQL, it works.
    But the memory leaks when i query the SQL statement.
    The more memory leaks, the more i query the SQL statement, even same SQL
    statement.
    I check it with top, shell command.
    My machine is SUN E450, Solaris 8. Oracle 9.2.0.1
    Compiler : gcc (GCC) 3.2.2
    I changed source code which is from
    $(ORACLE_HOME)/precomp/demo/proc/sample10.pc
    the sample10 doesn't need to be multithreaded. But i think it has to work
    correctly if i changed it to multithreaded application.
    the make file and source code will be placed below.
    I have to figure out the problem.
    Please help
    Thanks in advance,
    the make file is below
    HOME = /user/jkku
    ORA = $(ORACLE_HOME)
    CC = gcc
    PROC = proc
    LC_INCL = -I$(HOME)/work/dbmss/libs/include
    lc_incl = include=$(HOME)/work/dbmss/libs/include
    SYS_INCL =
    sys_incl =
    ORA_INCL = -I. \
    -I$(ORA)/precomp/public \
    -I$(ORA)/rdbms/public \
    -I$(ORA)/rdbms/demo \
    -I$(ORA)/rdbms/pbsql/public \
    -I$(ORA)/network/public \
    -DSLMXMX_ENABLE -DSLTS_ENABLE -D_SVID_GETTOD
    INCLUDES = $(LC_INCL) $(SYS_INCL) $(ORA_INCL)
    includes = $(lc_incl) $(sys_incl)
    LC_LIBS =
    SYS_LIBS = -lpthread -lsocket -lnsl -lrt
    ORA_LIBS = -L$(ORA)/lib/ -lclntsh
    LIBS = $(LC_LIBS) $(SYS_LIBS) $(ORA_LIBS)
    # Define C Compiler flags
    CFLAGS += -D_Solaris64_ -m64
    CFLAGS += -g -D_REENTRANT
    # Define pro*c Compiler flags
    PROCFLAGS += THREADS=YES
    PROCFLAGS += CPOOL=YES
    # Our object files
    PRECOMPS = sample10.c
    OBJS = sample10.o
    .SUFFIXES: .o .c .pc
    .c.o:
    $(CC) -c $(CFLAGS) $(INCLUDES) $*.c
    .pc.c:
    $(PROC) $(PROCFLAGS) $(includes) $*.pc $*.c
    all: sample10
    sample10: $(PRECOMPS) $(OBJS)
    $(CC) $(CFLAGS) -o sample10 $(OBJS) $(LIBS)
    clean:
    rm -rf *.o sample10 sample10.c
    the source code is below which i changed the oracle sample10.pc to
    multithreaded application.
    Sample Program 10: Dynamic SQL Method 4
    This program connects you to ORACLE using your username and
    password, then prompts you for a SQL statement. You can enter
    any legal SQL statement. Use regular SQL syntax, not embedded SQL.
    Your statement will be processed. If it is a query, the rows
    fetched are displayed.
    You can enter multi-line statements. The limit is 1023 characters.
    This sample program only processes up to MAX_ITEMS bind variables and
    MAX_ITEMS select-list items. MAX_ITEMS is #defined to be 40.
    #include <stdio.h>
    #include <string.h>
    #include <setjmp.h>
    #include <sqlda.h>
    #include <stdlib.h>
    #include <sqlcpr.h>
    /* Maximum number of select-list items or bind variables. */
    #define MAX_ITEMS 40
    /* Maximum lengths of the names of the
    select-list items or indicator variables. */
    #define MAX_VNAME_LEN 30
    #define MAX_INAME_LEN 30
    #ifndef NULL
    #define NULL 0
    #endif
    /* Prototypes */
    #if defined(__STDC__)
    void sql_error(void);
    int oracle_connect(void);
    int alloc_descriptors(int, int, int);
    int get_dyn_statement(void);
    void set_bind_variables(void);
    void process_select_list(void);
    void help(void);
    #else
    void sql_error(/*_ void _*/);
    int oracle_connect(/*_ void _*/);
    int alloc_descriptors(/*_ int, int, int _*/);
    int get_dyn_statement(/* void _*/);
    void set_bind_variables(/*_ void -*/);
    void process_select_list(/*_ void _*/);
    void help(/*_ void _*/);
    #endif
    char *dml_commands[] = {"SELECT", "select", "INSERT", "insert",
    "UPDATE", "update", "DELETE", "delete"};
    EXEC SQL INCLUDE sqlda;
    EXEC SQL INCLUDE sqlca;
    EXEC SQL BEGIN DECLARE SECTION;
    char dyn_statement[1024];
    EXEC SQL VAR dyn_statement IS STRING(1024);
    EXEC SQL END DECLARE SECTION;
    EXEC ORACLE OPTION (ORACA=YES);
    EXEC ORACLE OPTION (RELEASE_CURSOR=YES);
    SQLDA *bind_dp;
    SQLDA *select_dp;
    /* Define a buffer to hold longjmp state info. */
    jmp_buf jmp_continue;
    char *db_uid="dbmuser/dbmuser@dbmdb";
    sql_context ctx;
    int err_sql;
    enum{
    SQL_SUCC=0,
    SQL_ERR,
    SQL_NOTFOUND,
    SQL_UNIQUE,
    SQL_DISCONNECT,
    SQL_NOTNULL
    int main()
    int i;
    EXEC SQL ENABLE THREADS;
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    EXEC SQL WHENEVER NOT FOUND DO sql_not_found();
    /* Connect to the database. */
    if (connect_database() < 0)
    exit(1);
    EXEC SQL CONTEXT USE :ctx;
    /* Process SQL statements. */
    for (;;)
    /* Allocate memory for the select and bind descriptors. */
    if (alloc_descriptors(MAX_ITEMS, MAX_VNAME_LEN, NAME_LEN) != 0)
    exit(1);
    (void) setjmp(jmp_continue);
    /* Get the statement. Break on "exit". */
    if (get_dyn_statement() != 0)
    break;
    EXEC SQL PREPARE S FROM :dyn_statement;
    EXEC SQL DECLARE C CURSOR FOR S;
    /* Set the bind variables for any placeholders in the
    SQL statement. */
    set_bind_variables();
    /* Open the cursor and execute the statement.
    * If the statement is not a query (SELECT), the
    * statement processing is completed after the
    * OPEN.
    EXEC SQL OPEN C USING DESCRIPTOR bind_dp;
    /* Call the function that processes the select-list.
    * If the statement is not a query, this function
    * just returns, doing nothing.
    process_select_list();
    /* Tell user how many rows processed. */
    for (i = 0; i < 8; i++)
    if (strncmp(dyn_statement, dml_commands, 6) == 0)
    printf("\n\n%d row%c processed.\n", sqlca.sqlerrd[2], sqlca.sqlerrd[2] == 1 ? '\0' : 's');
    break;
    /* Close the cursor. */
    EXEC SQL CLOSE C;
    /* When done, free the memory allocated for pointers in the bind and
    select descriptors. */
    for (i = 0; i < MAX_ITEMS; i++)
    if (bind_dp->V != (char *) 0)
    free(bind_dp->V);
    free(bind_dp->I); /* MAX_ITEMS were allocated. */
    if (select_dp->V != (char *) 0)
    free(select_dp->V);
    free(select_dp->I); /* MAX_ITEMS were allocated. */
    /* Free space used by the descriptors themselves. */
    SQLSQLDAFree(ctx, bind_dp);
    SQLSQLDAFree(ctx, select_dp);
    } /* end of for(;;) statement-processing loop */
    disconnect_database();
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    EXEC SQL COMMIT WORK RELEASE;
    puts("\nHave a good day!\n");
    return;
    * Allocate the BIND and SELECT descriptors using sqlald().
    * Also allocate the pointers to indicator variables
    * in each descriptor. The pointers to the actual bind
    * variables and the select-list items are realloc'ed in
    * the set_bind_variables() or process_select_list()
    * routines. This routine allocates 1 byte for select_dp->V
    * and bind_dp->V, so the realloc will work correctly.
    alloc_descriptors(size, max_vname_len, max_iname_len)
    int size;
    int max_vname_len;
    int max_iname_len;
    int i;
    * The first sqlald parameter determines the maximum number of
    * array elements in each variable in the descriptor. In
    * other words, it determines the maximum number of bind
    * variables or select-list items in the SQL statement.
    * The second parameter determines the maximum length of
    * strings used to hold the names of select-list items
    * or placeholders. The maximum length of column
    * names in ORACLE is 30, but you can allocate more or less
    * as needed.
    * The third parameter determines the maximum length of
    * strings used to hold the names of any indicator
    * variables. To follow ORACLE standards, the maximum
    * length of these should be 30. But, you can allocate
    * more or less as needed.
    if ((bind_dp =
    SQLSQLDAAlloc(ctx, size, max_vname_len, max_iname_len)) ==
    (SQLDA *) 0)
    fprintf(stderr,
    "Cannot allocate memory for bind descriptor.");
    return -1; /* Have to exit in this case. */
    if ((select_dp =
    SQLSQLDAAlloc(ctx, size, max_vname_len, max_iname_len)) == (SQLDA *)
    0)
    fprintf(stderr,
    "Cannot allocate memory for select descriptor.");
    return -1;
    select_dp->N = MAX_ITEMS;
    /* Allocate the pointers to the indicator variables, and the
    actual data. */
    for (i = 0; i < MAX_ITEMS; i++) {
    bind_dp->I = (short *) malloc(sizeof (short));
    select_dp->I = (short *) malloc(sizeof(short));
    bind_dp->V = (char *) malloc(1);
    select_dp->V = (char *) malloc(1);
    return 0;
    int get_dyn_statement()
    char *cp, linebuf[256];
    int iter, plsql;
    for (plsql = 0, iter = 1; ;)
    if (iter == 1)
    printf("\nSQL> ");
    dyn_statement[0] = '\0';
    fgets(linebuf, sizeof linebuf, stdin);
    cp = strrchr(linebuf, '\n');
    if (cp && cp != linebuf)
    *cp = ' ';
    else if (cp == linebuf)
    continue;
    if ((strncmp(linebuf, "EXIT", 4) == 0) ||
    (strncmp(linebuf, "exit", 4) == 0))
    return -1;
    else if (linebuf[0] == '?' ||
    (strncmp(linebuf, "HELP", 4) == 0) ||
    (strncmp(linebuf, "help", 4) == 0))
    help();
    iter = 1;
    continue;
    if (strstr(linebuf, "BEGIN") ||
    (strstr(linebuf, "begin")))
    plsql = 1;
    strcat(dyn_statement, linebuf);
    if ((plsql && (cp = strrchr(dyn_statement, '/'))) ||
    (!plsql && (cp = strrchr(dyn_statement, ';'))))
    *cp = '\0';
    break;
    else
    iter++;
    printf("%3d ", iter);
    return 0;
    void set_bind_variables()
    int i, n;
    char bind_var[64];
    /* Describe any bind variables (input host variables) */
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    bind_dp->N = MAX_ITEMS; /* Initialize count of array elements. */
    EXEC SQL DESCRIBE BIND VARIABLES FOR S INTO bind_dp;
    /* If F is negative, there were more bind variables
    than originally allocated by sqlald(). */
    if (bind_dp->F < 0)
    printf ("\nToo many bind variables (%d), maximum is %d\n.",
    -bind_dp->F, MAX_ITEMS);
    return;
    /* Set the maximum number of array elements in the
    descriptor to the number found. */
    bind_dp->N = bind_dp->F;
    /* Get the value of each bind variable as a
    * character string.
    * C contains the length of the bind variable
    * name used in the SQL statement.
    * S contains the actual name of the bind variable
    * used in the SQL statement.
    * L will contain the length of the data value
    * entered.
    * V will contain the address of the data value
    * entered.
    * T is always set to 1 because in this sample program
    * data values for all bind variables are entered
    * as character strings.
    * ORACLE converts to the table value from CHAR.
    * I will point to the indicator value, which is
    * set to -1 when the bind variable value is "null".
    for (i = 0; i < bind_dp->F; i++)
    printf ("\nEnter value for bind variable %.*s: ",
    (int)bind_dp->C, bind_dp->S);
    fgets(bind_var, sizeof bind_var, stdin);
    /* Get length and remove the new line character. */
    n = strlen(bind_var) - 1;
    /* Set it in the descriptor. */
    bind_dp->L = n;
    /* (re-)allocate the buffer for the value.
    sqlald() reserves a pointer location for
    V but does not allocate the full space for
    the pointer. */
    bind_dp->V = (char *) realloc(bind_dp->V, (bind_dp->L + 1));
    /* And copy it in. */
    strncpy(bind_dp->V, bind_var, n);
    /* Set the indicator variable's value. */
    if ((strncmp(bind_dp->V, "NULL", 4) == 0) ||
    (strncmp(bind_dp->V, "null", 4) == 0))
    *bind_dp->I = -1;
    else
    *bind_dp->I = 0;
    /* Set the bind datatype to 1 for CHAR. */
    bind_dp->T = 1;
    return;
    void process_select_list()
    int i, null_ok, precision, scale;
    if ((strncmp(dyn_statement, "SELECT", 6) != 0) &&
    (strncmp(dyn_statement, "select", 6) != 0))
    select_dp->F = 0;
    return;
    /* If the SQL statement is a SELECT, describe the
    select-list items. The DESCRIBE function returns
    their names, datatypes, lengths (including precision
    and scale), and NULL/NOT NULL statuses. */
    select_dp->N = MAX_ITEMS;
    EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;
    /* If F is negative, there were more select-list
    items than originally allocated by sqlald(). */
    if (select_dp->F < 0)
    printf ("\nToo many select-list items (%d), maximum is %d\n",
    -(select_dp->F), MAX_ITEMS);
    return;
    /* Set the maximum number of array elements in the
    descriptor to the number found. */
    select_dp->N = select_dp->F;
    /* Allocate storage for each select-list item.
    sqlprc() is used to extract precision and scale
    from the length (select_dp->L).
    sqlnul() is used to reset the high-order bit of
    the datatype and to check whether the column
    is NOT NULL.
    CHAR datatypes have length, but zero precision and
    scale. The length is defined at CREATE time.
    NUMBER datatypes have precision and scale only if
    defined at CREATE time. If the column
    definition was just NUMBER, the precision
    and scale are zero, and you must allocate
    the required maximum length.
    DATE datatypes return a length of 7 if the default
    format is used. This should be increased to
    9 to store the actual date character string.
    If you use the TO_CHAR function, the maximum
    length could be 75, but will probably be less
    (you can see the effects of this in SQL*Plus).
    ROWID datatype always returns a fixed length of 18 if
    coerced to CHAR.
    LONG and
    LONG RAW datatypes return a length of 0 (zero),
    so you need to set a maximum. In this example,
    it is 240 characters.
    printf ("\n");
    for (i = 0; i < select_dp->F; i++)
    char title[MAX_VNAME_LEN];
    /* Turn off high-order bit of datatype (in this example,
    it does not matter if the column is NOT NULL). */
    sqlnul ((unsigned short *)&(select_dp->T), (unsigned short
    *)&(select_dp->T), &null_ok);
    switch (select_dp->T)
    case 1 : /* CHAR datatype: no change in length
    needed, except possibly for TO_CHAR
    conversions (not handled here). */
    break;
    case 2 : /* NUMBER datatype: use sqlprc() to
    extract precision and scale. */
    sqlprc ((unsigned int *)&(select_dp->L), &precision,
    &scale);
    /* Allow for maximum size of NUMBER. */
    if (precision == 0) precision = 40;
    /* Also allow for decimal point and
    possible sign. */
    /* convert NUMBER datatype to FLOAT if scale > 0,
    INT otherwise. */
    if (scale > 0)
    select_dp->L = sizeof(float);
    else
    select_dp->L = sizeof(int);
    break;
    case 8 : /* LONG datatype */
    select_dp->L = 240;
    break;
    case 11 : /* ROWID datatype */
    case 104 : /* Universal ROWID datatype */
    select_dp->L = 18;
    break;
    case 12 : /* DATE datatype */
    select_dp->L = 9;
    break;
    case 23 : /* RAW datatype */
    break;
    case 24 : /* LONG RAW datatype */
    select_dp->L = 240;
    break;
    /* Allocate space for the select-list data values.
    sqlald() reserves a pointer location for
    V but does not allocate the full space for
    the pointer. */
    if (select_dp->T != 2)
    select_dp->V = (char *) realloc(select_dp->V,
    select_dp->L + 1);
    else
    select_dp->V = (char *) realloc(select_dp->V,
    select_dp->L);
    /* Print column headings, right-justifying number
    column headings. */
    /* Copy to temporary buffer in case name is null-terminated */
    memset(title, ' ', MAX_VNAME_LEN);
    strncpy(title, select_dp->S, select_dp->C);
    if (select_dp->T == 2)
    if (scale > 0)
    printf ("%.*s ", select_dp->L+3, title);
    else
    printf ("%.*s ", select_dp->L, title);
    else
    printf("%-.*s ", select_dp->L, title);
    /* Coerce ALL datatypes except for LONG RAW and NUMBER to
    character. */
    if (select_dp->T != 24 && select_dp->T != 2)
    select_dp->T = 1;
    /* Coerce the datatypes of NUMBERs to float or int depending on
    the scale. */
    if (select_dp->T == 2)
    if (scale > 0)
    select_dp->T = 4; /* float */
    else
    select_dp->T = 3; /* int */
    printf ("\n\n");
    /* FETCH each row selected and print the column values. */
    EXEC SQL WHENEVER NOT FOUND GOTO end_select_loop;
    for (;;)
    EXEC SQL FETCH C USING DESCRIPTOR select_dp;
    /* Since each variable returned has been coerced to a
    character string, int, or float very little processing
    is required here. This routine just prints out the
    values on the terminal. */
    for (i = 0; i < select_dp->F; i++)
    if (*select_dp->I < 0)
    if (select_dp->T == 4)
    printf ("%-*c ",(int)select_dp->L+3, ' ');
    else
    printf ("%-*c ",(int)select_dp->L, ' ');
    else
    if (select_dp->T == 3) /* int datatype */
    printf ("%*d ", (int)select_dp->L,
    *(int *)select_dp->V);
    else if (select_dp->T == 4) /* float datatype */
    printf ("%*.2f ", (int)select_dp->L,
    *(float *)select_dp->V);
    else /* character string */
    printf ("%-*.*s ", (int)select_dp->L,
    (int)select_dp->L, select_dp->V);
    printf ("\n");
    end_select_loop:
    return;
    void help()
    puts("\n\nEnter a SQL statement or a PL/SQL block at the SQL> prompt.");
    puts("Statements can be continued over several lines, except");
    puts("within string literals.");
    puts("Terminate a SQL statement with a semicolon.");
    puts("Terminate a PL/SQL block (which can contain embedded
    semicolons)");
    puts("with a slash (/).");
    puts("Typing \"exit\" (no semicolon needed) exits the program.");
    puts("You typed \"?\" or \"help\" to get this message.\n\n");
    int connect_database()
    err_sql = SQL_SUCC;
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    EXEC SQL WHENEVER NOT FOUND DO sql_not_found();
    EXEC SQL CONTEXT ALLOCATE :ctx;
    EXEC SQL CONTEXT USE :ctx;
    EXEC SQL CONNECT :db_uid;
    if(err_sql != SQL_SUCC){
    printf("err => connect database(ctx:%ld, uid:%s) failed!\n", ctx, db_uid);
    return -1;
    return 1;
    int disconnect_database()
    err_sql = SQL_SUCC;
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    EXEC SQL WHENEVER NOT FOUND DO sql_not_found();
    EXEC SQL CONTEXT USE :ctx;
    EXEC SQL COMMIT WORK RELEASE;
    EXEC SQL CONTEXT FREE:ctx;
    return 1;
    void sql_error()
    printf("err => %.*s", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
    printf("in \"%.*s...\'\n", oraca.orastxt.orastxtl, oraca.orastxt.orastxtc);
    printf("on line %d of %.*s.\n\n", oraca.oraslnr, oraca.orasfnm.orasfnml,
    oraca.orasfnm.orasfnmc);
    switch(sqlca.sqlcode) {
    case -1: /* unique constraint violated */
    err_sql = SQL_UNIQUE;
    break;
    case -1012: /* not logged on */
    case -1089:
    case -3133:
    case -1041:
    case -3114:
    case -3113:
    /* �6�Ŭ�� shutdown�ǰų� �α��� ���°� �ƴҶ� ��b�� �õ� */
    /* immediate shutdown in progress - no operations are permitted */
    /* end-of-file on communication channel */
    /* internal error. hostdef extension doesn't exist */
    err_sql = SQL_DISCONNECT;
    break;
    case -1400:
    err_sql = SQL_NOTNULL;
    break;
    default:
    err_sql = SQL_ERR;
    break;
    EXEC SQL CONTEXT USE :ctx;
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    EXEC SQL ROLLBACK WORK;
    void sql_not_found()
    err_sql = SQL_NOTFOUND;

    Hi Jane,
    What version of Berkeley DB XML are you using?
    What is your operating system and your hardware platform?
    For how long have been the application running?
    What is your current container size?
    What's set for EnvironmentConfig.setThreaded?
    Do you know if containers have previously not been closed correctly?
    Can you please post the entire error output?
    What's the JDK version, 1.4 or 1.5?
    Thanks,
    Bogdan

  • How to configure ENV and DB for multithreaded application?

    Hi,
    From document, I know DB_THREAD must be checked for both ENV and DB, but , I don't know which one is best choice for multithreaded application while facing DB_INIT_LOCK and DB_INIT_CDB. In my application, there maybe multi readers and writers at the same time, should I use DB_INIT_LOCK instead of DB_INIT_CDB? what other flags should I use?
    DB_INIT_CDB provides multiple reader/single writer access while DB_INIT_LOCK should be used when multiple processes or threads are going to be reading and writing a Berkeley DB database.
    Thanks for your seggestions and answers.

    Thanks for the explanation,
    The Berkeley DB Concurrent Data Store product
    allows for multiple reader/single writer access
    to a database. This means that at any point in time,
    there may be either multiple readers accessing a
    database or a single writer updating the database.
    Berkeley DB Concurrent Data Store is intended for
    applications that need support for concurrent updates
    to a database that is largely used for reading.
    If you are looking to support multiple readers and
    multiple writers then take a look at the Transactional
    Data Store product
    (http://download.oracle.com/docs/cd/E17076_02/html/programmer_reference/transapp.html)
    In this case the Environment is typically opened with:
    DB_INIT_MPOOL, DB_INIT_LOCK, DB_INIT_LOG, and DB_INIT_TXN.
    Let me know if I missed any of your question.
    Thanks,
    Sandra

  • Using time() function in multithreaded application

    I am using time() function in a multithreaded application with NULL argument for getting current time.
    Some time it's observed that we get a time one minute earlier than current time (3600 seconds).
    Is there a problem in the usage of the function?
    I am using expression : currenttime = time(NULL);
    I had seen some people using following way - time(&currenttime );
    Will above two behaves differently in multithreaded environment?
    [I  am using  Sun C++ 5.5 compiler on Solaris 8]

    How do you compare actual time against the time seen by your threads? If your threads are printing the value from time(2) to stdout, it's possible that you're seeing an artifact of thread scheduling and/or output buffering.
    I really doubt that you have a concurrency problem, but anyway make sure that you include the -mt option on your compile line:
    CC -mt blahblahblah...

  • Dup2 after fork in multithreaded application

    I am working on a multithreaded application (C++) that must create several
    Java processes and capture their output though pipes.
    For a single threaded application, I know it is possible to call dup2 (in the child)
    after the fork to set up the proper connections.
    I am aware that it is not safe to make particular function calls between a fork
    and an exec. Is dup2 a call that is known to be safe?
    If not, is there a better alternative?
    Thanks.

    I'm using Oracle 10g2 on Windows Server 2003 on a Dell PowerEdge 1850 server.
    Both threads run the same code. Each thread performs multiple database queries though.
    This query is performed first:
    update my_table set my_data_processed='true' where my_data_processed='false' and rownum=1 returning my_index into :x
    Then the query with the getBlob is performed with <some_number> = my_index.
    Some background info: I have a lot of data in my_table that needs a very slow operation performed on the my_data blob in each row. I will eventually run this application on multiple servers in parallel and need a way to determine the next row that hasn't been processed yet then process it.
    The table is
    create table my_table (
    my_index int,
    my_data_processed varchar(10),
    my_data blob
    -Glen

  • How to Debug C++ Multithreaded Application in Solaris

    Hi All,
    I am working in Solaris Sparc 5.8 Machine. I need to debug Multithreaded C++ Application in Unix Environment.
    I am using dbx debugger.
    Please explain me how to debug multithreaded applications. if possible please explain me with example.
    Thanks in Advance.
    Thanks & Regards,
    Vasu

    1. Look over the dbx manual that comes with Sun Studio. Dbx includes many features for debugging MT code.
    2. If you have specific questions after reading the manual and trying out the features, ask them in the debugger forum:
    http://forum.sun.com/forum.jspa?forumID=257

  • PersistenceManager Patterns for Multithreaded Applications

    Hi,
    I'm looking for a good way, how to use Kodo JDO in a multithread application
    (actually Tomcat ;). The most easy thing is perhaps, to use only one
    persistence
    manager for the entire application, i.e. for all threads. Another solution
    might be,
    to create a PersistenceManager for each HTTP request. I don't like the
    solution,
    to have one PeristenceManager per session, because I expect thousands
    concurrent
    users. What I'm concerned with is the number of data base connections in
    this
    case.
    So my question to you is, what pattern do you use in your web applications?
    When do you create and close the PersistenceManagers to free resources?
    Has anybody alreay tried different patterns and compared the performance?
    Is there something like a best practice?
    Michael

    The other nice thing about the filter is that it should be easy to
    selectivly store and retrieve the PM in a user's session if they have a
    longer running transaction that spans multiple requests.
    So far, I've simply been passing the PM to each service method, although I
    don't particularliy like that approach. I haven't had time to try anything
    different yet and it isn't TOO bad, although I have a few ideas:
    1. The hashtable method you mentioned. I see there is a "Registry" pattern
    in the "Patterns of Enterprise Application Architecture" book I'm currently
    reading, but haven't gotten to it yet. I think it is close to what you
    mentioned.
    2. Retrieve the PM through JNDI. I don't know, however, if I can set up a
    PM in JNDI the way that the CurrentTransaction JNDI lookup works with
    container managed transactions.
    "Michael" <[email protected]> wrote in message
    news:[email protected]...
    I've never used a servlet filter before but I must say that is the most
    elegant approach I've seen, thanks for sharing that. How do you pass thePM
    to the service layer? Do you store it in a hashtable with the current
    thread as a key or do you pass it in to each service method?
    Thanks,
    Michael
    "Nathan Voxland" <[email protected]> wrote in message
    news:[email protected]...
    We've created the PersistenceManager in a filter and attached it to the
    request object. That way, the persistence manager can be accessed fromthe
    Struts Action, JSP Page, and anything else we do, plus we don't have to
    worry about closing it because it is closed by the filter after
    everything
    is done (Even if an exception is thrown)
    Here is the code:
    public void doFilter(final ServletRequest request, final ServletResponse
    response, FilterChain chain) throws IOException, ServletException {
    PersistenceManager pm = getPersistenceManager();
    request.setAttribute(JDO_PERSISTENCE_MANAGER, pm);
    try {
    chain.doFilter(request, response);
    } finally {
    if (pm.currentTransaction().isActive()) {
    pm.currentTransaction().rollback();
    pm.close();
    "Michael" <[email protected]> wrote in message
    news:[email protected]...
    I currently doing exactly this: getting a persistence manager at the
    beginning of a jsp page and close it at the end of the jsp page.
    When you use <jsp:include ...>, you must be careful not to close the
    persistence manager to early. I do it like this:
    This sounds like a good approach, especially the hashtable with the
    thread
    as the key. The only issue I see is if you create the PM in the JSPpage,
    often an Action is executed before the JSP is called. This is why I
    was
    thinking of creating the PM in the struts request processor, it'salways
    called for every request.

  • Consuming a jms queue from a multithread application

    Hi
    Thank you for reading my post.
    I have an application which shuld consume message of a queue.
    my application is multithread and what i want to know is:
    - How i can ask queue to remove a message after my thread finished its work with the message, by this way i can ensure that if my thread face an exception after it read the message my message will not lose.
    - As it is multithread application, How i can ensure that a message is not delivered to two or more thread? is there any way to consume the messages in a synchronized way?
    Will synchronized access restrict the performance?
    Thanks

    This is running in a J2DK environment. For my thread pool, I just copied one from a Sun tutorial on multi-threading then modified it. I creaed a seperate package with three classes. Here is my thread pooling package. I basically use a Vector to hold threads when they are not working. And you can set a limit as to the max number of active threads.
    package threadpooling;
    * @author cferris
    *  This class is simple to wrap the runnable
    *  thread for the worker thread around other
    *  needed parameters like salon id and source.
    public class TaskWrapper {
      // Define the needed instance fields.
      public  Runnable taskAtHand = null;
      public  String taskType = "";
        // Overloaded constructor.
        public TaskWrapper(Runnable task) {
          // Just set the runnable
          taskAtHand = task;
          taskType = "WorkerThread";
        }  // of overloaded contructor
        // Overloaded constructor.
        public TaskWrapper(Runnable task, String taskType) {
          // Just set the instance fields
          taskAtHand = task;
          this.taskType = taskType;
        }  // of overloaded contructor
    package threadpooling;
    * @author cferris
    *  This class is for tracking the number of
    *  active threads so we don't go over the set
    *  limit. This class basically synchronizes
    *  the methods that update the active thread
    *  counter.
    import java.util.*;
    public class ThreadPool {
      // Define the instance fields.
      //   First define instance fields for the
      //   management of the thread pools - pm.
      protected Vector poolOfThreads = new Vector();
      private  static LinkedList taskQueue = new LinkedList();
      private  static int poolSize = 0;
      private  static boolean pmAvailable = true;
      private  static boolean daemon = false;
      public   static boolean shutDown = false;
      //   Instance field for the management of
      //   the threads - tm.
      private  static int activeThreads = 0;
      private  static int maxReached = 0;
      private  static int numTimesReached = 0;
      private  static boolean tmAvailable = true;
      // Define constants
      private  static final int MAX_LIMIT_DEFAULT = 32;
        // The constructor.
        public ThreadPool() {
          this(MAX_LIMIT_DEFAULT, false);
        }  // of constructor
        // Overload the constructor with max number of active jobs.
        public ThreadPool(int poolSize, boolean daemon) {
          this.poolSize = poolSize;
          this.daemon = daemon;
          poolOfThreads = new Vector();
          taskQueue = new LinkedList();
          salonTracker = new Vector();
          // Load up and initialize the threads.
          for (int i=0; i < poolSize; i++) {
              Thread thread = new WorkerThread(this, i);
              thread.setDaemon(daemon);
              thread.start();
              poolOfThreads.add(thread);
        }  // of overloaded constructor
        // Overload this method to allow just the runnable task.
        public void assignWork(Runnable work) {
          // Create the wrapper
          TaskWrapper wrapper = new TaskWrapper(work, "A task");
          assignWork(wrapper);
        }  // of overloaded assignWork
        // Push a unit of work (work thread) on the the worker thread.
        public synchronized void assignWork(TaskWrapper work) {
          // First increament the active thread count.
          increment();
          // Wait until we can access the linked list 
          try {
              if (!pmAvailable)
                  wait();
          } catch(InterruptedException e) { }
          pmAvailable = false;
          notifyAll();
          taskQueue.addLast(work);
          pmAvailable = true;
          notifyAll();
        }  // of assignWork
        // Get the next unit of work (work thread).
        public synchronized TaskWrapper getNextWorkAssignment()  {
          // Wait until we can access the linked list 
          try {
              if (!pmAvailable)
                  wait();
          } catch(InterruptedException e) { }
          pmAvailable = false;
          notifyAll();
          TaskWrapper nextTask;
          if (!taskQueue.isEmpty())
              nextTask = (TaskWrapper)taskQueue.removeFirst();
          else
              nextTask = null;
          pmAvailable = true;
          notifyAll();    
          return nextTask;
        }  // of getNextWorkAssignment
        // Return the number of tasks waiting on the work queue.
        public synchronized int workWaiting()  {
          // Wait until we can access the linked list 
          try {
              if (!pmAvailable)
                  wait();
          } catch(InterruptedException e) { }
          pmAvailable = false;
          notifyAll();
          int qSize = taskQueue.size();
          pmAvailable = true;
          notifyAll();    
          return qSize;
        }  // of workWaiting
        // Wait until all the tasks have been completed.
        public void waitForCompletion() {
          int prev = 0;
          // Continue checking untill all the threads have
          // completed and been removed.
          while(!poolOfThreads.isEmpty()) {
             // Lets record the difference
             if (poolOfThreads.size() != prev) {
                 System.out.println("waiting for " + poolOfThreads.size() + " to complete");
                 prev = poolOfThreads.size();
             // Loop through the thread pool and check each thread.         
             for (int t = 0; t < poolOfThreads.size(); t++)
                  if (!((WorkerThread)poolOfThreads.get(t)).working)
                      // This thead is done so remove it.
                      poolOfThreads.remove(t);
          }  // of while
        } // waitForCompletion
        // Set the max number of allow threads.
        public synchronized void setPoolSize(int newLimit) {
          // just set the limit.
          // Check if available
          while (!tmAvailable) {
             try {
               wait();
             catch(InterruptedException ie) {
               System.exit(5);
          // Ok, lock the object while we update it.
          tmAvailable = false;
          notifyAll();
          poolSize = newLimit;
          // Reset the max counters/trackers too
          maxReached = 0;
          numTimesReached = 0;
          // Create additional threads in the pool if needed
          if (poolSize > poolOfThreads.size())
              // Load up and initialize the threads.
              for (int i = poolOfThreads.size(); i < poolSize; i++) {
                  Thread thread = new WorkerThread(this, i);
                  thread.start();
                  poolOfThreads.add(thread);
          // Done, now release the object.
          tmAvailable = true;
          notifyAll();       
        }  // of setPoolSize.
        // The method to increment thread counter.
        private synchronized void increment() {
          // Check if available
          while (!tmAvailable) {
             try {
               wait();
             catch(InterruptedException ie) {
               // logger.fatal("Synchronized error in ThreadPool "); 
               // logger.fatal("Error message is: " + ie.getMessage());
               System.exit(5);
          // Ok, lock the object while we update it.
          tmAvailable = false;
          notifyAll();
          activeThreads++;
          // Check if this is the highest number of threads
          // that has been active, if not then set so.
          if (activeThreads > maxReached)
              maxReached = activeThreads;
          if (activeThreads == poolSize)
              numTimesReached++;
          // Done, now release the object.
          tmAvailable = true;
          notifyAll(); 
        }  // of increment
        // The method to increment thread counter.
        public synchronized void decrement() {
          // Check if available
          while (!tmAvailable) {
             try {
               wait();
             catch(InterruptedException ie) {
               // logger.fatal("Synchronized error in ThreadPool "); 
               // logger.fatal("Error message is: " + ie.getMessage());
               System.exit(5);
          // Ok, lock the object while we update it.
          tmAvailable = false;
          notifyAll();
          activeThreads--;
          // Done, now release the object.
          tmAvailable = true;
          notifyAll(); 
        }  // of decrement
        // A method to determine if the maximum number of active
        // threads has been reached.
        public synchronized boolean limitNotReached() {
          boolean limit = false;
          // Check if available
          while (!tmAvailable) {
             try {
               wait();
             catch(InterruptedException ie) {
               // logger.fatal("Synchronized error in ThreadPool."); 
               // logger.fatal("Error message is: " + ie.getMessage());
               System.exit(5);
          // Ok, lock the object while we update it.
          tmAvailable = false;
          notifyAll();
          if (activeThreads < poolSize)
             limit = true;
          // Done, now release the object.
          tmAvailable = true;
          notifyAll();
          return limit;
        }  // of limitNotReached
        // A method to return the number of active threads.
        public synchronized int currentNum() {
          // Check if available
          while (!tmAvailable) {
             try {
               wait();
             catch(InterruptedException ie) {
               // logger.fatal("Synchronized error in ThreadPool"); 
               // logger.fatal("Error message is: " + ie.getMessage());
               System.exit(5);
          // Ok, lock the object while we update it.
          tmAvailable = false;
          notifyAll();
          int numberOf = activeThreads;
          // Done, now release the object.
          tmAvailable = true;
          notifyAll();
          return numberOf;
        }  // of currentNum
    package threadpooling;
    * @author cferris
    *   This is the work thread that receives the
    *   taskes or unit of work that make up the
    *   thread pool.
    public class WorkerThread extends Thread {
      // Define need instance fields.
      public  boolean working = true;
      public  int id = 0;
      private int pauseFactor = 1;
      // The thread pool that this object belongs to.
      public ThreadPool owner;
      // Define the constructor for the seperate thread.
      public WorkerThread(ThreadPool pooler, int id)  {
        owner = pooler;
        this.id = id;
      }  // of contructor
      //  Wait for work or for system shutdown.
      public void run() {
        // Define the work object. 
        TaskWrapper unitOfWork = null;
        // Wait for work or until system is shutting down.
        do {
           unitOfWork = owner.getNextWorkAssignment();
           if (unitOfWork != null) {
               // First set name and then add salon to watch list.
               String taskName = unitOfWork.taskType + "-" + id;
               setName(taskName);
            // System.out.println("Starting thread - " + getName());
               unitOfWork.taskAtHand.run();
               // Done, now adjust tracker and reset the pause factor.
               owner.decrement();
               pauseFactor = 1;
           else {
               // No work so pause a bit
               try {
                   sleep(150 * pauseFactor);
               } catch (InterruptedException e) { }
               // If consecutively pausing because of no work then
               // increase the pause time up to a point.
               if (pauseFactor < 49)
                   pauseFactor++;
        } while (!owner.shutDown);
        // Set indicator this thread in the pool is done.
        working = false; 
      }  // of run
    }To use the thread pool just create a static instance of the ThreadPool in your main class
      private  static ThreadPool threadPool = new ThreadPool(getMaxThreads(), useDaemon());Then to launch a new thread use this.
        if (runOnSeperateThread()) {  
            WriteTransThread task = new WriteTransThread(messasge);
            // Add to the thread pool.
            threadPool.assignWork(new TaskWrapper(task, "my thread"));
      private boolean runOnSeperateThread() {
        // First check if mutli threading is turned on
        // and if there are open threads.
        if (multiThreadingOn && threadPool.limitNotReached())
            // Yes, there are open threads.
            return true;
        // Check if we can wait for an open thread.
        if (!multiThreadingOn || !waitForOpenThread)
            // No multi threading or waiting allowed.
            return false;
        // OK, wait for an open thread, wait for up
        // to 90 seconds.
        int j = 0;
        do {
            try {
                // Wait 1/2 a second.
                Thread.sleep(500);
            } catch(InterruptedException ignore) { };
            // Check again for an optn thread.
            if (threadPool.limitNotReached())
                // Ok to run on separate thread.
                return true;
            // Increament counter and wait again.
            j++;
        } while(j < 180);
        // If we got this far then we've waited long enough.
        logger.warn("Reached limit on waiting for an open thread in the pool");
        return false;
      }  // of runOnSeperateThreadThe WriteTransThread is a class that will extract the message body and write out its data to a table.
    Last, before you end your program make sure all the threads have competed by calling the following.
      //  If multi-threading is activated then this method will
      //  wait for them to complete before returning. The
      //  max wait time is 300 secs or 5 minutes.
      private void waitForThreadsToComplete(String message) {
        // First check for multi threading. 
        if (prop.useMultiThread() && (threadPool.currentNum() > 0)) {
            int limit = 0;       
            // Some threads are still active, wait
            logger.info("Waiting for threads to complete before " + message
                      + " current thread count is " + threadPool.currentNum());
            do {
               limit++;
               try {
                   Thread.sleep(500);  // Sleep 1/2 a sec.
               } catch(InterruptedException ie) { }  // Ignore the error for now.
               // Max waith is 300 sec or 5 min.
            } while ((threadPool.currentNum() > 0) && (limit < 600));
      }  // of waitForThreadsToComplete
     

  • I/O Exception in multithreading application

    I have a multithreading application that utilizes OracleDataSource with explicit caching enabled. The cache is sized to keep as little opened connections as possible. It works fine but under heavy load I'm getting
    I/O Exception: The Network Adapter could not establish the connection(SQL Code: 17002, SQL State: + 08006) (java.sql.SQLRecoverableException)
    oracle.jdbc.driver.SQLStateMapping:101 (null)
    I suppose that this is due to numerous concurrent calls to Oracle listener which the last is unable to process. The database itself is OK as when opening the same number (and even times more) of connections sequentially we get no errors.
    Is it possible to slow down OracleDataSource somehow to dose new physical connection requests?
    ConnectionWaitTimeout is not an option as it dilays ALL connection requests thus after this timeout listener accepts that calls again with the same rate.
    Actually I'm ready to give up some speed to get better reliability.

    It means the file is not there. Check to make sure the file is in the specified location, if it is, check the spelling.

  • Multithread application is crashing random in malloc, free, new or delete

    Hi Gurus,
    Operating System:
    SunOS 5.8 Generic_108528-04 sun4u sparc SUNW,Ultra-80
    C++:
    WorkShop Compilers 5.0 98/12/15 C++ 5.0
    I'm a multithread application using "pthread" and compiled with "-mt -xtarget=ultra -xarch=v9".
    My problem is that the server is crashing in differents points (random).
    The server can be running without problems, but other times the server crashs immediately.
    Always, the server is crashing in the functions "new", "delete", "malloc" or "free".
    Normally two functions appears in the top of the stack:
    realfree and mallocunlocked with SIGBUS or SIGSEGV.
    So, I don't know if I need to install a pacth or is a problem in my server. The problem is that the server can run "n" times correctly and other times crashs with SIGBUS or SIGSEGV in the above functions doing exactly the same.
    Could you help me ? Any idea ?
    Do you need the stack ?
    Thanks and Merry Christmas,
    Andres

    Hi Susan,
    Here is the threads and stack with the rtc_error_stack on:
    (/opt/SUNWspro/bin/../WS5.0/bin/sparcv9/dbx) where
    current thread: t@23
    [1] realfree(0x100341190, 0xffffffff7c3bb608, 0x100341330, 0xffffffff7c3aeef8, 0x100341190, 0x63), at 0xffffffff7c249140
    [2] freeunlocked(0xffffffff7c3bb4f0, 0xffffffff7c3aeef8, 0x1003415a0, 0x1002a34c0, 0x1002a34b0, 0x0), at 0xffffffff7c249928
    [3] free(0x1003415a0, 0x1001d9e10, 0x1, 0xffffffff7c3aeef8, 0x100252900, 0x31), at 0xffffffff7c24986c
    =>[4] JS_free(cx = 0x1002a2ff0, p = 0x1003415a0), line 1450 in "jsapi.c"
    [5] js_DestroyScope(cx = 0x1002a2ff0, scope = 0x1003415a0), line 475 in "jsscope.c"
    [6] js_DestroyObjectMap(cx = 0x1002a2ff0, map = 0x1003415a0), line 1554 in "jsobj.c"
    [7] js_DropObjectMap(cx = 0x1002a2ff0, map = 0x1003415a0, obj = 0x10033d9a0), line 1571 in "jsobj.c"
    [8] js_FinalizeObject(cx = 0x1002a2ff0, obj = 0x10033d9a0), line 1770 in "jsobj.c"
    [9] js_GC(cx = 0x1002a2ff0, gcflags = 0), line 1217 in "jsgc.c"
    [10] js_ForceGC(cx = 0x1002a2ff0), line 943 in "jsgc.c"
    [11] JS_GC(cx = 0x1002a2ff0), line 1641 in "jsapi.c"
    [12] DestroyJsEngine(jsRuntime = 0x1003358b0, jsContext = 0x1002a2ff0), line 149 in "jsengine.cpp"
    [13] JSWorkerThread(lpContextHandle = 0x1002c46a0), line 2329 in "pccwebserver.cpp"
    (/opt/SUNWspro/bin/../WS5.0/bin/sparcv9/dbx) threads
    t@1 a l@1 ?() sleep on 0x1001d78f0 in __lwp_sema_wait()
    t@2 b l@2 ?() running in __signotifywait()
    t@3 b l@3 ?() running in __lwp_sema_wait()
    t@4 ?() sleep on (unknown) in reapwait()
    t@5 a l@4 ArgusThread() running in soaccept()
    t@6 a l@5 PollThread() running in _poll()
    t@7 a l@6 PollThread() running in _poll()
    t@8 a l@7 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@9 a l@8 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@10 a l@9 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@11 a l@10 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@12 a l@11 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@13 a l@12 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@14 a l@13 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@15 a l@14 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@16 a l@15 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@17 a l@16 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@18 a l@18 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@19 a l@17 WorkerThread() sleep on 0x1002d8868 in __lwp_sema_wait()
    t@20 a l@19 ListenThread() running in soaccept()
    t@21 a l@20 ListenThread() running in soaccept()
    t@22 a l@21 ListenThread() running in soaccept()
    *> t@23 a l@22 JSWorkerThread() signal SIGBUS in realfree()
    t@24 a l@23 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@25 a l@24 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@25 a l@24 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@26 a l@26 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@27 a l@27 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@28 a l@25 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@29 a l@28 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@30 a l@29 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@31 a l@30 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@32 a l@31 JSWorkerThread() sleep on 0x1002c46e8 in __lwp_sema_wait()
    t@33 a l@32 UnixFtThread() running in soaccept()
    t@34 b l@34 cotimerset() running in __lwp_sema_wait()
    (/opt/SUNWspro/bin/../WS5.0/bin/sparcv9/dbx)
    Any idea ??
    Andres

  • MultiThreaded Application == multiProcessor Application

    Using XTools 2.2
    I wrote a multithreaded application with 4 threads that appears to run equally fast on a single processor Mac (PowerBook G4) and a multiprocessor Mac (G5 Quad). The only speed increase I see (2X) can be explained from the fact that the Quad has a faster clock than the PowerBook.
    Threads were created with Cocoa (NSThread). I can test that the threads are created and carry out their caclulations (calculations/thread are set for approx 30 seconds for the tests).
    I've checked the documentation from apple but haven't found any issue that seem appropriate. Is there a compiler flag?? Some obscure directive?? Help please....
    -thazlett

    Thank you for the replies.
    One of the primary issues pointed out by Apple in their documentation for multithreading applications is the difficulties in shared resources.
    I have split threads to work on separate, but identical, objects that each carry out a CPU intensive task. Each creates a large vector that will all eventually be summed. I have tried to comment out everything (like a counter) that might be shared, but no speed increases. I have instantiated the objects within the Interface builder (interface builder causing some invisible connection here?) and set their variables before splitting off separate threads for the individual computations. The variables names within the objects are identical, but are local (I assume so).
    I will continue to screen my code for something that is being shared since this is the most likely problem.

  • Is it supported to use Microsoft JDBC driver use in Java 7 (and 8) multithreaded applications?

    Hello everone
    is it supported to use Microsoft JDBC driver (the latest version) with Java 7 multithreaded application?
    I am planning to use standard Java 7 threads library and use separate JDBC objects per each thread, i.e. Java threads will not share any JDBC objects among them, only the thread-safe Java collections/data structures will be shared between threads (such as
    ConcurrentHashMap etc). The JDBC connections, resultsets, statements, etc will be created and dedicated per each individual thread.
    If it is supported - do you expect this design to scale-up well or am I better off using multiple but single-threaded Java/JBDC programs to access SQL Server 2012/2014 from Microsoft JDBC driver?
    Thanks
    Yuri Budilov
    Yuri Budilov Melbourne Australia

    >is it supported to use Microsoft JDBC driver (the latest version) with Java 7 multithreaded application?
    Yes, so long as:
    > Java threads will not share any JDBC objects among them,
    >do you expect this design to scale-up well or am I better off using multiple but single-threaded Java/JBDC programs >to access SQL Server 2012/2014 from Microsoft JDBC driver?
    Using threads should scale better than using processes (at least on Windows), as there is quite a bit of overhead with a process.  Each process has it's own JRE, it's own GC heap, its own threads...
    The bigger question, though, is how this scales on the SQL Server.  Your throughput may be limited by resources on your database server, and the thread's workloads may not be able to run concurrently because of locking. 
    David
    David http://blogs.msdn.com/b/dbrowne/

  • Log4j : How to avoid duplicate logmessages in a multithreaded application?

    Hi,
    I m using log4j for my heavily multithreaded JAVA application. After my application comes up, a few minutes later , I find many duplicate logger messages on the console(only logger messages in threads are being displayed 8-9 times each). Is it that my application being multithreaded or is there something wrong with my configuration file?
    My log4j-config.xml is as below:-
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
                         configDebug="true">
         <appender name="ASYNC" class="org.apache.log4j.AsyncAppender" additivity="false">
                 <appender-ref ref="TEMP"/>
              <appender-ref ref="CONSOLE"/>
              <appender-ref ref="TRACE"/>
              <appender-ref ref="DEBUG"/>
              <appender-ref ref="INFO"/>
              <appender-ref ref="WARN"/>
              <appender-ref ref="ERROR"/>
              <appender-ref ref="FATAL"/>
         </appender>
         <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
              <!-- param name="ImmediateFlush" value="true"/-->
              <layout class="org.apache.log4j.PatternLayout">
                     <!-- param name="ConversionPattern"
                                value="%d %-5p [%t] %C %A (%F:%L) - %m%n"/-->
              </layout>
         </appender>
         <appender name="TEMP" class="org.apache.log4j.FileAppender">
              <param name="File" value="/var/log/abs/acpu_fun/acpu.log"/>
              <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern"
                                value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
         </appender>
         <appender name="TRACE" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acputrc.log"/>
         <param name="Threshold" value="TRACE"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
         </appender>
         <appender name="DEBUG" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acpudeb.log"/>
         <param name="Threshold" value="DEBUG"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
         </appender>
         <appender name="INFO" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acpuinfo.log"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
              <filter class="org.apache.log4j.varia.LevelMatchFilter">
                          <param name="LevelToMatch" value="info"/>
                 </filter>
         <filter class="org.apache.log4j.varia.DenyAllFilter"/>     
         </appender>
         <appender name="WARN" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acpuwar.log"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
              <filter class="org.apache.log4j.varia.LevelMatchFilter">
                     <param name="LevelToMatch" value="warn"/>
                </filter>
         <filter class="org.apache.log4j.varia.DenyAllFilter"/>     
         </appender>
         <appender name="ERROR" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acpuerr.log"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
              <filter class="org.apache.log4j.varia.LevelMatchFilter">
                     <param name="LevelToMatch" value="error"/>
                </filter>
         <filter class="org.apache.log4j.varia.DenyAllFilter"/>     
         </appender>
         <appender name="FATAL" class="org.apache.log4j.FileAppender">
         <param name="File" value="/var/log/abs/acpu_fun/acpufatl.log"/>
         <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern"
                               value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
              <filter class="org.apache.log4j.varia.LevelMatchFilter">
                     <param name="LevelToMatch" value="fatal"/>
                </filter>
         <filter class="org.apache.log4j.varia.DenyAllFilter"/>     
         </appender>
         <appender name="SLCS" class="org.apache.log4j.FileAppender">
              <param name="File" value="/var/log/slcs/slcs.log"/>
              <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern"
                                value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
              </layout>
         </appender>
         <category name="com.aircell.abs.acpu.softwareloadconfig" additivity="false">
          <level value="debug"/>
         <appender-ref ref="SLCS"/>
          </category>
         <root>
              <priority value="info"/>
              <appender-ref ref="ASYNC"/>
         </root>
    </log4j:configuration>Regards.

    Hi,
    Did you find any solution for this problem. I am also encountering the same problem.
    Thanks,
    Mohit

  • Visibility of variables in a multithreaded application

    In a code review of our GUI application, we encountered some multithreading problems, concerning the visibility of variable values.
    In theory, write and read operations have to be synchronized or at least, volatile has to be used to provide a accurate access to the variable values from different threads.
    Although we have encountered many code sequences in our application violating the above rules, we have never seen a problem in our application.
    This forced us to write a simple test program which should discover these problems, but there was no difference using volatile or non volatile variables !!
    Our experience is, that on our platform (Intel Core2Duo/Pentium M - Linux 2.6.23 - Sun Java HotSpot 1.6.0_04), no visibility problems caused by multithread access occurs, even if the variables are neither declared as volatile nor protected by a synchronized block.
    Is there anybody out there, who has more details to this multi threading issue ?

    Pulsar7 wrote:
    But visibility of variables in multi core environments is not an issue of atomic access.No. I meant to say that you also can see other problems if you remove the syncronized/volatile access. Reading stale data isn't the only problem.
    Multi-Core architectures can have per core-registers, -caches, -controller devices interchanging the memory content in a certain way.
    It is obvious, that two thread running on different cores should run into problems. But we do not see them !?It's all implementation and hardware dependent. You can get different result when you are executing on different VMs as well as different operating systems.
    Are there some memory consistency features implemented in modern CPUs, supporting immediate register/cache/memory updates ?
    Is the behaviour depended on the OS ?See above.

  • Multithreading application - how to use correctly if uses network?

    I'm coding "daemon"-like software, which do some things in the loop (login to somewhere, do some things and logout) using multithreading.
    My application run about 1000-2000 threads and this threads do job.
    So here is my htop log at first ~10 minutes:
    http://i.stack.imgur.com/wlaeh.png
    So you can see on the above screenshot that some number of threads is running (ordered by green R status)
    But after some time this happens:
    http://i.stack.imgur.com/xf9Ny.png
    As you can see just one-three threads are running periodically (it seems like system slow down threads or make it sleeping).
    Application run threads for small-time job (it's about 10 seconds to 1 minute) and after thread die Fixed Thread Pool Executor create new threads to do same job.
    So as I understand it should be equal speed of threads execution at all times. But it slow down after ~10-20 minutes. Normally running threads in htop up to ~500-200-300-100 threads and down to ~3-30-60 threads at each time. But after this 10-20 minutes just only thread is executing. So can you tell me what is happens and how to fix that?
    Or can please anybody contact me at email *[REMOVED BY MODERATOR]* and I'll give source code (it's not big) and *[REMOVED BY MODERATOR]* ?
    Edited by: 907212 on Jan 10, 2012 5:45 PM
    Edited by: EJP on 11/01/2012 14:57: moderated. Don't post your email address in public places; and this is not a rent-a-coder service. Answers should be posted here.

    You don't mention what hardware you are runnng this on and what your software configuration is (os version, java version).
    But unless your are on a really big system you will never get 1000 - to 2000 threads doing anything at the same time.
    The number of threads that can execute simultaneously is mostly governed by hardware related to the number of processors and the number of cores.
    And if a thread isn't one of those that is executing simultaneously then it must be sleeping and not doing any work. With that many threads the biggest percentage of them will always be sleeping.
    Threads that are executing will keep executing until they relinquish control (to wait for some event) to another thread; unless you have implemented a homegrown time-slice mechanism that doles out fixed-interval slices to threads in a round-robin fashion.
    What drove the decision to use so many threads?
    We can provide some suggestions if you give us some information about what the business requirement actually is for your application. Include information about what type of work the threads will actually be doing.
    You included one example of logging in, doing some work, and logging out. How many different systems are you connecting to? Are they all Oracle DBs? How often do you connect/reconnect to the same Oracle DB? Depending on the use case your app could benefit by using a connection pool that keeps connections open; there is a high performance cost for opening/closing/reopening connections.

  • Solaris vs Linux For Multithreaded Application

    Hi all,
    I am having a Multithreaded Java application which is used to monitor devices in a network (NMS). After collecting the data from the devices, this will update the data in the database. (Usualy there will be lot of device around 25000) . There will be lot of database updation. I am planning to run this application in a server.
    But now I am confused in selecting which platform (which os) I have to use. There are two options for me. Either to use Linux or Solaris.
    Can anyone help me in selecting the platform. Since my application is multithreaded , please tell me which of these OS will give the best performance
    Thanks in advance

    There's no way to tell which one performs better without testing it. Both have reasonably mature and stable multitasking and multi threading. Why don't you write the application and try it on both platforms?

Maybe you are looking for

  • Battery Level Doesn't Change

    I just got a new iPod 3g Nano. The battery icon when I took it out of the box was about 6/7ths charged/green. I tried to fully charge it and it never increased to fully green. Plus, when I use it, it doesn't seem like it's losing any of the green col

  • Thunderbolt to DV in/out

    Hello, I bought a new 13" MacBook Pro Retina. As there is no FireWire Port I ordered the Apple adaptor Thunderbolt to FireWire. I have now following problem : my Sony camera has only DV in/out. I have a cable DV to FireWire 400 that I used to import

  • Lightroom 6 quits unexpectedly.  Has this happened to anyone else?

    Every time I try to edit images in the Develop module, the spinning wheel of death appears and Lightroom quits unexpectedly.  It makes trying to edit weddings extremely frustrating because it happens at different times while in the develop module.  I

  • Will a Pixma automatically use the alternate black cartridge?

    When the 220 black cartridge in my Pixma goes empty will the printer automatically use the 221 black instead? I now have three 221 blacks on the shelf and would just as soon use them up instead of buying a 220.

  • Embedding Captivate content on a webpage

    Hi there - apologies if this is a simple question, but it's different to how I normally produce Captivate content (which is as a zipped file that is uploaded to our server with the URL placed in various locations on our site). Our web developers have