开发者

Looking for a C or C++ library providing a functionality similar to Google Go's channels [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

...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++.


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:

  • ACE: http://www.cs.wustl.edu/~schmidt/ACE.html
  • Poco: http://pocoproject.org/


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 :)


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*


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.


"A Channel is a buffered or unbuffered queue for fixed–size messages" (plan9 thread).
There is a buffered queue in the TBB: concurrent_bounded_queue.
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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜