遇到std :: mutex和std :: thread的问题

时间:2016-02-22 14:56:02

标签: c++ multithreading c++11 ncurses

我遇到了std :: thread和std :: mutex的问题,无法让两者相互配合得很好。在过去的几个小时里,我一直把头发拉过来,只是无法取得任何进展。我相信这不属于我的技能领域。代码如下:

void GekkoFyre::TuiHangouts::gui_userRosterListNav(std::shared_ptr<WINDOW> display,
                                                   std::vector<const char *> userList,
                                                   const short &menuItem)
{
    while (rosterEnabled) {
        for (short i = 0; i < totalMenuItems; ++i) {
            short diff = (menuItem - i);
            if (i < numMenuItems) {
                if (i == menuItem) {
                    size_t msgLen = strlen(userList.at(i));
                    size_t l = 0;
                    if (msgLen > subMaxX) {
                        for (l = subMaxX; l < msgLen; ++l) {
                            // Scroll the message from left to right, then vice versa, so that it fits within
                            // the designated window.
                            std::this_thread::sleep_for(std::chrono::milliseconds(500));
                            rosterListMutex.lock();
                            wattron(usrSubWin.get(), A_REVERSE); // Highlight selection
                            const char *msg = userList.at(i);
                            mvwaddstr(usrSubWin.get(), i, 0, &msg[(msgLen - l)]);
                            wrefresh(usrSubWin.get());
                            touchwin(usrSubWin.get());
                            rosterListMutex.unlock();
                        }
                    } else {
                        rosterListMutex.lock();
                        wattron(usrSubWin.get(), A_REVERSE); // Highlight selection
                        mvwaddstr(usrSubWin.get(), i, 0, userList.at(i));
                        wrefresh(usrSubWin.get());
                        touchwin(usrSubWin.get());
                        rosterListMutex.unlock();
                    }
                }

                wattroff(usrSubWin.get(), A_REVERSE); // Remove highlight

                if ((i + 1) < numMenuItems) {
                    mvwaddstr(usrSubWin.get(), (i + 1), 0, userList.at((i + 1)));
                }
            } else if (diff < (totalMenuItems - numMenuItems) && diff > 0) {
                // Allow the scrolling of a username list, from downwards and then back upwards, so that
                // the user may see the list in its entirety.
                wclear(usrSubWin.get());
                int pos = 0;
                for (short c = diff; c < (numMenuItems + diff); ++c) {
                    ++pos;
                    mvwaddstr(usrSubWin.get(), pos, 0, userList.at(c));
                }
                pos = 0;
                break;
            }
        }

        rosterListMutex.lock();
        wattroff(usrSubWin.get(), A_REVERSE); // Remove highlight
        touchwin(usrSubWin.get());
        wrefresh(usrSubWin.get());
        wrefresh(display.get());
        rosterListMutex.unlock();
    }
}

我正在尝试在NCurses的聊天窗口左侧显示用户列表,如下面的[1]中所示,尽管所述屏幕截图中没有任何用户。我想保留我的隐私:)当你向下滚动列表时,在滚动过去几个之后,闪烁开始在两个用户名之间来回发生。他们一遍又一遍地反复选择对方。我相信这是线程无法正常同步的原因。该功能实现如下:

[1] - http://imgur.com/ZZlFHg2

        #define WIDGET_USERS_LIST 1
        short menuItem = 0;
        int ch = 0;
        int curr_widget = 0;
        std::thread rosterListNav1(&GekkoFyre::TuiHangouts::gui_userRosterListNav, this, userListWin, rosterFormatted, menuItem);
        rosterListNav1.detach();

        while ((ch = wgetch(display.get())) != KEY_F(12)) {
            switch (ch) {
            case KEY_DOWN:
                if (curr_widget == WIDGET_USERS_LIST && rosterEnabled) {
                    ++menuItem;
                    if (menuItem > totalMenuItems - 1) {
                        menuItem = 0;
                    }
                }
                break;
            case KEY_UP:
                if (curr_widget == WIDGET_USERS_LIST && rosterEnabled) {
                    --menuItem;
                    if (menuItem < 0) {
                        menuItem = totalMenuItems - 1;
                    }
                }
            }

            std::thread rosterListNav2(&GekkoFyre::TuiHangouts::gui_userRosterListNav, this, userListWin, rosterFormatted, menuItem);
            rosterListNav2.detach();
        }

对此问题的任何帮助都会非常感激,我相信我已将std :: mutex放在正确的区域。我真的很难过这个问题。还要注意,虽然我知道一些交易技巧,但我完全是自学成才。对于经历过大学的程序员来说,一些正常的命名法对我来说是完全无法理解的。

0 个答案:

没有答案
相关问题