Home > Software engineering >  Serializing err.h calls in a mulit-threaded program
Serializing err.h calls in a mulit-threaded program

Time:04-27

My program uses the <err.h> functions (like warnx(3)) to emit diagnostics. Sometimes multiple threads do the reporting at the same time, resulting in the output lines overlapping.

It is not a big deal, but if there is an easy fix, I'd like to implement it... Is there?

(The threading is all managed by OpenMP.)

CodePudding user response:

The simplest solution to avoid the interleaved printed warnings is to use an OpenMP critical section. To do that, one can use the directive #pragma omp critical in a wrapping function replacing the warnx calls.

Note that stdio/stderr accesses should already be locked, that being said, regarding the kind of operations done on it, the lock are not guaranteed to cut lines or block of lines the way you want. This is especially true if you do multiple calls to warnx for example, but also if an implementation of warnx use multiple calls to fprintf (as you pointed out in the comments).

  • Related