Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Thread Problem

Thread Problem

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Priyank Bolia
    wrote on last edited by
    #1

    1.)I have a data source that is continously modified, i.e items added, deleted, changed. I used a static global mutex object to actually allow only one thread to modify it. Now I have a list ctrl that shows that data, the refreshing is done in a different thread. Now in that thread:

    	DeleteAllItems();
    	for(unsigned int i=0; i
    

    No what if I have a list of 10 elements and I deleted some of them when this thread is running. As the compiler will store the value of m_aryCall.Count() in some temporary variable and when its asks for 10th element, which is not there. 2.)In Modify Data Block a create a thread by new and run it, now the threads are running and the ModifyData is called many times when the previous threads is running. This actually creates memory leak as the thread is not deleting itself. [http://www.priyank.in/](http://www.priyank.in/)

    S H 2 Replies Last reply
    0
    • P Priyank Bolia

      1.)I have a data source that is continously modified, i.e items added, deleted, changed. I used a static global mutex object to actually allow only one thread to modify it. Now I have a list ctrl that shows that data, the refreshing is done in a different thread. Now in that thread:

      	DeleteAllItems();
      	for(unsigned int i=0; i
      

      No what if I have a list of 10 elements and I deleted some of them when this thread is running. As the compiler will store the value of m_aryCall.Count() in some temporary variable and when its asks for 10th element, which is not there. 2.)In Modify Data Block a create a thread by new and run it, now the threads are running and the ModifyData is called many times when the previous threads is running. This actually creates memory leak as the thread is not deleting itself. [http://www.priyank.in/](http://www.priyank.in/)

      S Offline
      S Offline
      SunKnight0
      wrote on last edited by
      #2

      I am not sure I understand 100% of what you are trying to do, but it seems to me that you have a mumber of threads modifying the data (and using a Global Mutex to make sure they do it one at a time) and a number of threads reading the data. If that is the case just use the same Mutex for the reading threads also. That way your data is not being modified as another thread is tryting to read it.

      1 Reply Last reply
      0
      • P Priyank Bolia

        1.)I have a data source that is continously modified, i.e items added, deleted, changed. I used a static global mutex object to actually allow only one thread to modify it. Now I have a list ctrl that shows that data, the refreshing is done in a different thread. Now in that thread:

        	DeleteAllItems();
        	for(unsigned int i=0; i
        

        No what if I have a list of 10 elements and I deleted some of them when this thread is running. As the compiler will store the value of m_aryCall.Count() in some temporary variable and when its asks for 10th element, which is not there. 2.)In Modify Data Block a create a thread by new and run it, now the threads are running and the ModifyData is called many times when the previous threads is running. This actually creates memory leak as the thread is not deleting itself. [http://www.priyank.in/](http://www.priyank.in/)

        H Offline
        H Offline
        HumanOsc
        wrote on last edited by
        #3

        Priyank Bolia wrote: I used a static global mutex object to actually allow only one thread to modify it The problem is, you like to directly modify the control from different threads... You should only modify controls from the main thread (or the thread which created the control)... When you like to directly modify it from an other thread, it comes to unexpected situations like memory leaks or a deadlock of the app... You should only modify the controls from different threads with the usage of PostMessage... Communicate from the worker thread(s) with the main thread only with PostMessage and custom defined user messages to provide an save ui access... like: http://www.codeproject.com/threads/usingworkerthreads.asp Best regards ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups