C++实现 Go Goroutines 或 Go Channels 的库?

发布于 2024-10-07 23:54:49 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼眸印温柔 2024-10-14 23:54:49

如果您的目标主要是加速计算速度,英特尔的 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.

谜泪 2024-10-14 23:54:49

这个问题以及一般的谷歌搜索“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.

故事与诗 2024-10-14 23:54:49

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(类似于golang中的channel)
  • co::wait_group(类似于golang中的sync.WaitGroup)

coost

A go-style C++ coroutine library.

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

go(fun);
go([]() {
    std::cout << "hello world" << 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)
拥醉 2024-10-14 23:54:49

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

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

养猫人 2024-10-14 23:54:49

libgolang 提供 go和渠道,包括工作选择。以下是示例用法:

      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 根据激活的运行时生成线程或基于 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.

淡淡的优雅 2024-10-14 23:54:49

从我目前所见, cilk 似乎与 go 的并行风格非常相似。看起来英特尔已经收购了它并通过Intel Cilk Plus<提供商业支持/a>.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文