Is a libmodbus context really thread-safe? #246

Closed
mhei opened this Issue Feb 20, 2015 · 4 comments

Comments

Projects
None yet
5 participants
@mhei
Contributor

mhei commented Feb 20, 2015

Documentation states:

A libmodbus 'context' is thread safe and may be shared among as many application
threads as necessary, without any additional locking required on the part of the caller.

I wonder whether this is really the case (any longer). In struct _modbus there are fields which are struct timeval for example, which consists of two sub-fields which are updated in two steps. And both sub-fields are non-atomic types, thought one can assume that updating one field is atomic, can't we?

I also wonder whether is is really necessary or important, that libmodbus is thread-safe? Modbus itself is a master-slave protocol, so there should be no parallelism. Maybe anybody could illustrate an usage scenario where multiple threads are involved?

@fengyikil

This comment has been minimized.

Show comment
Hide comment
@fengyikil

fengyikil Jun 4, 2015

I tested it ,and found many "ERROR Connection timed out: " , and when I add pthread_mutex_t, it can work. so it seems not thread-safe,or I misunderstand.

I tested it ,and found many "ERROR Connection timed out: " , and when I add pthread_mutex_t, it can work. so it seems not thread-safe,or I misunderstand.

@stephane stephane closed this in b7247f1 Jun 9, 2015

@stephane

This comment has been minimized.

Show comment
Hide comment
@stephane

stephane Jun 9, 2015

Owner

Thank you for the report

Owner

stephane commented Jun 9, 2015

Thank you for the report

georgidimov added a commit to georgidimov/libmodbus that referenced this issue Jul 24, 2017

@bigzwey

This comment has been minimized.

Show comment
Hide comment
@bigzwey

bigzwey Jan 19, 2018

I find it useful to create multiple servers simultaneously on one machine, each on a different port and using different logic and/or registers. It seems possible that some of the function calls, pointed to by the context, could be interrupted partway through.

bigzwey commented Jan 19, 2018

I find it useful to create multiple servers simultaneously on one machine, each on a different port and using different logic and/or registers. It seems possible that some of the function calls, pointed to by the context, could be interrupted partway through.

@pboettch

This comment has been minimized.

Show comment
Hide comment
@pboettch

pboettch Jan 19, 2018

Contributor

@bigzwey Interrupted by what? Libmodbus is a library and does not contain any shared-between-threads-data in its context(s). (If it does it would be a bug).

If you use libmodbus in your threaded code it is up to you to take care of the thread-safety by using mutexes when accessing shared-memory (for example the modbus_ctx).

Contributor

pboettch commented Jan 19, 2018

@bigzwey Interrupted by what? Libmodbus is a library and does not contain any shared-between-threads-data in its context(s). (If it does it would be a bug).

If you use libmodbus in your threaded code it is up to you to take care of the thread-safety by using mutexes when accessing shared-memory (for example the modbus_ctx).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment