基础问题:有关 Posix 线程和动态内存的最佳实践
由不同的并发运行的 posix 线程提供和读取动态增长的结构数组是否可取甚至可能?我必须在哪里寻找此类应用程序的最佳实践 - 有没有“常识”?我是这个领域的新手,需要一些初步指导来了解从哪里开始以及要注意什么。看来这里有很多可能,我不想落入初学者的陷阱。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获得“共同智慧”的良好来源,请查看通过线程提高性能。
尽管本文从显而易见的内容开始,但它对线程设计模式进行了很好且简单的描述。
另请务必阅读并发数据结构,其中应该让您了解如何安排数据存储。
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.