CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2008
    Posts
    28

    Exclamation STL hashmap,vector crashing

    hi all,

    i have a service application, which maintains a record of each and every USB being used in the windows system.

    in brief, my problem is with the hashmap values getting corrupted in between two threads. one thread picks the database path where values are to be written and other thread writes into that database path.

    Service crashes at the, when the below codes are executed.



    WaitForSingleObject(*hThreadIterBegin->second,INFINITE);

    rename(hThreadIterBegin->first->c_str(),newFileName.c_str());

    ReleaseMutex(*hThreadIterBegin->second);



    The first thread fetches the values and the values are sent to the second thread so that it can write the necessary things to the fetched value which is the path of the database. but the hashmap value crashes at some point due to which the service crahses at above code executions.



    i am not able to identify the exact reason, as to why the value of ThreadMutexMapPrimary changes or crashes when it is called from the second thread???

    i am attaching the code below, with declarations and values i received while debugging.

    Thankyou.
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: STL hashmap,vector crashing

    Containers are typically not thread safe out of their own. This is because most of the time it will just add overhead.

    You will need to assure proper synchronised access to any variable/class instance that is read from one (or more) thread and is being written to from one other (or more) thread.
    Any such variable typically also needs to be marked as volatile or the compiler could optimise away necessary changes other threads need to see.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured