在C语言中实现类似Go语言风格的通道(channels)

huangapple go评论100阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2013年12月14日 22:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/20584691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定