英文:
Go-style channels in C
问题
对于一个实时的C语言DSP程序,我需要类似Go风格的通道来在线程之间进行通信:
- 一个线程向通道中推送令牌
- 另一个线程从通道中拉取令牌
- 通道具有缓冲区
- 在我的情况下,令牌的内存占用很小(由数字组成的小向量)
我想知道是否使用传统的流(streams)是一个好主意,如果不是,是否有一种良好的实践和/或库可以尽可能接近Go通道的功能?
英文:
For a real time DSP program in C, I need something like Go-style channels to communicate between threads:
- One thread pushes tokens on the channel
- Another thread pulls tokens from the channel
- The channel has a buffer
- In my case, tokens have a small memory footprint (small vectors of numbers)
I was wondering if using good old streams was a good idea, or not. If not, is there a good practice and/or a library to get something as close as Go channels as possible?
答案1
得分: 1
你所描述的是一个线程安全的队列。
Apache项目有一个实现(http://apr.apache.org/docs/apr-util/1.3/apr__queue_8h.html)。
除此之外,很多用户实际上分享了他们的pthread
队列实现,假设这是你正在使用的。
英文:
What you're describing describes a thread-safe queue.
The Apache Project has one implementation.
Other than that, a lot of users have actually shared their pthread
queue implementations, assuming that was what you're using.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论