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. Crash in multiple threading with shared resource

Crash in multiple threading with shared resource

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmindata-structuresdebuggingtutorialquestion
5 Posts 5 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.
  • I Offline
    I Offline
    includeh10
    wrote on last edited by
    #1

    several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.

    M _ E T 4 Replies Last reply
    0
    • I includeh10

      several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You HAVE to use synchronization if you have multiple threads accessing the same object if that object is being altered!!!!! You can use a critical section[^] or a mutex[^] to ensure only one thread can use the protected object at a time.

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • I includeh10

        several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Put the code that accesses the shared resources between the EnterCriticalSection and LeaveCriticalSection APIs.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        1 Reply Last reply
        0
        • I includeh10

          several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.

          E Offline
          E Offline
          Eugen Podsypalnikov
          wrote on last edited by
          #4

          See also :) :

          class CUserInfo;

          typedef CUserInfoArray CArray;

          class CUserSyncArray : protected CUserInfoArray
          {
          CCriticalSection m_cLock;

          public:
          CUserSyncArray() {..}
          virtual ~CUserSyncArray() {..}

          // Beginn of all needed CArray methods:
          CUserInfo* GetAt(INT_PTR iPos) {
          CSingleLock cExceptionFreeLock(&m_cLock, true);
          return CUserInfoArray::GetAt(iPos);
          }
          //..and so on, for all needed methods :)
          };

          They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

          1 Reply Last reply
          0
          • I includeh10

            several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            includeh10 wrote:

            reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array.

            In continuation with Superman :-) you can also utilize service of Events or semaphore also. the benefits of semaphore ( you can solve reader writer problem using it)

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
            Never mind - my own stupidity is the source of every "problem" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

            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