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. atomic file write

atomic file write

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
6 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.
  • D Offline
    D Offline
    Dave_
    wrote on last edited by
    #1

    I have a simple server app that uses WritePrivateProfileString() when a client changes the status of a user. I want to protect against multiple 'change status' requests resulting in clashing file writes. The clients communicate with the server using UDP and the server uses a listener socket. The listener socket uses 'OnReceive()' to call a function to write to the data file. I've been using a temp file to write to and then delete the working file then rename the temp file. It works most of the time but once in a while the main data file is deleted and there is no temp file. It's a bug but I'm wondering if there is a better way? Thank you.

    R 1 Reply Last reply
    0
    • D Dave_

      I have a simple server app that uses WritePrivateProfileString() when a client changes the status of a user. I want to protect against multiple 'change status' requests resulting in clashing file writes. The clients communicate with the server using UDP and the server uses a listener socket. The listener socket uses 'OnReceive()' to call a function to write to the data file. I've been using a temp file to write to and then delete the working file then rename the temp file. It works most of the time but once in a while the main data file is deleted and there is no temp file. It's a bug but I'm wondering if there is a better way? Thank you.

      R Offline
      R Offline
      Rajasekharan Vengalil
      wrote on last edited by
      #2

      Does each client get serviced on a separate thread by the server? If yes, you'll need to use some kind of synchronization object while writing to the file. A CRITICAL_SECTION might do nicely for you.

      -- gleat http://blogorama.nerdworks.in[^] --

      Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG

      D 1 Reply Last reply
      0
      • R Rajasekharan Vengalil

        Does each client get serviced on a separate thread by the server? If yes, you'll need to use some kind of synchronization object while writing to the file. A CRITICAL_SECTION might do nicely for you.

        -- gleat http://blogorama.nerdworks.in[^] --

        Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG

        D Offline
        D Offline
        Dave_
        wrote on last edited by
        #3

        I'm using a single instance of the listener socket. Would that be enough to ensure the file is only written by one client at a time?

        R 1 Reply Last reply
        0
        • D Dave_

          I'm using a single instance of the listener socket. Would that be enough to ensure the file is only written by one client at a time?

          R Offline
          R Offline
          Rajasekharan Vengalil
          wrote on last edited by
          #4

          What do you do when your "accept" (or the equivalent Windows socket extensions API) call returns? Do you launch another thread to respond to the client socket returned by "accept"? If you are doing it on the same thread as the one that invokes "accept" then you shouldn't require synchronization; in which case the problem lies elsewhere.

          -- gleat http://blogorama.nerdworks.in[^] --

          Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG

          D 1 Reply Last reply
          0
          • R Rajasekharan Vengalil

            What do you do when your "accept" (or the equivalent Windows socket extensions API) call returns? Do you launch another thread to respond to the client socket returned by "accept"? If you are doing it on the same thread as the one that invokes "accept" then you shouldn't require synchronization; in which case the problem lies elsewhere.

            -- gleat http://blogorama.nerdworks.in[^] --

            Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG

            D Offline
            D Offline
            Dave_
            wrote on last edited by
            #5

            I have a pointer to the server object (the object that created the listener socket), and I call a function in that server object. No additional threads. Thank you for your time on this.

            R 1 Reply Last reply
            0
            • D Dave_

              I have a pointer to the server object (the object that created the listener socket), and I call a function in that server object. No additional threads. Thank you for your time on this.

              R Offline
              R Offline
              Rajasekharan Vengalil
              wrote on last edited by
              #6

              OK. You might want to conduct a little experiment. From your "OnReceive" handler, trace the return value of "GetCurrentThreadId" to a log and have multiple clients connect simultaneously to the server. Now inspect the log to see if the values logged are the same for all the clients. Also, is there a code snippet that you can post that shows how you are doing the file processing?

              -- gleat http://blogorama.nerdworks.in[^]

              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