无法推送到由静态映射<结构 ,队列>创建的队列;

发布于 2024-12-09 02:47:03 字数 1137 浏览 3 评论 0原文

所以基本上我有一个静态映射将我的结构映射到队列,但是当我尝试推送到队列时,它无法推送任何内容。

struct SignalID_t {
    unsigned int lock;
    unsigned int cond;

    SignalID_t(unsigned int lock_, unsigned int cond_):lock(lock_), cond(cond_) {}
};

class SignalIDComp {
public:
bool operator()(const SignalID_t& a, const SignalID_t& b) const
    {return a.lock == b.lock && a.cond == b.cond;}
}; 

class ThreadManager{
public:

static std::queue<ucontext_t *>& waitingQ(SignalID_t id)
    {return threadsWaitingOnConditions[id];}

private:
    static std::map<SignalID_t, std::queue<ucontext_t*>> threadsWaitingOnConditions;
}

map<SignalID_t, queue<ucontext_t *>, SignalIDComp>    ThreadManager::threadsWaitingOnConditions;

class Threads{

void thread_wait(int lock, int cond){
    SignalID_t id(lock, cond);
    ThreadManager::waitingQ(id).push(ThreadManager::getRunningThread());
    if (ThreadManager::waitingQ(id).empty())
       cout << "failed to push thread to cond waiting Q" << endl;
}
}

在我尝试推送到队列的函数中,我立即测试队列以查看它是否为空,并且它总是显示它是空的。我怀疑这可能与地图是静态的并且队列未正确初始化这一事实有关,但我似乎无法修复它。

So basically I have a static map that maps my struct to a queue, but when I try to push to the queue, it fails to push anything.

struct SignalID_t {
    unsigned int lock;
    unsigned int cond;

    SignalID_t(unsigned int lock_, unsigned int cond_):lock(lock_), cond(cond_) {}
};

class SignalIDComp {
public:
bool operator()(const SignalID_t& a, const SignalID_t& b) const
    {return a.lock == b.lock && a.cond == b.cond;}
}; 

class ThreadManager{
public:

static std::queue<ucontext_t *>& waitingQ(SignalID_t id)
    {return threadsWaitingOnConditions[id];}

private:
    static std::map<SignalID_t, std::queue<ucontext_t*>> threadsWaitingOnConditions;
}

map<SignalID_t, queue<ucontext_t *>, SignalIDComp>    ThreadManager::threadsWaitingOnConditions;

class Threads{

void thread_wait(int lock, int cond){
    SignalID_t id(lock, cond);
    ThreadManager::waitingQ(id).push(ThreadManager::getRunningThread());
    if (ThreadManager::waitingQ(id).empty())
       cout << "failed to push thread to cond waiting Q" << endl;
}
}

In the function where I try to push to the queue, I immediately test the queue to see if it's empty and it always cout's that it is. I suspect that it may have something to do with the fact that the map is static and the queue isn't getting initialized properly, but I can't seem to fix it.

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

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

发布评论

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

评论(1

不疑不惑不回忆 2024-12-16 02:47:03

您有两个独立的、不幸的是名称相同的对象:

map_type ThreadManager::threadsWaitingOnConditions;

并且

map_type threadsWaitingOnConditions;

您混淆了这两个对象。最终检查应该说:

if (ThreadManager::threadsWaitingOnConditions[id].empty()) { /* ... */ }

或类似的内容。鉴于静态成员映射是私有的,您可能应该说,

if (ThreadManager::waitingQ(id).empty()) { /* ... */ }

我不明白您对全局映射对象有什么需要。看来没有必要。或者更确切地说,您可能意味着它是静态成员对象的定义,在这种情况下,您应该这样写:

map<SignalID_t, std::queue<ucontext_t*>> ThreadManager::threadsWaitingOnConditions;
//                                       ^^^^^^^^^^^^^^^

You have two separate, and unfortunately identically-named, objects:

map_type ThreadManager::threadsWaitingOnConditions;

and

map_type threadsWaitingOnConditions;

You're confusing the two. The final check should say:

if (ThreadManager::threadsWaitingOnConditions[id].empty()) { /* ... */ }

or something to that effect. Given that the static member map is private, you should probably just say,

if (ThreadManager::waitingQ(id).empty()) { /* ... */ }

I don't understand what need you have with the global map object. It seems unnecessary. Or rather, you probably mean it to be the definition of the static member object, in which case you should write it like this:

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