Full duplex on win32 api

Hey just a beginner here to the win32 API so hopefully you'll bare with me :)

Programing in c++ with the win32 API on windows 10, to make a serial comm program to communicate with an Atmel microcontroller. I have been wondering if you can make a call to ReadFile and WriteFile at the same time, so as to be full duplex. Let's say i have a separate thread for writing and reading, would it be possible to read and write at the exact same time? Or do i have to pause the other thread while one is reading/writing?
That is using the:
#include <thread>

Thanks in advance, Benji.
you can't read and write in same time from 2 different threads.

Either thread needs to be guarded with mutex or critical section while reading or writing.

#include <mutex>

static std::mutex my_mutex;


// do read or write operations here

Ok. Why is this? Is the serial port just not capable of reading and writing at the same time?

In any case i think I'll have a bool flag trigger just before read/write, and if the other thread try's to do the opposite I'll make it wait in a while loop.
serial port may be capable because of multiple pins but you have just one file not multiple files.

win32 API ex. WriteFile is thread safe, it will not write to file until that file is freed.

You'll get ERROR_IO_PENDING error instead, but file won't be written and read in exactly the same time.

What behavior do you expect if 2 threads modify same data within file in same time? won't that result is corruption?

Sorry for slow reply!
I guess i just assumed that when you use it for serial com port interfacing, readfile and writefile pushed data to two discrete registers, not one file. What do you mean by file? As in a memory address in the system that gets buffered before it gets pushed to the actual pins? Is there any way you can have two file's, so that you could write and read at the same time?
I see, let's try again..

If you create a "file" with CreateFile API, you need to "or" 2 flags for dwShareMode parameter:
Enables subsequent open operations on a file or device to request read and write access.

And you also need to specify FILE_FLAG_OVERLAPPED
If this flag is specified, the file can be used for simultaneous read and write operations.


You then use ReadFile and WriteFile to do simultaneous read and write on file that may be anything supported by API.

How does this work internally, the API manages file access in asyncronous way that is you don't need to work with mutexes, to verify this behavior call GetLastError()
The GetLastError code ERROR_IO_PENDING is not a failure; it designates the read operation is pending completion asynchronously.

This means read and write in same is not exactly in same time down to nano second unless in case of serial ports the API works differently, you said serial com interfacing works by pushing data to registers so syncronization may work slightly differently..

but I'm not expert for serial ports and registers so this is all I can say about win32 API and how to use it.
Last edited on
Ok that clears it up a bit! Thanks for the help!
Topic archived. No new replies allowed.