Boost logo

Boost :

Subject: Re: [boost] [interprocess] SHARING_VIOLATION due to antivirus
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-08-19 08:30:48


Lars Hagstrom wrote:
>
> I made a quick patch to create_file in win32_api.hpp (the patch is
> rather verbose, to make its workings obvious):
>
> static inline void *create_file(const char *name, unsigned
> long access,
> unsigned long creation_flags, unsigned long attributes = 0)
> {
> int tries = 0;
> for (;;)
> {
> void * const res = CreateFileA(name, access,
> file_share_read | file_share_write | file_share_delete,
> 0, creation_flags, attributes, 0);
> if (tries > 100)
> {
> //ran out of attempts, return and let the exceptions fly
> return res;
> }
> if (res != invalid_handle_value)
> {
> //all went well, return
> return res;
> }
> else if (GetLastError() != 32)
> {
> //we got some other error than an ERROR_SHARING_VIOLATION,
> //let the caller handle the error.
> return res;
> }
>
> Sleep(1);
> ++tries;
> }
> }

I think this would be more readable and do the same:

static inline void *
create_file(const char * name, unsigned long access,
   unsigned long creation_flags, unsigned long attributes = 0)
{
   for (int attempt(0); attempt < 100; ++attempt)
   {
      void * const handle(
         CreateFileA(name, access,
            file_share_read | file_share_write | file_share_delete,
            0, creation_flags, attributes, 0));
      bool const invalid(invalid_handle_value == handle);
      if (!invalid)
      {
         return handle;
      }
      bool const sharing_violation(32 == GetLastError());
      if (!sharing_violation)
      {
         return handle;
      }
      Sleep(1);
   }
   return invalid_handle_value;
}

This makes the magic number more obvious, too (assuming it were to remain a magic number).

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk