开发者

Basics Question: Best Practices concerning Posix Threads and Dynamic Memory

开发者 https://www.devze.com 2022-12-10 04:36 出处:网络
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 application

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 t开发者_Python百科o fall for the beginner traps.


For a nice source of "common wisdon" check out Improving Performance through Threads. Although the article starts from obvious stuff, it has nice and easy description of thread design patterns.
Also make sure to read Concurrent Data Structures which should give you an idea how to arrange your data storage.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消