Is it advisable or even possible to have a dynamically growing array of structs fed and read by different concurrently running posix threads? Where do I have to look for best practices for applications of that sort - are there any which are "common wisdom"? I am new to this area and would need some initial pointers for where to start and what to look out for. It seems that a lot is possible here, I don't want to fall for the beginner traps.
views:
109answers:
2
+1
A:
Sounds like you're describing a thread-safe stack or queue. Doing a web search for the term "thread-safe" may get you started.
In general, you protect your array with a mutex: The mutex is unlocked by default, and when a thread accesses (modifies or reads) the structure it first locks the mutex, makes its modifications and unlocks the mutex. If another thread needs to access the data structure while the mutex is locked, it will block until the thread that locked the mutex unlocks it.
iWerner
2009-11-02 12:27:48
I would say: "when a thread modifies _or reads_ the structure it first locks the mutex". You need some sort of locking, even if it can be a shared lock, to access the data structure. Otherwise, someone could get a write-lock and modify it while you are reading it.
Thomas Padron-McCarthy
2009-11-02 12:40:56
Thanks. I've edited it
iWerner
2009-11-02 12:53:59