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. How to access a single object from multiple thread

How to access a single object from multiple thread

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialannouncement
7 Posts 2 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.
  • R Offline
    R Offline
    Ranojay
    wrote on last edited by
    #1

    :doh: Hello friends I am using VC++ 6.0 for writing a multi threaded application. But I am facing problems when I try to Update a recordset (CRecordset object) from multiple threads running simultaneously please help Ranojay

    I 1 Reply Last reply
    0
    • R Ranojay

      :doh: Hello friends I am using VC++ 6.0 for writing a multi threaded application. But I am facing problems when I try to Update a recordset (CRecordset object) from multiple threads running simultaneously please help Ranojay

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      Have a look at critical sections - they could be your friend. CreateCriticalSection EnterCriticalSection LeaveCriticalSection DestroyCriticalSection (? - could be delete) Good luck, Iain.

      R 1 Reply Last reply
      0
      • I Iain Clarke Warrior Programmer

        Have a look at critical sections - they could be your friend. CreateCriticalSection EnterCriticalSection LeaveCriticalSection DestroyCriticalSection (? - could be delete) Good luck, Iain.

        R Offline
        R Offline
        Ranojay
        wrote on last edited by
        #3

        Thank you Sir for your help But the functions EnterCriticalSection LeaveCriticalSection are giving error messages while in use can you be more specific about how to use them thanks again

        I 1 Reply Last reply
        0
        • R Ranojay

          Thank you Sir for your help But the functions EnterCriticalSection LeaveCriticalSection are giving error messages while in use can you be more specific about how to use them thanks again

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          I have *never* had one of those functions giving me an error. The only thing I can think of is that you've not initialised the critical section before using it. Look on MSDN for EnterCriticalSection. One of the links take you to an example page for critical sections. (Copied below) I would avoid the use of a global variable, and make the critical section a co-member of a class with your recordset, but I hope you'll get the idea. Iain.

          // Global variable
          CRITICAL_SECTION CriticalSection;

          void main()
          {
          ...

          // Initialize the critical section one time only.
          InitializeCriticalSection(&CriticalSection); 
          
          ...
          
          // Release resources used by the critical section object.
          DeleteCriticalSection(&CriticalSection)
          

          }

          DWORD WINAPI ThreadProc( LPVOID lpParameter )
          {
          ...

          // Request ownership of the critical section.
          \_\_try 
          {
              EnterCriticalSection(&CriticalSection); 
          
              // Access the shared resource.
          }
          \_\_finally 
          {
              // Release ownership of the critical section.
              LeaveCriticalSection(&CriticalSection);
          }
          
          ...
          

          }

          R 1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            I have *never* had one of those functions giving me an error. The only thing I can think of is that you've not initialised the critical section before using it. Look on MSDN for EnterCriticalSection. One of the links take you to an example page for critical sections. (Copied below) I would avoid the use of a global variable, and make the critical section a co-member of a class with your recordset, but I hope you'll get the idea. Iain.

            // Global variable
            CRITICAL_SECTION CriticalSection;

            void main()
            {
            ...

            // Initialize the critical section one time only.
            InitializeCriticalSection(&CriticalSection); 
            
            ...
            
            // Release resources used by the critical section object.
            DeleteCriticalSection(&CriticalSection)
            

            }

            DWORD WINAPI ThreadProc( LPVOID lpParameter )
            {
            ...

            // Request ownership of the critical section.
            \_\_try 
            {
                EnterCriticalSection(&CriticalSection); 
            
                // Access the shared resource.
            }
            \_\_finally 
            {
                // Release ownership of the critical section.
                LeaveCriticalSection(&CriticalSection);
            }
            
            ...
            

            }

            R Offline
            R Offline
            Ranojay
            wrote on last edited by
            #5

            Thanks again I will try that. Actaully I am using the CWinThread Run() function from a CWinThread derived class and doing the simultaneous update of the CRecordSet object and not AfxBeginthread function will that work too? anyway thank you very much for your help and I will keep you disturbing Thanks a lot

            I 1 Reply Last reply
            0
            • R Ranojay

              Thanks again I will try that. Actaully I am using the CWinThread Run() function from a CWinThread derived class and doing the simultaneous update of the CRecordSet object and not AfxBeginthread function will that work too? anyway thank you very much for your help and I will keep you disturbing Thanks a lot

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              The code I showed was just an example - mostly for initialising the critical section. Any code wrapped up by a Enter/LeaveCriticalSection pair will be callable from only one thread at a time. This includes any calls you make to a database, etc. It doesn't matter whether you make a thread using CWinThread::Run, AfxBeginThread, or ::CreateThread (mostly because they all end up being a call to BeginThread). Iain.

              R 1 Reply Last reply
              0
              • I Iain Clarke Warrior Programmer

                The code I showed was just an example - mostly for initialising the critical section. Any code wrapped up by a Enter/LeaveCriticalSection pair will be callable from only one thread at a time. This includes any calls you make to a database, etc. It doesn't matter whether you make a thread using CWinThread::Run, AfxBeginThread, or ::CreateThread (mostly because they all end up being a call to BeginThread). Iain.

                R Offline
                R Offline
                Ranojay
                wrote on last edited by
                #7

                Sir I tried all the functions but it is still not working some rows are surely getting updated but some of them are not the error Mesage is No rows were affected by the update or delete operation. has it any thing to do with the type of database I am using.Actually I am uising MS-ACCESS database. may be ACCESS is not able to handle concurrent updates

                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