volatile
bitfields must not be used as synchronization primites #58
Labels
No labels
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: Telodendria/Cytoplasm#58
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
struct HttpServer
definesstop
andisRunning
as 1-bitvolatile
bitfields, presumably used as flags with atomic access.However, the use of
volatile
variables on a multi-threaded environment is a wrong assumption. In C99, platform-specific primitives, such aspthread_mutex_t
, must be used to protect variable access so that the operations are thread-safe.If
Cytoplasm
is meant to keep using a multi-threaded HTTP server, these bitfields must be then replaced with thread-safe and portable alternatives.I actually am not so sure this is a big issue, because those variables are only ever modified by the main thread; it's not like they're being modified asynchronously. They're only written by one thread and then read by the others. So as far as multithreaded access goes, it's really not a problem. We aren't going to get any weird behavior due to context switches as far as I can tell.
In the case where the variable changes while it's being read by a thread, the worst that can happen is that one more loop happens, but that's not the end of the world because the new value will be picked up on the next iteration.
Locking a mutex every time we check for the shutdown condition seems like it would add a lot of overhead and somewhat kill the concurrency benefits of multithreading.
I think that if so, then the HttpServer functions should have a note explaining that the start/stop primitives should be handled by the main thread.