在 C++ 中使用标准串行调度队列?

发布于 2024-12-10 20:21:21 字数 226 浏览 4 评论 0原文

我想编写一个玩具应用程序,该应用程序将从 1 个实例开始并不断增长,直到达到 5fps。该实例有一个 run() 方法,运行时间应小于 1 毫秒。因此,我想使用串行调度队列来接受任务并执行它们,而不是实际的线程。我有一台多核机器,如果可以避免它,我不想锁定。我不想编写串行调度队列,但是有我可以使用的 C++(0x?) 标准串行调度队列吗?

任务可能会将自己添加回来,或者队列可能只是在每个元素之间循环。这对我来说并不重要

I want to write a toy app that will start with 1 instance and grow until it hits 5fps. The instance have a run() method that should take <1ms to run. So instead of actual threads i'd like to use a serial dispatch queue to take task and execute them. I have a multicore machine and i would not like locking if i could avoid it. I dont want to write a serial dispatch queue but is there a C++(0x?) standard serial dispatch queue i can use?

The task may add themselves back in or the queue could just loop between each element. It doesnt matter to me

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

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

发布评论

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

评论(1

夜未央樱花落 2024-12-17 20:21:21

如果您想使用标准队列,但没有锁定,那么请寻找无锁队列实现。 C++11 中没有标准的无锁队列。

为什么不使用每个核心/线程的任务向量并在它们之间循环,而不是使用单个队列?例如,如果您有 4 个 CPU 和线程,则为每个 CPU 提供一个任务向量。在它们之间平均分配任务,然后按照您在最后一句循环注释中提到的方式迭代它们。它没有锁定,并且具有与来自多个线程的队列相同的处理顺序保证(即没有)。

If you want to use a standard queue, but without locking, then look for a lock-free queue implementation. There is no standard lock-free queue in C++11.

Instead of a single queue, why not use a vector of tasks per core/thread and cycle between them? For example, if you have 4 CPU and threads, have a vector of tasks for each one. Distribute the tasks equally between them and then just iterate over them as you mention in your looping comment in the last sentence. That'd have no locking and would have the same processing order guarantees (i.e. none) that a queue from multiple threads would have.

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