CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Location
    Brazil - São Paulo
    Posts
    62

    ReadFile() interferes in WriteFile()

    Hello,

    I have an multi-threaded application that uses the APIs ReadFile() and WriteFile() to access the serial port. One thread reads periodically the serial port and the other writes upon an event.

    What I have found is that when a ReadFile() operation is in progress, the WriteFile() operation waits until the first one completes!!! Even in separated threads...

    Does anyone have any idea of how to solve this???

    Thanks in advance,
    Wagner

  2. #2
    Join Date
    Aug 2004
    Posts
    184

    Re: ReadFile() interferes in WriteFile()

    You might try wrapping your read and write calls inside a CriticalSection.....


    MSDN Sample
    Code:
    // Global variable
    CRITICAL_SECTION CriticalSection; 
    
    void main()
    {
        ...
    
        // Initialize the critical section one time only.
        if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x80000400) ) 
            return;
        ...
    
        // Release resources used by the critical section object.
        DeleteCriticalSection(&CriticalSection)
    }
    
    DWORD WINAPI ThreadProc( LPVOID lpParameter )
    {
        ...
    
        // Request ownership of the critical section.
        EnterCriticalSection(&CriticalSection); 
    
        // Access the shared resource.
    
        // Release ownership of the critical section.
        LeaveCriticalSection(&CriticalSection);
    
        ...
    }
    Last edited by f1shrman; September 3rd, 2004 at 06:16 PM.

  3. #3
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567

    Re: ReadFile() interferes in WriteFile()

    Originally quoted by wschalch
    Does anyone have any idea of how to solve this???
    Overlapped IO

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured