英文:
Looking for a C or C++ library providing a functionality similar to Google Go's channels
问题
...用于多线程网络服务器。
我想在多个线程之间传递数据。目前我正在使用套接字,主线程在select()上阻塞,工作线程在recv()上阻塞,但我觉得可能有更高级或预打包的方法来处理这个任务在C++中。
英文:
...for use in a multithreaded network server.
I want to pass data around between multiple threads. Currently I'm using sockets, with the master thread blocking on select() and workers blocking on recv(), though I feel there probably are more advanced or prepackaged ways of handling this task in C++.
答案1
得分: 4
我会在线程池中有工作线程等待。
然后主线程等待选择(用于读和写)。
当数据到达时,主线程将作业添加到线程池中。每个作业添加后,一个线程会被唤醒执行该作业,然后返回到线程池。这样,您不会阻塞在特定端口上等待recv()的线程,并且一组固定的子线程可以处理所有传入的流量。
目前支持此功能的库有:
- ACE:http://www.cs.wustl.edu/~schmidt/ACE.html
- Poco:http://pocoproject.org/
英文:
I would have worker threads waiting in a thread pool.
Then the master waiting on select (for both reads and writes).
As data comes the master adds jobs to the thread pool. As each job is added a thread wakes up executes the job and returns to the pool. This way you are not blocking threads waiting on specific ports with recv() and a fixed set of child threads can handle all incoming traffic.
Currentl libs that support this functionality in ready made objects:
答案2
得分: 4
libthread来自plan9port,其中包括一个名为Channel的结构体,它与go语言中的channel非常相似;请注意Russ Cox对plan9port和go-lang的贡献,以及libthread的历史:
Luca Cardelli和Rob Pike将CSP的思想发展成了用于生成用户界面代码的Squeak迷你语言[4]。(这个Squeak与Squeak Smalltalk实现不同。)Pike随后将Squeak扩展为完整的编程语言Newsqueak [5][6],它衍生出了Plan 9的Alef [7][8],Inferno的Limbo [9]和Google的Go [13]。
在Plan 9的发展历史中的某个时候,维护两种语言的基础设施变得过于费力,因此Alef被停用,并将CSP构造移植到C语言中,形成了libthread。
因此,由于go语言的channel实际上是从libthread直接继承而来的,我认为你不会找到更相似的东西
英文:
libthread from plan9port includes a Channel struct that will be very similar; take note of Russ Cox's contribution to both plan9port and go-lang, and the libthread history:
> Moving in a different direction, Luca Cardelli and Rob Pike developed
> the ideas in CSP into the Squeak mini-language [4] for generating user
> interface code. (This Squeak is distinct from the Squeak Smalltalk
> implementation.) Pike later expanded Squeak into the fully-fledged
> programming language Newsqueak [5][6] which begat Plan 9's Alef [7]
> [8], Inferno's Limbo [9], and Google's Go [13].
At a later point in Plan 9's history, it became too much effort to maintain infrastructure for two languages, so Alef was discontinued and the CSP constructs ported to C in the form of libthread.
So, since go channels are essentially a direct descendent from libthread, I don't think you'll find anything more similar
答案3
得分: 2
你可以尝试使用ACE库,该库提供了适用于线程间通信的管道和消息队列。
ACE代表自适应通信环境
英文:
You can try the ACE library which ships with pipes and message queues which are specially suited for inter-thread communication.
*ACE stands for Adaptive Communication Environment
答案4
得分: 1
也许ZeroMQ值得一试。它有一个'inproc'通道,可以在线程之间进行通信。当然,你只能在线程之间发送字符串,而不能发送对象,但另一方面,它支持其他传输方式,如TCP/IP(因此可以在网络上轻松地在进程之间进行通信),它是跨平台的,并且有大多数当前语言的语言绑定。
英文:
Maybe ZeroMQ might be worth checking out. It has an 'inproc' channel which allows you to communicate between threads. Of course, you can only send strings between threads, not objects, but on the other hand it supports other transports like TCP/IP (so you can easily communicate between processes on a network), is cross platform and has language bindings for most current languages.
答案5
得分: 0
“通道是一个用于固定大小消息的缓冲或非缓冲队列”(plan9 thread)。
TBB中有一个缓冲队列:concurrent_bounded_queue。
我刚刚在C++11中实现了一种非缓冲通道:https://gist.github.com/ArtemGr/7293793。尽管更通用的实现方式是创建一对引用(就像Felix中的mk_ioschannel_pair),每个通道端点一个引用,以便在通道的另一端不再存在时中断任何等待。
英文:
"A Channel is a buffered or unbuffered queue for fixed–size messages" (plan9 thread).<br>
There is a buffered queue in the TBB: concurrent_bounded_queue.<br>
And I've just implemented a kind of unbuffered Channel in C++11: https://gist.github.com/ArtemGr/7293793. Although a more generic implementation would be to create a pair of references (like in the Felix mk_ioschannel_pair), one for each endpoint of the channel, in order to interrupt any waiting in case the other end of the channel no longer exists.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论