Greetings all,
I am working with a co-worker on a problem where some times, but not all of the times, our printf() output is duplicated. When the problem occurs we always get the duplicate lines one after the other.
For example:
[size=75][color=darkred]Output line 1
Output line 1
Output line 2
Output line 2
Output line 3
Output line 4[/size]
We have a multi-threaded applications where some output is coming from one thread and other output is comfing from another thread. From the example above, lines 1, 3 and 4 are from thread A and line 2 is from thread B.
We are using round robin scheduling. So it is possible, and probable that the threads are pre-empting each other once a timeslice completes.
A co-worker has a theory that printf is not thread safe. That we are being preempted in the middle of the printf and is somehow leading to the duplicated output.
This just does not seem likily to me. First the QNX documentation indicates that printf() is thread safe. And considering that QNX’s main line of buisiness is a multi-threaded environment, I could not imagine something so common and simple as this occuring – but who knows.
I have added code to validate that we are not invoking the functions twice, by using a static global that gets printed and incremented in the print statement. So if it was truly called twice the globals value would increment.
I have also added code to display the thread id so we can confirm that this is truly coming from the same thread.
I am baffled by this.
Can anyone suggest other debugging methods? Has anyone else encoutered this before? Is my co-worker correct that printf() doesn’t work correclty in a multi-threaded environment?
I have replicated this problem with QNX on a power PC board, as well as with the x86 vmWare QNX simulation.
Thanks in advance for your time.
-=John