I am using the boost thread library but I am a new user. I have written some code but I am still at the planning stage of my program.

I first set up two serial port connections using:
Code:
port1 = open(portName1, O_RDWR | O_NOCTTY | O_NONBLOCK);
this->port = port1;
port2 = open(portName2, O_RDWR | O_NOCTTY | O_NONBLOCK);

Then I produce two threads. They share port2 a global variable.

Thread A reads from serial port 1 continuously and prints what it receives.
Thread B reads from serial port 2. When thread B has a full packet received it writes a counter to serial port 1 and then increments a counter.
The device attached to serial port 1 writes what it receives so that thread A receives what thread B sends.

To read, thread A uses:
Code:
n = read(this->port, serialBuffer, ASERIAL_BUFFER_SIZE);

To read, thread B uses:
Code:
n = read(port2, serialBuffer, ASERIAL_BUFFER_SIZE);
To write, thread B uses:
Code:
n = write(port1, &(this->counter),2);
this->counter = this->counter + 1;
I want thread B to write preferably without having to wait for thread A to stop reading. Is this possible? I want to know if I need to use a mutex.

Thanks