Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法更新类内字段的值_C++_Multithreading_Message Queue - Fatal编程技术网

C++ 无法更新类内字段的值

C++ 无法更新类内字段的值,c++,multithreading,message-queue,C++,Multithreading,Message Queue,此函数用于从消息容器中删除过期消息 void Broker::removeExpiredMessages(){ while(true){ std::this_thread::sleep_for(std::chrono::milliseconds(1000)); messageMut.lock(); std::cout<<Broker::getMessages().size()<<std::endl;

此函数用于从消息容器中删除过期消息

void Broker::removeExpiredMessages(){
    while(true){
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        messageMut.lock();
        std::cout<<Broker::getMessages().size()<<std::endl;
        for(auto& i : Broker::getMessages()){
            if(i.second.getHeader().expireAfter <= 0){
                std::cout<<"del"<<std::endl;
                Broker::getMessages().erase(i.first);
            }
           else
             i.second.getHeader().setExpireAfter(i.second.getHeader().getExpireAfter()-1);
        }
        messageMut.unlock();
    }
}
Broker.hpp

class Message{
public:
    struct Header{
        std::string time;
        int expireAfter;
        std::string topicName;

        int getExpireAfter() const {
            return expireAfter;
        }

        void setExpireAfter(int expireAfter) {
            this->expireAfter = expireAfter;
        }

        const std::string& getTime() const {
            return time;
        }

        void setTime(const std::string &time) {
            this->time = time;
        }

        const std::string& getTopicName() const {
            return topicName;
        }

        void setTopicName(const std::string &topicName) {
            this->topicName = topicName;
        }
    };
    Message(){}
    void setPayload(std::string _payload){
        this->payload = _payload;
    }
    std::string getPayload()const{
        return this->payload;
    }

    void setHeader(const std::string& _time, const int _expireAfter, const std::string _topicName){
        this->header.setExpireAfter(_expireAfter);
        this->header.setTime(_time);
        this->header.setTopicName(_topicName);
    }

    Header getHeader()const{
        return this->header;
    }

private:
    Header header;
    std::string payload;
};
class Broker{
public:
    static bool isSubscriberLoopRunning;
    Broker(){std::cout<<"Broker()"<<std::endl;}
    static std::queue<const char*>& getMessageQueue() {
        return messageQueue;
    }

    static std::unordered_map<const char*, Message>& getMessages() {
        return messages;
    }

    static std::vector<Subscriber>& getSubscriberList() {
        return subscriberList;
    }

    static void pushNewSubscriber(Subscriber&);
    static void receiveMessageFromPublisher(const Message&);
    static void sendToSubscriber();
    static void removeSubscriber(const Subscriber&);
    static void removeExpiredMessages();
private:
    static std::unordered_map<const char*,Message> messages;
    static std::queue<const char*> messageQueue;
    static std::vector<Subscriber> subscriberList;
};
类代理{
公众:
静态bool-issubscriberloop运行;

Broker(){std::cout您需要它来返回引用

Header &getHeader() const{
    return this->header;
}

它现在是按值返回,而不是按引用返回。这就是过期时间没有减少的原因!

要返回引用,需要使用此选项

Header &getHeader() const{
    return this->header;
}

它是按值返回,而不是现在引用。这就是为什么过期时间没有递减。

请提取A并将其作为您问题的一部分。注意它是完整的和最小的,您的既不是最小的也不是完整的。作为一个新的用户,也可以读取和读取。在codereview.stackexchange.com上提交审查时,我已经发现了一些no-no。我马上想到的是,在遍历容器时,您正在从容器中删除一个元素。这通常是no-no,但根据
std::unordered_-map::erase
操作只会使刚刚创建的迭代器无效所以我想这很好。只是不和谐。
std::this\u thread::sleep\u for(std::chrono::millides(1000))
看起来很可疑。每当我看到显式睡眠(或类似睡眠)时,我的第一个想法是“这里有个bug”。这有什么意义?哦,等等,不,在潜在地删除它之后,你立即访问
i
。不,不,不。@UlrichEckhardt好的,我会记住的。请提取a并将其作为你问题的一部分。请注意它是完整的和最小的,你的既不是最小的,也不是完整的。作为一个新用户,也接受和rea而且,一旦你有了这个运行,考虑在CODReVIEW.STACKExchange .COM中备案它,我就可以在那里发现一些NOS了。我马上跳出来的是,你在重复这个容器的过程中,从容器中删除一个元素。这通常是“否”,但是根据<代码> STD::unOrdEdSMAP::擦除< /代码> OPER。初始化只会使刚刚删除的迭代器无效。所以我想这很好。只是不和谐。
std::this\u thread::sleep\u for(std::chrono::millides(1000))
看起来可疑。每当我看到显式睡眠(或类似情况)时,我的第一个想法是“这里有一个bug”.这有什么意义?哦,等等,不,你在潜在地删除它之后立即访问
i
。不,不,不,不。@UlrichEckhardt好的,我会记住这一点