What are the guidelines to write thread-safe UNIX code in C and C++?
I know only a few:
What others are there?
The simple thing to do is read a little. The following list contains some stuff to look at and research.
static variables, and any shared dynamically allocated memory.pthread_cond_init and related functions.Once you understand the basics, learn about the common problems so that you can identify them when they occur:
It really comes down to shared state, globals and static local are examples of shared state. If you don't share state, you won't have a problem. Other examples of shared state include multiple threads writing to a file or socket.
Any shared resource will need to be managed properly - that might mean making something mutex protected, opening another file, or intelligently serializing requests.
If two threads are reading and writing from the same struct, you'll need to handle that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With