On 2012-09-21,
<> wrote:
> Now my application has a separate thread for polling. But this causes faults
> in the data read from the serial device. Is it correct that this is because
> of the multithreading? Or should this function normaly?
No, it's because you have a bug in your program. Of course threads can be used
to read from serial ports.
> What would be a good solution:
> 1) Create separate thread which reads all the data and store these in a struct or something. Functions reads the structs instead of serial port.
Your serial driver already does the same thing. You achieve nothing by
repeating it. There is no advantage in those threads reading the structs
instead of the serial port.
The best way to use a threads for input is to have that thread perform the
input, and then in that same thread, so the processing of that input.
The processing code should be executable by any thread, rather than
a decidated thread.
> 2) Communication between the functions and the thread which communicates with
> the serial port.
This is not different from 1 in any fundamental way. Communication means
putting data into structures that another thread reads.
> - Any other good solutions?
Use one thread to write a single event loop based on polling descriptors
with poll or select. Then you don't have to debug multithreading problems.