简体   繁体   English

选择STL容器来存储线程

[英]Choosing an STL container to store threads

I am trying to choose the best STL container to hold Thread objects (I am writing a thread library). 我正在尝试选择最好的STL容器来保存Thread对象(我正在编写一个线程库)。 My problem is that I'm not very familiar with any of them, and while reading the api helps, I would like to consult someone who had used it before. 我的问题是我对它们中的任何一个都不是很熟悉,在阅读api有帮助的时候,我想咨询以前使用它的人。

Anyway - every Thread object has two important attributes: _id and _priority . 无论如何 - 每个Thread对象都有两个重要的属性: _id_priority I need to be able to access a thread by _id , so I naturally thought of a hash_map. 我需要能够通过_id访问一个线程,所以我自然会想到一个hash_map。 I also want the objects to be sorted by _priority (different Thread objects can have the same priority), so I thought of a priority queue with pointers to the hash_map, but if I delete a thread who's not first on the queue it gets a little ugly. 我还希望通过_priority对对象进行排序(不同的Thread对象可以具有相同的优先级),所以我想到了一个带有指向hash_map的指针的优先级队列,但是如果我删除了一个不在队列中的第一个线程,它会得到一点点丑陋。

Is there a better solution? 有更好的解决方案吗? Thanks! 谢谢!

To get two types of access you either need to combine two containers... or reuse a library that combines containers for you. 要获得两种类型的访问,您需要组合两个容器...或者重用一个为您组合容器的库。

Boost.MultiIndex was invented for precisely this kind of needs. Boost.MultiIndex是为了满足这种需求而发明的。

The basics page shows an example that has employees accessible by id (unique) and sorted by name (non-unique), which is pretty much what you are going for. 基本页面显示了一个示例,其中的员工可通过id(唯一)访问并按名称排序(非唯一),这几乎就是您的目的。

The key extractors are perhaps not obvious. 关键的提取器可能并不明显。 Supposing that your thread ressemble: 假设您的线程重新组合:

class Thread {
public:
    std::size_t id() const;
    std::size_t priority() const;

    ...
};

You should be able to write: 你应该写:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/const_mem_fun.hpp>
#include <boost/multi_index/member.hpp>

// define a multiply indexed set with indices by id and name
typedef multi_index_container<
    Thread,
    indexed_by<
        ordered_unique<
            const_mem_fun<Thread, std::size_t, &Thread::id>
        >,
        ordered_non_unique<
            const_mem_fun<Thread, std::size_t, &Thread::priority>
        >
    > 
> ThreadContainer;

Which defines a container of thread uniquely identified by their id() and sorted according to their priority() . 它定义了一个由其id()唯一标识并根据其priority()排序的线程容器。

I encourage you to play around with the various indexes. 我鼓励你玩各种索引。 Also, if you provide friend access to your class or specific getters that return mutable references, then using mem_fun instead of const_mem_fun you will be able to update your objects in place (for example, change their priority). 此外,如果您提供朋友访问您的类或返回可变引用的特定getter,那么使用mem_fun而不是const_mem_fun您将能够更新您的对象(例如,更改其优先级)。

It's a very complete (if daunting) library. 这是一个非常完整(如果令人生畏)的图书馆。

Best solution is possibly std::map ,, providing you a key / value pair. 最好的解决方案可能是std::map ,为您提供键/值对。 In your scenario the key has the type of your _id and the value the type Thread (assuming this is the name of your class). 在您的场景中,键具有_id的类型和值类型Thread (假设这是您的类的名称)。 By copying all values to a std::vector you can sort by _priority with std::sort and a predicate. 通过将所有值复制到std::vector您可以使用_prioritystd::sort和谓词进行std::sort

一个简单的解决方案是保持std::unordered_map提供密钥 - >线程查找,然后使用std::set来实现优先级队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM