C++库实现Go的goroutines或Go的channels?

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

C++ libraries that implement Go goroutines or Go channels?

问题

我正在处理一个中等规模的C++代码库,目前完全是单线程的。然而,现在是时候追求并发和并行性以获得性能提升了。我对Google的Go编程语言的并发模型非常感兴趣,它具有非常轻量级的goroutines和一套通信通道系统。

不幸的是,出于各种完全合理的原因,该项目需要保持在C++中。所以我的问题是:是否有一个C++库可以近似Go的并行性范式?具体而言,是否有一个近似goroutines或go通道的C++库可用?我的备选计划只是使用boost::thread。

所涉及的应用是一个长时间运行的专有模拟程序,用于金融预测领域。它通常是CPU绑定的,但在新数据可用时也会被IO阻塞。其中许多计算与先前的结果无关,可以相对容易地并行运行。能够在分布式环境中运行应用程序是一个长期目标,但不是需要立即解决的问题。

英文:

I'm working with a medium-sized C++ code base which is currently entirely single-threaded. The time has come, however, to pursue concurrency and parallelism for performance gains. I'm very interested in the concurrency model of Google's Go programming language, with very lightweight goroutines and a system of communicating channels.

Sadly, for a variety of perfectly valid reasons, the project needs to stay in C++. So my question is: Is there a C++ library that approximates the Go paradigm for parallelism? Specifically, is there an approximation of goroutines or go channels available for C++? My fallback plan is just to use boost::thread.

The application in question is a long-running proprietary simulation for the financial forecasting domain. It's usually CPU bound, but also gets blocked on IO when new data become available. Many of the computations involved are not dependent on previous results and could be fairly easily made to run in parallel. Being able to run the application in a distributed context is a long-term goal, but not one that needs to be solved immediately.

答案1

得分: 7

如果你的目标主要是加速计算,Intel的TBB(线程构建块)是一个比从boost::thread自己编写的劣质版本更好的选择(在我看来)。

英文:

If your aim is primarily speeding up compute things, Intel's TBB (Threading Building Blocks) is (IMHO) a better option than rolling your own inferior version from boost::thread.

答案2

得分: 5

这个问题和一般的谷歌搜索“C++协程”应该能找到一些相关内容。这个SO问题建议尝试使用Boost::coroutine。

如果你不介意使用C语言,你也可以尝试使用libtask。这个库是由Russ Cox(Go官方开发团队的成员之一)在开始开发Go之前编写的。不过我只在C中使用过它,所以不知道它是否适用。

顺便说一下,Go的通道是通过锁定队列实现的,所以你也可以尝试使用常规线程来实现类似的机制。

英文:

This question and in a general a google search for "C++ coroutines" should get you something close. The SO question suggests trying Boost::coroutine.

If you don't mind wrapping C you might be able to try libtask. Which was written by Russ Cox (one of the official Go dev team) before work on Go began. I've only used it in C though, so I don't know if it's applicable.

Go channels are implemented as locking queues by the way, so you might be able to incorporate a similar mechanism using regular threads.

答案3

得分: 2

尝试使用GBL库,它拥有一切:协程(纤程),线程,同步和异步处理程序 - 而且都是现代C++。

英文:

Try GBL library, it has everything: coroutines (fibers), threads, sync and async handlers -- and it's all modern C++.

答案4

得分: 2

libgolang提供了go和包括工作的select在内的通道。以下是示例用法:

      chan<int> ch = makechan<int>(); // 创建新通道
      go(worker, ch, 1);              // 生成worker(chan<int>, int)
      ch.send(1)
      j = ch.recv()

      _ = select({
          _default,       // 0
          ch.sends(&i),   // 1
          ch.recvs(&j),   // 2
      });
      if (_ == 0)
          // 选择了默认情况
      if (_ == 1)
          // 选择了case 1:i发送到ch
      if (_ == 2)
          // 选择了case 2:j从ch接收到

      defer([]() {
          printf("离开...\n");
      });

      if (<错误条件>)
          panic("错误");

go根据激活的运行时,生成线程或基于gevent的协程。

英文:

libgolang provides go and channels including working select. Here is example usage:

      chan<int> ch = makechan<int>(); // create new channel
      go(worker, ch, 1);              // spawn worker(chan<int>, int)
      ch.send(1)
      j = ch.recv()

      _ = select({
          _default,       // 0
          ch.sends(&i),   // 1
          ch.recvs(&j),   // 2
      });
      if (_ == 0)
          // default case selected
      if (_ == 1)
          // case 1 selected: i sent to ch
      if (_ == 2)
          // case 2 selected: j received from ch

      defer([]() {
          printf("leaving...\n");
      });

      if (<bug condition>)
          panic("bug");

go spawns either a thread or gevent-based coroutine depending on activated runtime.

答案5

得分: 2

coost

一个类似于Go风格的C++协程库。

void fun() {
    std::cout << "hello world" << std::endl;
}

go(fun);
go([]() {
    std::cout << "hello world" << std::endl;
});

它还提供了以下功能:

  • co::event
  • co::mutex
  • co::pool
  • co::chan(类似于Go语言中的channel)
  • co::wait_group(类似于Go语言中的sync.WaitGroup)
英文:

coost

A go-style C++ coroutine library.

void fun() {
    std::cout &lt;&lt; &quot;hello world&quot; &lt;&lt; std::endl;
}

go(fun);
go([]() {
    std::cout &lt;&lt; &quot;hello world&quot; &lt;&lt; std::endl;
});

It also provides:

  • co::event
  • co::mutex
  • co::pool
  • co::chan (similar to channel in golang)
  • co::wait_group (similar to sync.WaitGroup in golang)

答案6

得分: 1

从我目前所见,cilk似乎与Go的并行风格非常相似。看起来Intel已经购买了它,并提供了商业支持,名为Intel Cilk Plus

英文:

From what I have seen so far, cilk seems to be quite similar to go's style of parallelism. It looks like Intel has bought it and provides commercial support with Intel Cilk Plus.

huangapple
  • 本文由 发表于 2010年12月16日 04:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/4454444.html
匿名

发表评论

匿名网友

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

确定