Fubini
January 24th, 2012, 04:52 AM
Hello all,
I've got a question for an advanced Linux dev! I'm writing a shared library for multi-threading in multiple processes within a single system.
I want all processes linking to my shared library to have access to a few global variables that will be shared and arbitrarily modified by other processes.
A simple use-case of this would be to maintain a count of all running processes that are currently linked against the shared library.
My current thought is that whenever a new process runs it will execute a method that looks something like:
volatile libraryStarted;
//Get library-global version of libraryStarted
//If no currently running process has linked against the library
//then it should be FALSE
if ( ! libraryStarted ){
//Initialize shared data structures for library
libraryStarted = TRUE;
} else { //Library has already been started
//Get shared data
}
Of course, with the appropriate locks and whatnot. Each individual piece of shared data shouldn't need to be larger than a size_t, as it's no problem to write thread-safe data structures.
Any suggestions are greatly appreciated! I recognize there are a LOT of sticky issues to consider (e.g. thread crashing) but I'm just trying to get an idea of the mechanisms that could work here.
I've got a question for an advanced Linux dev! I'm writing a shared library for multi-threading in multiple processes within a single system.
I want all processes linking to my shared library to have access to a few global variables that will be shared and arbitrarily modified by other processes.
A simple use-case of this would be to maintain a count of all running processes that are currently linked against the shared library.
My current thought is that whenever a new process runs it will execute a method that looks something like:
volatile libraryStarted;
//Get library-global version of libraryStarted
//If no currently running process has linked against the library
//then it should be FALSE
if ( ! libraryStarted ){
//Initialize shared data structures for library
libraryStarted = TRUE;
} else { //Library has already been started
//Get shared data
}
Of course, with the appropriate locks and whatnot. Each individual piece of shared data shouldn't need to be larger than a size_t, as it's no problem to write thread-safe data structures.
Any suggestions are greatly appreciated! I recognize there are a LOT of sticky issues to consider (e.g. thread crashing) but I'm just trying to get an idea of the mechanisms that could work here.