Multithread locking of arrays

Hi, I'm writting a program which has a single array, and it's trying to pass the array to multiple threads (each thread is working on part of the array, there's no collision issues). Am I able to make each component of the array sychronized and not the entire array? I can always copy the array into many smaller sub-arrays, but that can get annoying very fast, and it takes time. Thank you!

If you are able to use ArrayLists, your data must be Objects, and each Object has an available monitor, so, yes, you may lock on each individual Object. This is not likely to solve your problem, however. There is no way to test the status of a monitor. So, if you are hoping to skip past a locked object to one that is not, you are out of luck. Also, if your List is to be modified in multiple threads, you must be very careful with the synchronization of the list (which is not coded synchronized like Vector). Finally, Iterators are not useable if the list is modified in multiple threads, as you will find that iterating through a list that is being modified will result in a ConcurrentModificationException.
You haven't specified much about the problem you are solving, just that you need to process items from an array in multiple threads. If you can predetermine which items go to which thread ahead of time, that would be an easy way to solve the problem, and would require no locking at all. Just pass into each thread the range of indexes to process.
If the list is fixed and the items in the list are independent, but processing time is variable, a synchronized update to a last modified index is one way to solve the problem. Write a method that is synchronized that returns the current value of an integer and increments it. Have each thread call this method when it is ready for a new object to process, and fetch that index from the array. The only locking needed is for this increment. Note that this solution and the one before it work fine for all data structures you list, and will work fine if the data is primitives.
If the list is not fixed, it is probably best to organize this as a queue. You'll need to synchronize on insertions into the queue and removals.
If the items are not independent, then synchronization and scheduling become a real problem.
With more details I may be able to help you more.

Similar Messages

  • Multithreading lock problem - AppClassLoader

    Hi all,
    I'm tuning a complex multithread java application.
    Performing various thread-dumps during application execution, I can see a great number of threads waiting for monitor entry: the locked resource is a Launcher$AppClassLoader class... one thread holds that resource, while all the others are waiting for it.
    Have you ever seen such a lock on the class loader? Is that a correct behaviour? Why the other threads cannot run concurrently instead of waiting for that particular thread?
    In the stack trace I can see that the thread that holds the lock is performing an operation on the filesystem ( java.io.File.exists() ) , but I/O subsystem is always idle...
    Any idea?
    Thanks guys!!!

    What's the output of the utlockt.sql? Let's see that to confirm that there is some lock contention going on?
    HTH
    Aman....

  • Array processing question

    Hello,
    I am somewhat new to JNI technology and am getting a bit confused about array processing.
    I am developing a C program that is called by a third party application. The third party application expects the C function to have a prototype similar to the following:
    int read_content(char *buffer, int length)
    It is expecting that I read "length" bytes of data from a device and return it to the caller in the buffer provided. I want to implement the device read operation in Java and return the data back to the C 'stub' so that it, in turn can return the data to the caller. What is the most efficient way to get the data from my Java "read_content" method into the caller's buffer? I am getting somewhat confused by the Get<Type>ArrayElements routines and this concepts of pinning, copying and releasing with the Release<Type>ArrayElements routine. Any advice would be helpful... particularly good advice :)
    Thanks

    It seems to me that you probably want to call the Java read_content method using JNI and have it return an array of bytes.
    Since you already have a C buffer provided, you need to copy the data out of the Java array into this C buffer. GetPrimitiveArrayCritical is probably what you want:
    char *myBuf = (char *)((*env)->GetPrimitiveArrayCritical(env, javaDataArray, NULL));
    memcpy(buffer, myBuf, length);
    (*env)->ReleasePrimitiveArrayCritical(env, javaDataArray, myBuf, JNI_ABORT);Given a Java array of bytes javaDataArray, this will copy length bytes out of it and into the C array named buffer. I used JNI_ABORT as the last argument to ReleasePrimitiveArrayCritical because you do not change the array. But you could pass a 0 there also, there would be no observable difference in the behavior.
    Think of it as requesting and releasing a lock on the Java array. When you have no lock, the VM is free to move the array around in memory (and it will do so from time to time). When you use one of the various GetArray functions, this will lock the array so that the VM cannot move it. When you are done with it, you need to release it so the VM can resume normal operations on it. Releasing it also propagates any changes made back to the array. You cannot know whether changes made in native code will be reflected in the Java array until you commit the changes by releasing the array. But since you are not modifying the array, it doesn't make any difference.
    What is most important is that you get the array before you read its elements and that you release it when you are done. That's all that you really need to know :}

  • Unlock permissitic lock.

    Dear all,
    How to unlock a permissitic lock without rollback the transaction?
    The following code is trying to lock an array of object with a number of try. If anyone of the object is locked already, it will sleep for 1 sec and then retry again until the retry limit is reached. During the sleep I would like to release all acquired lock. The block of ???????? is where I want to release the acquired lock. However, I don't have any idea to do so without rollback the transaction. Thanks in advance for your help.
    private static boolean lockObjList(UnitOfWork uow, PersistenceObject[] refObjList, int numRetry)throws DomainServiceException{
    if (refObjList == null){
    return true;
    boolean sucessful = false;
    while (!sucessful && numRetry >= 0) {
    try {
    for (int i=0; i<refObjList.length; i++){
    PersistenceObject refObj = refObjList;
    // use LOCK_NOWAIT option to avoid deadlock.
    PersistenceObject refObjClone
    = (PersistenceObject)uow.refreshAndLockObject(refObj, ObjectLevelReadQuery.LOCK_NOWAIT);
    if (refObj.getVersion() != refObjClone.getVersion()){
    throw new Exception("One of the refered object has been modified.");
    sucessful = true;
    } catch (DatabaseException dbException){
    // check whether the exception is due to the locking.
    if (dbException.getCause() instanceof SQLException){
    SQLException sqlException = (SQLException)dbException.getCause();
    // currently, we assume "Oracle" database is used
    // the error code of cannot arquire the lock is 54.
    if (sqlException.getErrorCode() == 54){     
    numRetry --;     // retry again
    // release currently acquired lock.
    try {
    Thread.sleep(1000);// sleep for 1 second
    } catch (InterruptedException e) {}
    } else {
    // or rethrow the exception.
    throw dbException;     
    return sucessful;
    Thanks and regards,
    William

    The only way to release a lock is through commit or rollback. You need to begin the transaction inside of your operation so that you can rollback or re-try.
    There are also other ways you can accomplish locking a set of objects.
    - Lock all of the objects in a single query, this will ensure that all of the locks are acquire as a group.
    - Lock the entire table, inefficient but will either succeed or fail as a single operation.
    - Use SQL SAVEPOINTS to rollback just to the save point. Savepoints are not currently directly support by TopLink, but you can create one through SQL.
    i.e.
    uow.executeNonSelectingSQL("SAVEPOINT MYSAVEPOINT");
    uow.executeNonSelectingSQL("ROLLBACK TO SAVEPOINT MYSAVEPOINT");

  • Problem with save as PDF with FrameMaker 12

    Hey,
    I just received FrameMaker 12 and I like it.
    But I'm having problems when I use the save as PDF function.
    It takes very long time to generate the PDF (at least 30 minutes for a book)
    The fonts and spaces are messed up in the finished PDF. Some text blocks have paragraphs running over the margin
    I have tried a couple of different things:
    saving as MIF and back to FM
    uncheck rely on system fonts only, do not use document fonts
    but nothing have worked yet, please help I'm getting frustrated.

    Hi Jeff,
    After I realized that I didn't make the setprint.ini file, I did that and deleted the printer=adobe pdf in the .dll file. But it still doesn't work. I save to pdf anyway, I was just trying to optimize my usage by making the default printer in FM the adobe pdf printer.
    Anyway, the .dll file from above has the below information in it. I don't know what it means, but it has errors all over. Does this make a difference and what does it mean?....Thanks!
    TLOSS error
       SING error
        DOMAIN error
      R6028
    - unable to initialize heap
        R6027
    - not enough space for lowio initialization
        R6026
    - not enough space for stdio initialization
        R6025
    - pure virtual function call
       R6024
    - not enough space for _onexit/atexit table
        R6019
    - unable to open console device
        R6018
    - unexpected heap error
        R6017
    - unexpected multithread lock error
        R6016
    - not enough space for thread data
    abnormal program termination
        R6009
    - not enough space for environment
    R6008
    - not enough space for arguments
       R6002
    - floating point not loaded
        Microsoft Visual C++ Runtime Library   
      Runtime Error!
    Program:    ... <program name unknown>        ð?   À~ PA   €ÿÿGAIsProcessorFeaturePresent   KERNEL32    e+000                    EEE     50 P     (8PX   700WP           `h````  ppxxxx                     ( n u l l )     (null)  GetLastActivePopup  GetActiveWindow MessageBoxA user32.dll          ÿÿÿÿÇ´  Ë´      ÿÿÿÿVà Zà ÿÿÿÿÅà Éà ÿÿÿÿÇ  ¡Ç  ÿÿÿÿQÈ  UÈ  SunMonTueWedThuFriSat   JanFebMarAprMayJunJulAugSepOctNovDec    TZ  1#QNAN  1#INF   1#IND   1#SNAN              À      F        À      F        À      Fìþ          z    à              ¶   ,á            ì    á  (               <á                      @   N   ^   l   „   j   Z   J   8   &   ø  
           .   <   L   Z   j   x   ‚   Œ   ¢   ´   Ä   Ú   î   þ           0   B   N   Z   h   x      ¤   ´   À   Ø   ò           ,   @   T   n   ~   Œ   š   ¨   º   Ê   Ø   ê           4   L   f   ‚   Ž   ž   °   À   Ô   à   ì   ö               Ä   Ú       ˜   ¦   ˆ       œ   ¬   ¾   Ø   ê       ˆ GlobalFree  “ GlobalUnlock  Œ GlobalLock   GlobalAlloc KERNEL32.dll    ClosePrinter  n GetPrinterA | OpenPrinterA  WINSPOOL.DRV    GetFileVersionInfoA
    VerQueryValueA  VERSION.dll  RaiseException 
    GetFileAttributesA  t GetVersion    CloseHandle a SetEndOfFile  4 CreateFileA d SetErrorMode  W DeleteFileA ó _lcreat ò _lclose $ GetModuleFileNameA  > GetProcAddress   LoadLibraryA  ä MultiByteToWideChar & GetModuleHandleA  Õ LockResource  Ç LoadResource  • SizeofResource  £ FindResourceA Ê GetCommandLineA ™ HeapAlloc Ÿ HeapFree  ¢ HeapReAlloc   GetLastError  õ GetCurrentDirectoryA    GetFullPathNameA    GetDriveTypeA  FindClose Š FileTimeToSystemTime  ‰ FileTimeToLocalFileTime ” FindFirstFileA  b SetEnvironmentVariableA } ExitProcess ž TerminateProcess  ÷ GetCurrentProcess   GetEnvironmentVariableA u GetVersionExA  HeapDestroy › HeapCreate  ¿ VirtualFree m SetHandleCount  R GetStdHandle    GetFileType P GetStartupInfoA ² FreeEnvironmentStringsA ³ FreeEnvironmentStringsW Ò WideCharToMultiByte   GetEnvironmentStrings   GetEnvironmentStringsW  ] DisableThreadLibraryCalls ß WriteFile » VirtualAlloc  j SetFilePointer  | SetStdHandle  ª FlushFileBuffers    ReadFile  ¿ GetCPInfo ¹ GetACP  1 GetOEMCP  S GetStringTypeA  V GetStringTypeW  ! CompareStringA  " CompareStringW  ¿ LCMapStringA  À LCMapStringW  p GetTimeZoneInformation  / RtlUnwind USER32.dll  O CoTaskMemFree N CoTaskMemAlloc  @ CoRegisterMessageFilter S CoUninitialize    CoDisconnectObject  ole32.dll           °oÓF    B               8   <   @    ?  O     SetPrint.dll _F_ApiClientInit@16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ö¤   ²          ›¥                  Could not open SetPrint.ini file    success
        failed, error = %i
    Setting printer to "%s"...  Printer =   rt  SetPrint plugin from Sundorne Communications www.sundorne.com
      SetPrint.ini    /restore    /save   winspool    `á  `â  `ã  `ä  `å  `æ  `è  `é  `ê  `ë  `ì  `í  \   <c> .   <u> ..  <r> <v> ..\ FRM %ld ø  ü   „   †   ˆ  Ð4                                                                  ô           ì           à   €O  GetResource Notify  DialogEvent Message Command Quit    Initialize  ClientOutFormat ClientInFormat  ClientSuffix    ClientVendor    ClientFormatId  ClientFacet ClientProducts  FileDescription ClientType  ClientName  \StringFileInfo\040904b0\   Hj  u˜  s˜  “V  ªV  ªV              ÿÿÿÿ
          $ü      øû      Ìû 
       ¨û      |û      Lû      (û      üú      Äú      œú      dú      ,ú       ú  x   ôù  y   äù  z   Ôù  ü   Ðù  ÿ   Àù  š   š                       ( ( ( ( (                                     H                               „ „ „ „ „ „ „ „ „ „                                                                         ‚ ‚ ‚ ‚ ‚ ‚                                                                                                                                                                                                                                                                                                                            .               è‹              –Á  –Á  –Á  –Á  –Á  –Á  Ø   Ø   ð   ð   ÿÿÿÿÿÿÿÿð   ñ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

  • Flex Builder 4.6 app runs fine in builder - exported release Air app flashes and dies

    I have tried exporting release build on more than one computer.
    I have successfully exported release builds of other projects.
    if I open the application's .exe file and scroll down I see this kind of stuff:
    An application has made an attempt to load the C runtime library incorrectly.
    Please contact the application's support team for more information.
          R6033
    - Attempt to use MSIL code from this assembly during native code initialization
    This indicates a bug in your application. It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain.
      R6032
    - not enough space for locale information
          R6031
    - Attempt to initialize the CRT more than once.
    This indicates a bug in your application.
      R6030
    - CRT not initialized
      R6028
    - unable to initialize heap
        R6027
    - not enough space for lowio initialization
        R6026
    - not enough space for stdio initialization
        R6025
    - pure virtual function call
       R6024
    - not enough space for _onexit/atexit table
        R6019
    - unable to open console device
        R6018
    - unexpected heap error
        R6017
    - unexpected multithread lock error
        R6016
    - not enough space for thread data
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
       R6009
    - not enough space for environment
    R6008
    - not enough space for arguments
       R6002
    - floating point support not loaded
        Microsoft Visual C++ Runtime Library   
      ... <program name unknown>  Runtime Error!

    I have tried exporting release build on more than one computer.
    I have successfully exported release builds of other projects.
    if I open the application's .exe file and scroll down I see this kind of stuff:
    An application has made an attempt to load the C runtime library incorrectly.
    Please contact the application's support team for more information.
          R6033
    - Attempt to use MSIL code from this assembly during native code initialization
    This indicates a bug in your application. It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain.
      R6032
    - not enough space for locale information
          R6031
    - Attempt to initialize the CRT more than once.
    This indicates a bug in your application.
      R6030
    - CRT not initialized
      R6028
    - unable to initialize heap
        R6027
    - not enough space for lowio initialization
        R6026
    - not enough space for stdio initialization
        R6025
    - pure virtual function call
       R6024
    - not enough space for _onexit/atexit table
        R6019
    - unable to open console device
        R6018
    - unexpected heap error
        R6017
    - unexpected multithread lock error
        R6016
    - not enough space for thread data
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
       R6009
    - not enough space for environment
    R6008
    - not enough space for arguments
       R6002
    - floating point support not loaded
        Microsoft Visual C++ Runtime Library   
      ... <program name unknown>  Runtime Error!

  • I have droplet problems

    i drag picture on it but nothing is done.  " mscoree.dll runtime error" if someone can help me thanks a lot

    so it is on photoshop cs5 on windows7
    problem apear is:
    GetValue FlsAlloc  CorExitProcess mscoree.dllruntime error
      TLOSS error
       SING error
        DOMAIN error
      R6034
    An application has made an attempt to load the C runtime library incorrectly.
    Please contact the application's support team for more information.
          R6033
    - Attempt to use MSIL code from this assembly during native code initialization
    This indicates a bug in your application. It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain.
      R6032
    - not enough space for locale information
          R6031
    - Attempt to initialize the CRT more than once.
    This indicates a bug in your application.
      R6030
    - CRT not initialized
      R6028
    - unable to initialize heap
        R6027
    - not enough space for lowio initialization
        R6026
    - not enough space for stdio initialization
        R6025
    - pure virtual function call
       R6024
    - not enough space for _onexit/atexit table
        R6019
    - unable to open console device
        R6018
    - unexpected heap error
        R6017
    - unexpected multithread lock error
        R6016
    - not enough space for thread data
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
       R6009
    - not enough space for environment
    R6008
    - not enough space for arguments
       R6002
    - floating point support not loaded
        Microsoft Visual C++ Runtime Library   
      ... <program name unknown>  Runtime Error!
    intel core i5 cpu 650 3,2
    6go ram
    windows7 64bits

  • I want to know why it's happening me this error: Object synchronization method was called from an unsynchronized block of code.

    I'm developing a Smart Array (it's a request I cannot use a List of int that I know it's easier because I made both codes). I have done this class and below is the example of how I use it. The error is often in this line (153 from class):
    // Ensure that the lock is released.
    Monitor.Exit(array);
    If I use a List nothing wrong happens just when I translate to an array. Thanks for your help.
    SmartArray3.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    namespace SmartArray
    class SmartArray3
    private int[] array;
    private int size = 0;
    private int count = 0;
    public SmartArray3()
    Resize(1);
    public SmartArray3(int size)
    this.size = size;
    array = new int[this.size];
    public bool Resize(int size)
    try
    if (array == null)
    array = new int[size];
    else
    Array.Resize(ref array, size);
    this.size++;
    return true;
    catch
    return false;
    private void add(int value)
    try
    if (array == null)
    this.size = 1;
    Resize(this.size);
    array[0] = value;
    this.count++;
    else
    if (this.count == (this.size - 1))
    this.size *= 2;
    this.Resize(this.size);
    if ((this.count - 1) < 0)
    array[0] = value;
    else
    array[this.count - 1] = value;
    this.count++;
    catch (Exception ex)
    Console.Write(ex.ToString());
    throw new System.IndexOutOfRangeException("Index out of Range.");
    // Lock the array and add an element.
    public void Add(int value)
    // Request the lock, and block until it is obtained.
    Monitor.Enter(array);
    try
    if (array == null)
    this.size = 1;
    Resize(this.size);
    array[0] = value;
    this.count++;
    else
    if (this.count == (this.size - 1))
    this.size *= 2;
    this.Resize(this.size);
    if ((this.count - 1) < 0)
    array[0] = value;
    else
    array[this.count - 1] = value;
    this.count++;
    finally
    // Ensure that the lock is released.
    Monitor.Exit(array);
    // Try to add an element to the List: Add the element to the List
    // only if the lock is immediately available.
    public bool TryAdd(int value)
    // Request the lock.
    if (Monitor.TryEnter(array))
    try
    if (array == null)
    this.size = 1;
    Resize(this.size);
    array[0] = value;
    this.count++;
    else
    if (this.count == (this.size - 1))
    this.size *= 2;
    this.Resize(this.size);
    if ((this.count - 1) < 0)
    array[0] = value;
    else
    array[this.count - 1] = value;
    this.count++;
    finally
    // Ensure that the lock is released.
    Monitor.Exit(array);
    return true;
    else
    return false;
    public int Get(int index)
    try
    return array[index];
    catch (IndexOutOfRangeException ex)
    throw new System.IndexOutOfRangeException("Index out of range");
    Code for called the Class:
    private static int threadsRunning = 0;
    private SmartArray3 sa = new SmartArray3();
    private List<double> times;
    private static string[] titles ={
    "Add ", "Add failed ", "TryAdd succeeded ", "TryAdd failed "};
    private static int[][] results = new int[3][];
    //Event to Create Threads
    private void newTest()
    for (int i = 0; i < 3; i++)
    Thread t = new Thread(ThreadProc);
    t.Start(i);
    Interlocked.Increment(ref threadsRunning);
    private void ThreadProc(object state)
    times = new List<double>();
    DateTime finish = DateTime.Now.AddSeconds(10);
    Random rand = new Random();
    int[] result = { 0, 0, 0, 0};
    int threadNum = (int)state;
    while (DateTime.Now < finish)
    Stopwatch sw = Stopwatch.StartNew();
    int what = rand.Next(250);
    int how = rand.Next(25);
    if (how < 16)
    try
    sa.Add(what);
    result[(int)ThreadResultIndex.AddCt] += 1;
    times.Add(sw.Elapsed.TotalMilliseconds);
    catch
    result[(int)ThreadResultIndex.AddFailCt] += 1;
    else
    if (sa.TryAdd(what))
    result[(int)ThreadResultIndex.TryAddSucceedCt] += 1;
    else
    result[(int)ThreadResultIndex.TryAddFailCt] += 1;
    sw.Stop();
    results[threadNum] = result;
    if (0 == Interlocked.Decrement(ref threadsRunning))
    StringBuilder sb = new StringBuilder(
    " Thread 1 Thread 2 Thread 3 Total\n");
    for (int row = 0; row < 4; row++)
    int total = 0;
    sb.Append(titles[row]);
    for (int col = 0; col < 3; col++)
    sb.Append(String.Format("{0,4} ", results[col][row]));
    total += results[col][row];
    sb.AppendLine(String.Format("{0,4} ", total));
    Console.WriteLine(sb.ToString());
    private enum ThreadResultIndex
    AddCt,
    AddFailCt,
    TryAddSucceedCt,
    TryAddFailCt
    Federico Navarrete

    The array that you're calling Monitor.Enter under is not always the same array that you call Monitor.Exit on. When you resize an array using Array.Resize, you pass the variable in as a reference parameter (ref). The method then creates a
    new array object and assigns it to the array variable. Then, when you leave the synchronization block after resizing the array, your Monitor.Exit uses the new array rather than the one it originally entered
    and... boom.
    Instead of locking on the array itself, create a new private readonly field of type Object called "lock" within the SmartArray class and use that to lock on. It doesn't
    need to be readonly, but the keyword will prevent you from accidentally introducing this issue again.

  • Multithreaded array sorting

    Does anyone have an example program which demonstrates how to sort an array efficiently using multiple synchronized threads. I need to use 8 threads to sort an array of 64 integers for a college project and I cant seem to work out how to do it!!
    Thankyou
    Stuart Mortimer

    Sorting with multiple threads is only efficiant if each thread run at its own processor. Because multithreading on one processor means that all threads except one is allways in a pause state. A sorting algorithm that can be used with multithreading has to be a devide and conquer algorithm. You divide the data, and let different threads handle its own part of the data. Mergesort and Quicksort are algorithms you could use, whereas boublesort, insertionsort and shellsort are not. Using multithreaded sorting for 64 elements is an overkill, but it could be interesting for a student- project. In real life you ned far more data to get an advantage of such an advanced sorting method with so much overhead.
    Both mergesort and quicksort are recursive, and both call itself two times. Thouse two calls could be done simultaniously by multithreading.
    Mergesort seems to be the easyest algorithm, because mergesort always devide the data in two halfs of the same size. Quicksort might perform better however. In singlethreaded sorting quicksort is faster than mergesort except when the amount of data is too big to be kept in memory. - and offcorse in the theoretical worstcase of quicksort where the performance is O(n^2), not O(n*log(n)).

  • How to input 1d waveform array into lock in amplifier

    I have a LabView application that I need to modify to include a lock in amplifier.  The application uses a DAQ card to repeatedly capture a 0.1s sample of two signals (one signal, one reference) during a laboratory test.  Previously the measurement was being made by the basic RMS module to record the VRMS of the signal, and the reference ignored.  However, the measurements I am now performing are at a much lower S:N and so I'd like to use a simple LIA from the NI kit. 
    I am happy (I think) with the setup of the LIA but I'm struggling to understand the input data requirement.  It has to be a 2D array, with the reference signal identifited.  However, the output from the part of the utility that captures the waveform is a 1D array of waveform.  Could someone advise me on how to convert from the 1D array of waveform to a 2D array suitable for input to the LIA?
    As you might have guessed I'm a newbie to LabView, I'm trying to modify an application designed and assembled by a former colleague, so I'd appreciate simple suggestions! 
    TIA.
    Solved!
    Go to Solution.

    I tried that but fell at the first hurdle.  I've attached the error message that I got.  It refers to the issue being due to connecting the type Waveform (DBL) to a 1D array of double.  Apologies if I'm missing something simple here, software is not my strong point.
    I'm a bit confused as to why the LIA (or its subVI's) require a 2D array.  I would have thought, from experience with hardware LIA (5210's etc) that the VI's would want a waveform to process.
    Each entry in the TDMS file is a 0.1s capture of an ~800Hz sine wave, along with the reference signal from the chopper that generated the modulation.  I'm a bit confused as to how this data gets represented in a 2D array for the LIA.  I assume that the waveform is broken into an array of amplitude versus time for each sample and that each sample is then indexed in the 2D array?
    Anyway, all help greatly appreciated.
    Thanks.
    Message Edited by Ringding on 03-27-2009 12:25 PM
    Attachments:
    connection error.jpg ‏84 KB

  • SQL Updates, locks and multithreading

    I am trying to do the following:
    Updating 5M rows in multiple database writer thread using OCI. I have not set any Oracle parallel execution related parameters yet.
    However, it is found that there is no performance improvement in doing this in multiple threads.
    My questions:
    1. Does Oracle lock the table during each update? I think it does and that is why there is no performance enhancement.
    - If yes, is there any way to tell Oracle not to lock the table during updates. As it is, my threads will never update the same row in parallel.
    2. Is the way out is to do the following?
    - Partition the table
    - Do an "ALTER SESSION ENABLE PARALLEL DML"
    - Fire updates
    3. Is there any OCI way of doing this?
    Thanks in advance,
    ~Sri

    I am trying to do the following:
    Updating 5M rows in multiple database writer thread using OCI. I have not set any Oracle parallel execution related parameters yet.
    However, it is found that there is no performance improvement in doing this in multiple threads.
    My questions:
    1. Does Oracle lock the table during each update? I think it does and that is why there is no performance enhancement.
    - If yes, is there any way to tell Oracle not to lock the table during updates. As it is, my threads will never update the same row in parallel.Unless you're explicitly locking the table, Oracle won't lock it. Oracle doesn't ever escalate row locks to table locks.
    My guess is that something in your system is bottlenecked. Perhaps your disk, CPU, or RAM is maxed out. Perhaps your system is busy waiting to switch redo logs. There are lots of potential bottlenecks here, you'd have to do some investigation to figure out what's taking so long.
    One quick thing you might try is to use the /*+ append */ hint (i.e. insert /*+ append */ into table (your query here)). This tells the database to do a parallel direct path load, avoiding most redo log generation. This improves the speed, but isn't particularly friendly for database recovery. Make sure you read the documentation on this hint before you use it.
    You may also be able to change your SQL to use the bulk collection syntax.
    I'd also take a look at this bit of the Oracle documentation, "Serial and Parallel INSERT"
    <http://technet.oracle.com/docs/products/oracle8i/doc_library/817_doc/server.817/a76965/c21dlins.htm#2507>
    There's a pretty good asktom article over here. Search the site and you'll find lots of info on this sort of thing.
    <http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:1119033812977>
    2. Is the way out is to do the following?
    - Partition the table
    - Do an "ALTER SESSION ENABLE PARALLEL DML"
    - Fire updatesI'm assuming that you have the partitioning options in your database. If so, this is likely to be faster, though I'm not sure by how much. Have you benchmarked a test to see? Of course, if you account for the time to partition the table, you're likely to remove all of your performance gain.
    Also, unless you are able to spread your inserts across the various partitions, you're unlikely to see any performance benefit.
    This strikes me as a pretty drastic approach.
    3. Is there any OCI way of doing this?Well, you could execute the SQL above against the table from OCI. I'd recommend doing this sort of thing from SQL*Plus or something of that genre, though, simply because folks may get upset at you if your application partitions their tables for them.
    Thanks in advance,
    ~Sri

  • Lock elements of array true when the input element becomes true

    I am working on a project in which I want to create an alarm for any number of inputs.  Any time that there is a input that is out of range, an alarm will be indicated.  I would like the indicated alarm to persist until the user pushes a reset button, even if the out of range condition is removed.  In addition, new alarm indication may occur and should also persist. 
    In the VI below, the Alarm Dummy Control is used to generate alarm(s).  I want the Alarm Indicated array to maintain any element that had or currently has a true (alarm element).  Every alarm will persist until the manual reset is pushed.  
    I am sure there is a simple solution, but I am drawing a blank right now.  
    Solved!
    Go to Solution.
    Attachments:
    Alarm Test.vi ‏21 KB

    You can operate on the array directly, no FOR loop needed.
    Here's a very quick draft. See if it can give you some ideas.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    Alarm TestMOD.vi ‏13 KB

  • Too many locks

    Hello everyone,
    We are using Berkeley DB Java Edition with our heavily-multithreaded application. Transactions have been enabled on the database.
    While the system is busy, we occasionally face an orgy of LockTimeoutException, which can be resolved only by a restart. Note that the locks also usually prevent a clean close of the database/environment.
    One such sample is:
    com.sleepycat.je.LockTimeoutException: (JE 4.1.17) Lock expired. Locker 1668415747 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970960_ReadCommittedLocker: waited for lock on database=sampleDatabase LockAddr:292555378 node=8759642048 type=READ grant=WAIT_NEW timeoutMillis=30000 startTime=1327243343323 endTime=1327243373324 Owners: [<LockInfo locker="1542681801 610308237201_TH_CGTOutCylSMS5 for gOutMsgID 96970930_Txn" type="WRITE"/>]
    Waiters: [<LockInfo locker="711907833 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970946_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1403551794 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970940_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1369480950 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970957_ReadCommittedLocker" type="READ"/>, <LockInfo locker="136598104 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970970_ReadCommittedLocker" type="READ"/>, <LockInfo locker="144315285 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970968_ReadCommittedLocker" type="READ"/>, <LockInfo locker="997957532 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970975_ReadCommittedLocker" type="READ"/>, <LockInfo locker="824957254 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970961_ReadCommittedLocker" type="READ"/>, <LockInfo locker="902142946 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970967_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1081945602 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970977_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1515693665 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970981_ReadCommittedLocker" type="READ"/>, <LockInfo locker="812406941 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970964_ReadCommittedLocker" type="READ"/>, <LockInfo locker="422429761 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970966_ReadCommittedLocker" type="READ"/>, <LockInfo locker="196056145 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970976_ReadCommittedLocker" type="READ"/>, <LockInfo locker="875714517 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970971_Read
    CommittedLocker" type="READ"/>, <LockInfo locker="1428454328 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970969_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1312200883 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970985_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1650669538 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970986_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1609173220 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970983_ReadCommittedLocker" type="READ"/>, <LockInfo locker="2112741869 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970982_ReadCommittedLocker" type="READ"/>, <LockInfo locker="210489138 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970980_ReadCommittedLocker" type="READ"/>, <LockInfo locker="785375757 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970973_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1375853437 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970978_ReadCommittedLocker" type="READ"/>, <LockInfo locker="2012288410 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970988_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1958833914 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970987_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1197284088 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970984_Re
    adCommittedLocker" type="READ"/>, <LockInfo locker="831894626 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970979_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1534437212 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970911_ReadCommittedLocker" type="READ"/>, <LockInfo locker="155105261 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970922_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1956457816 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970919_ReadCommittedLocker" type="READ"/>, <LockInfo locker="2064394295 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970928_ReadCommittedLocker" type="READ"/>, <LockInfo locker="1529115133 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970936_ReadCommittedLocker" type="READ"/>, <LockInfo locker="901236682 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970943_ReadCommittedLocker" type="READ"/>, <LockInfo locker="882752083 -1_TH_CGTOutCylSMS5 for gOutMsgID 96970937_ReadCommittedLocker" type="READ"/>]
    This has today resulted in, during shutdown:
    com.sleepycat.je.EnvironmentFailureException: (JE 4.1.17) Txn undo for LSN=0x4c5d59/0x2fd8da UNEXPECTED_EXCEPTION: Unexpected internal Exception, may have side effects.
    Problem: 376426 locks left
    java.lang.OutOfMemoryError
    Java heap space
    java.util.Arrays.copyOf(Arrays.java:2882)
    java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    java.lang.StringBuilder.append(StringBuilder.java:119)
    java.lang.StringBuilder.append(StringBuilder.java:115)
    com.sleepycat.je.txn.LockManager.dumpToStringNoLatch(LockManager.java:1205)
    com.sleepycat.je.txn.LockManager.dumpToString(LockManager.java:1187)
    com.sleepycat.je.txn.LockManager.dump(LockManager.java:1177)
    com.sleepycat.je.dbi.EnvironmentImpl.checkLeaks(EnvironmentImpl.java:1786)
    com.sleepycat.je.dbi.EnvironmentImpl.doClose(EnvironmentImpl.java:1642)
    com.sleepycat.je.dbi.DbEnvPool.closeEnvironment(DbEnvPool.java:348)
    com.sleepycat.je.dbi.EnvironmentImpl.close(EnvironmentImpl.java:1480)
    com.sleepycat.je.Environment.closeInternal(Environment.java:460)
    com.sleepycat.je.Environment.close(Environment.java:321)
    and also an entry in the je.log:
    120122 19:55:18:205 SEVERE [prog/citrine/db]LNUndo success=false node=46893596705 lsn=0x4c5d59/0x2fd8da found=false replaced=false inserted=false index=-1
    We are using 4.1.17 (also previously used 4.1.10 with the same problem) on java 1.6.0u27, 64-bit, on CentOS. The size of the database is over 250 GB.
    We am currently running a database.verify() to ensure that the data isnt corrupt.
    Our questions are:
    * The locks seem to be read locks. While i am aware that the Java Edition does not have MVCC, is there any other mechanism that will give similar 'locklessness'?
    * Should increasing the setting of LOCK_N_LOCK_TABLES to say, 500, reduce the chances of this issue?
    * Are there any other lock-specific settings we should be aware of? We have set the reads to READ_COMMITTED.
    and more immediately:
    * Is database.verify sufficient to verify that there is no corruption of data? (It has been running for over half an hour now) There is only one database in the environment.
    Awaiting your response,
    Sushant A.

    I have some additional comments.
    The SEVERE log message that you describe should be accompanied by a stack trace. If you have one, please send it. If there was no stack trace, then it is possible this is due to an OutOfMemoryError. This is likely due to the large number of locks that have accumulated.
    Increasing the number of JE lock tables will not solve this problem. The number of lock tables is increased for better concurrency at a lower level. It won't avoid lock timeouts.
    In the stack trace you sent, one thread holds a write lock, and many threads are waiting on a read lock. Lock timeouts can occur because the writer holds the lock (does not commit/abort the txn) for the timeout interval, but this seems unlikely since it looks like you have set the timeout to 30s.
    The other reason for the exception is a deadlock. Deadlocks are described in the doc link I sent you earlier. If a deadlock occurs, you'll need to retry the operation after closing the cursors and aborting the txn.
    Because many (300k) locks have accumulated, I strongly suspect your app is not closing cursors and/or txns when an exception occurs. The first thing is to check for that.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Cursors throwing Unexpected lock status: 6

    I am using a Berkeley DB instance and have a multithreaded clients each performing various types of operations on the database. In general there are three types of operations being performed, reads, writes (which use RMW locks), and (non-transactional) cursors. After some time my application dumps the following:
    Unexpected lock status: 6
    PANIC: Invalid argument
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__os_stack+0x2b) [0xb7ef085f]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__os_abort+0x1d) [0xb7eecbb1]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__env_panic+0xa8) [0xb7e80b8e]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__lock_get_internal+0x1855) [0xb7e2e925]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__lock_vec+0x167) [0xb7e2bd3c]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__db_lget+0x45c) [0xb7e8bfe2]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so [0xb7d850a5]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so [0xb7d816ce]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__dbc_get+0x598) [0xb7e76a29]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__dbc_get_pp+0x11f) [0xb7e870ec]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(_ZN3Dbc3getEP3DbtS1_j+0x3f) [0xb7d71feb]
    ...(non-berkeley db layers)...
    /lib/tls/i686/cmov/libpthread.so.0 [0xb7a2350f]
    /lib/tls/i686/cmov/libc.so.6(clone+0x5e) [0xb7b20a0e]
    Aborted
    In studying the core dumps and doing some testing this behavior only occurs when cursors are used, when those operations are removed the error no longer manifests itself so either the cursor is the issue, or how the cursors are interacting with other operations. Below is a sample of cursor code being run:
    void run_cursor()
    Dbc *curp = NULL;
    int ret;
    try
    int i = 0;
    Dbt key(&i,sizeof(int));
    Dbt data(buffer, 0);
    data.set_flags(DB_DBT_USERMEM);
    data.set_ulen(MAX_BUFFER_SIZE);
    db->cursor(NULL, &curp, 0);
    while ((ret = curp->get(&key, &data, DB_NEXT)) == 0) {
    //keep a list of all the keys in the db
    keys.push_back(i);
    if (ret != DB_NOTFOUND) {
    cerr << "Cursor Error" << endl;
    curp->close();
    catch (DbException &ex) {
    cout << ex.what() << endl;
    if( curp != NULL )
    curp->close();
    I have tried wrapping the cursor in a transaction and it seems to worsen the issue (it happens more frequently). In all methods I am printing error messages when exceptions are thrown by the database (e.g. DbExceptions) and the program prints no errors so I do not believe it is a side effect of some other error. Here is the relevant parts of the case where is RMW lock is used:
    void update()
    DbTxn *txn = NULL;
    try
    env->txn_begin(NULL, &txn, 0);
    int id;
    Dbt key( &id, sizeof(int) );
    Dbt value(buffer, 0);
    value.set_flags(DB_DBT_USERMEM);
    value.set_ulen(MAX_BUFFER_SIZE);
    int err = db->get(txn, &key, &value, DB_RMW);
    if( err == DB_NOTFOUND )
    cerr << "Error trying to read key" << endl;
    txn->abort(0);
    return;
    //code to modify the value here
    db->put(txn, &key, &value, 0);
    txn->commit(0);
    } catch (DbDeadlockException &de) {
    cout << de.what() << endl;
    if( txn != NULL )
    txn->abort();
    Just in case it helps, here is the output of db_stat -CA on the database after it has crashed:
    Default locking region information:
    12     Last allocated locker ID
    0x7fffffff     Current maximum unused locker ID
    9     Number of lock modes
    5000     Maximum number of locks possible
    5000     Maximum number of lockers possible
    1000     Maximum number of lock objects possible
    1     Number of lock object partitions
    6     Number of current locks
    9     Maximum number of locks at any one time
    4     Maximum number of locks in any one bucket
    0     Maximum number of locks stolen by for an empty partition
    0     Maximum number of locks stolen for any one partition
    11     Number of current lockers
    13     Maximum number of lockers at any one time
    3     Number of current lock objects
    9     Maximum number of lock objects at any one time
    2     Maximum number of lock objects in any one bucket
    0     Maximum number of objects stolen by for an empty partition
    0     Maximum number of objects stolen for any one partition
    3786734     Total number of locks requested
    3786664     Total number of locks released
    0     Total number of locks upgraded
    1     Total number of locks downgraded
    7089     Lock requests not available due to conflicts, for which we waited
    61     Lock requests not available due to conflicts, for which we did not wait
    0     Number of deadlocks
    0     Lock timeout value
    0     Number of locks that have timed out
    1000000     Transaction timeout value
    0     Number of transactions that have timed out
    1MB 592KB     The size of the lock region
    0     The number of partition locks that required waiting (0%)
    0     The maximum number of times any partition lock was waited for (0%)
    0     The number of object queue operations that required waiting (0%)
    0     The number of locker allocations that required waiting (0%)
    165     The number of region locks that required waiting (0%)
    2     Maximum hash bucket length
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Lock REGINFO information:
    Lock     Region type
    5     Region ID
    __db.005     Region name
    0xb781b000     Original region address
    0xb781b000     Region address
    0xb781b0c0     Region primary address
    0     Region maximum allocation
    0     Region allocated
    Region allocations: 11006 allocations, 0 failures, 0 frees, 1 longest
    Allocations by power-of-two sizes:
    1KB     11003
    2KB     0
    4KB     0
    8KB     0
    16KB     1
    32KB     0
    64KB     1
    128KB     1
    256KB     0
    512KB     0
    1024KB     0
    REGION_JOIN_OK     Region flags
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Lock region parameters:
    104     Lock region region mutex [165/7025066 0% 5979/3075574672]
    8191     locker table size
    1031     object table size
    600     obj_off
    62608     locker_off
    0     need_dd
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Lock conflict matrix:
    0     0     0     0     0     0     0     0     0     
    0     0     1     0     1     0     1     0     1     
    0     1     1     1     1     1     1     1     1     
    0     0     0     0     0     0     0     0     0     
    0     1     1     0     0     0     0     1     1     
    0     0     1     0     0     0     0     0     1     
    0     1     1     0     0     0     0     1     1     
    0     0     1     0     1     0     1     0     0     
    0     1     1     0     1     1     1     0     1     
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Locks grouped by lockers:
    Locker Mode Count Status ----------------- Object ---------------
    4 dd= 9 locks held 1 write locks 0 pid/thread 5979/3080828608
    4 READ 1 HELD ./db.db handle 0
    5 dd= 8 locks held 0 write locks 0 pid/thread 5979/3072416656
    6 dd= 7 locks held 0 write locks 0 pid/thread 5979/3073469328
    7 dd= 6 locks held 0 write locks 0 pid/thread 5979/3073469328
    8 dd= 5 locks held 0 write locks 0 pid/thread 5979/3073469328
    9 dd= 4 locks held 0 write locks 0 pid/thread 5979/3072416656
    a dd= 3 locks held 1 write locks 0 pid/thread 5979/3073469328
    a READ 1 WAIT ./db.db page 21
    a READ 2 HELD ./db.db page 20
    b dd= 2 locks held 1 write locks 0 pid/thread 5979/3075574672
    b READ 1 WAIT ./db.db page 21
    b READ 2 HELD ./db.db page 20
    c dd= 1 locks held 0 write locks 0 pid/thread 5979/3075574672
    800054d9 dd= 0 locks held 1 write locks 1 pid/thread 5979/3074522000expires 05-05-12:41:36.048050000
    800054d9 WRITE 2 HELD ./db.db page 21
    800054da dd= 0 locks held 0 write locks 0 pid/thread 5979/3072416656
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Locks grouped by object:
    Locker Mode Count Status ----------------- Object ---------------
    800054d9 WRITE 2 HELD ./db.db page 21
    a READ 1 WAIT ./db.db page 21
    b READ 1 WAIT ./db.db page 21
    a READ 2 HELD ./db.db page 20
    b READ 2 HELD ./db.db page 20
    4 READ 1 HELD ./db.db handle 0
    The number of locks and lockers is somewhat low at 5,000 each, this was intentional since the same error occurs with larger numbers of locks/lockers it simply takes longer to occur. I have reviewed this output and nothing out of the ordinary seems to be occurring as far as I can tell.
    Any additional insight would be appreciated.

    Thanks to both of you for your suggestions.
    I have still noticing the same issue with very similar output etc so I wont repost that. After running it some more I have started seeing another error message, it looks like its very similar and hopefully with make the issue more clear.
    Here is the output of the error file:
    TAS unlock failed: lock already unlocked
    PANIC: Permission denied
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__os_stack+0x2b) [0xb7ee585f]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__os_abort+0x1d) [0xb7ee1bb1]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__env_panic+0xa8) [0xb7e75b8e]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__db_tas_mutex_unlock+0xa5) [0xb7d72245]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so [0xb7e27d04]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__lock_detect+0x129) [0xb7e268bf]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__lock_get_internal+0x1434) [0xb7e23504]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__lock_vec+0x167) [0xb7e20d3c]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__db_lget+0x45c) [0xb7e80fe2]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so [0xb7d7a0a5]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so [0xb7d766ce]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__dbc_get+0x598) [0xb7e6ba29]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(__dbc_get_pp+0x11f) [0xb7e7c0ec]
    /usr/local/BerkeleyDB.4.7/lib/libdb_cxx-4.7.so(_ZN3Dbc3getEP3DbtS1_j+0x3f) [0xb7d66feb]
    ...(user code stack layers)...
    /lib/tls/i686/cmov/libpthread.so.0 [0xb79a950f]
    /lib/tls/i686/cmov/libc.so.6(clone+0x5e) [0xb7a9ea0e]
    Here is the output of the waits for table:
    Waiter:     Waiting on:
    80002249/50:     957608
    6/50:     80002249 957208
    Waitsfor array
    Waiter:     Waiting on:
    8000224a/28:     957288
    a/28:     8000224a 957608
    Waitsfor array
    Waiter:     Waiting on:
    8000224c/22:     957608
    6/22:     8000224c 957368
    Waitsfor array
    Waiter:     Waiting on:
    8000224c/22:     957608
    6/22:     8000224c 957368
    4/22:     8000224c 957048
    Waitsfor array
    Waiter:     Waiting on:
    8000224d/25:     957528
    7/25:     8000224d 957608
    Waitsfor array
    Waiter:     Waiting on:
    8000224d/25:     957528
    7/25:     8000224d 957608
    6/25:     8000224d 957208
    Waitsfor array
    Waiter:     Waiting on:
    8000224d/25:     957528
    7/25:     8000224d 957608
    6/25:     8000224d 957208
    4/25:     8000224d 957288
    Waitsfor array
    Waiter:     Waiting on:
    80002251/10:     957048
    a/10:     80002251 957128
    Waitsfor array
    Waiter:     Waiting on:
    80002257/39:     80002254 957368
    80002254/39:     957288
    Waitsfor array
    Waiter:     Waiting on:
    80002258/14:     957048
    4/14:     80002258 957608
    Waitsfor array
    Waiter:     Waiting on:
    80002258/14:     957048
    8/14:     80002258 957208
    4/14:     80002258 957608
    Waitsfor array
    Waiter:     Waiting on:
    80002259/39:     80002257 957448
    80002258/14:     957048
    80002257/39:     957368
    8/14:     80002258 957208
    4/14:     80002258 957608
    Waitsfor array
    Waiter:     Waiting on:
    80002259/39:     957448
    8/39:     80002259 957288
    7/39:     80002259 957048
    6/39:     80002259 957528
    Waitsfor array
    Waiter:     Waiting on:
    80002259/0:     0
    80002257/0:     0
    Waitsfor array
    Waiter:     Waiting on:
    8000225a/40:     956968
    7/40:     8000225a 956888
    Waitsfor array
    Waiter:     Waiting on:
    8000225a/40:     956968
    7/40:     8000225a 956888
    4/40:     8000225a 957608
    It looks like there is a similar pattern here at the end with the two entries waiting on zero ("80002259/0:     0 80002257/0:     0"), which is why I suspect its the same error just manifesting itself in a slightly different manner.
    I am using lock detection, I currently have it set to use DB_LOCK_MAXLOCKS, I have tried others as well with not change in behavior.
    Most of my transaction code takes the following form, so if the get is successful (the id exists) then the data is updated and written back to the database. If an exception is thrown then it is caught and the transaction is aborted. As far as I can tell this should not leave and transaction handles left open or anything along those lines as the error message suggests.
    void do_txn()
    DbTxn *txn = NULL;
    try
    db->txn_begin(NULL, &txn, 0);
    Data d;
    //does a db->get using the txn pointer and a DB_RMW lock
    //returns do_get returns false if the id does not exist
    bool success = do_get(id, &d, txn);
    if( !success )
    txn->abort();
    return;
    //update data as necessary
    //does a db->put using the txn pointer
    do_put(id, &d, txn);
    txn->commit(0);
    } catch (DbDeadlockException &de) {
    cout << de.what() << endl;
    if( txn != NULL )
    txn->abort();
    }

  • Passing collection or array to function: shallow copy or not?

    Hello all,
    When passing a collection or an array to a function, what is most used practice to store the given collection in some class member field: reference-copy or a shallow copy?
    I believe it's frustrating to see that there is no real 'standard' about this: some developers copy the whole collection/array, others prefer to simply copy the reference. I tend to agree with the latter group:
    - it's the user's responsibility if he wants the class to use a copied version instead of the 'original' version of the collection/array.
    - most of the times, a copy is not needed, and therefor a shallow copy is pure waste of resources.
    - when nothing is known about the given collection, and there are also no restrictions on the collection to be given, it could be everything: ArrayList? HashSet? SortedSet? AnUnknownImplementation?
    reference copy:
    class ComeClass
        private ArrayList list;
        public SomeClass(ArrayList list) { this.list = list; }
    }shallow copy:
    class ComeClass
        private List list;
        public SomeClass(List list) { this.list = new ArrayList(list); }
    }What are your thoughts about this?

    basically, it's how much access you want to provides.
    public SomeClass(ArrayList list) { this.list = list; }to me, if some other class has a reference to the list object and start modifying it..Someclass internal data (the list) is also modified...thus breaking encapsulation.
    yes, in multithread enviroment, sharing the list(by reference can lead to some hard bug to debug), so it;s much safer to copy (clone) the list. Java does this and provides you the MultiCaster class to make thing easier for you.
    even with synchronization, there are issues you have to look at (such as deadlock)..you can synchronize the list, but can still run into a dead lock
    list is synchronized in class A
    class B have object (that's in list and call the list (wait due to class A synchronization)
    list tries to invoke method of class B, but wait (class B is lock)..thus..deadlock.
    it's a crued exapmle..but i think you get the point.
    the saying is that to make a safer application..declare all you object immutable or restrict access to your private data. Of course, performance wise, we have to comprise sometime.
    example..a list with over 500,000 objects..you might not want to to clone the reference.

Maybe you are looking for