Click here to Skip to main content
14,218,216 members

C / C++ / MFC

 
GeneralRe: Passing / modifying char* to/ by function. Pin
CPallini16-Dec-18 22:56
mveCPallini16-Dec-18 22:56 
GeneralRe: Passing / modifying char* to/ by function. Pin
Richard MacCutchan16-Dec-18 23:01
protectorRichard MacCutchan16-Dec-18 23:01 
GeneralRe: Passing / modifying char* to/ by function. Pin
Vaclav_17-Dec-18 3:26
memberVaclav_17-Dec-18 3:26 
GeneralRe: Passing / modifying char* to/ by function. Pin
Richard MacCutchan17-Dec-18 3:50
protectorRichard MacCutchan17-Dec-18 3:50 
GeneralRe: Passing / modifying char* to/ by function. Pin
leon de boer17-Dec-18 18:48
memberleon de boer17-Dec-18 18:48 
AnswerRe: Passing / modifying char* to/ by function. Pin
Joe Woodbury17-Dec-18 8:20
professionalJoe Woodbury17-Dec-18 8:20 
GeneralRe: Passing / modifying char* to/ by function. Pin
Vaclav_17-Dec-18 11:20
memberVaclav_17-Dec-18 11:20 
Questioneliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 4:23
memberAlexander Kindel16-Dec-18 4:23 
I'm cleaning up a chess program of mine, which includes a clock feature. For those unfamiliar with chess clocks, both players start the game with a certain amount of time, and each of their clocks counts down when and only when it's currently their turn. The program has GUI, and the displays for the timers have to be prompted to redraw every time a second flips.

I had previously implemented this by having a timer thread that stores the time when a turn starts, then constantly re-checks the time in a loop, using this to update the active player's time on every iteration. This worked like a charm, but I understand that spinning like that is bad practice for wasting cycles, so I tried redoing it using wait_until(), roughly like this:

HWND g_mainWindowHandle;
std::condition_variable g_timerConditional;
std::mutex g_timerLock;
std::atomic<bool>g_turnIsOver;
std::atomic<std::chrono::nanoseconds>g_blackPlayerNanosecondsLeft;
std::atomic<std::chrono::nanoseconds>g_whitePlayerNanosecondsLeft;

void turnTimer(std::atomic<std::chrono::nanosecondsNanosecondsLeft>*activePlayerNanosecondsLeft, 
    Rect*displayRect)
{    
    std::chrono::nanoseconds currentNanosecondsLeft{ activePlayerNanosecondsLeft->load() };
    std::chrono::nanoseconds nanosecondsToNextSecond{ currentNanosecondsLeft % 1000000000 };
    std::chrono::steady_clock::time_point nextSecondBoundary{
        std::chrono::steady_clock::now() + nanosecondsToNextSecond };
    std::unique_lock<std::mutex>lock(g_timerLock);    
    std::cv_status status{
        g_timerConditional.std::condition_variable::wait_until(lock, nextSecondBoundary) };
    while (!g_turnIsOver.load())
    {        
        if (status == std::cv_status::timeout)
        {
            currentNanosecondsLeft -= nanosecondsToNextSecond;
            *activePlayerNanosecondsLeft = currentNanosecondsLeft;
            nanosecondsToNextSecond = std::chrono::nanoseconds(1000000000);
            nextSecondBoundary += nanosecondsToNextSecond;
            InvalidateRect(g_mainWindowHandle, displayRect, FALSE);
        }
        status = g_timerConditional.std::condition_variable::wait_until(lock, nextSecondBoundary);
    }
    currentNanosecondsLeft -= 
        std::chrono::steady_clock::now() - (nextSecondBoundary - nanosecondsToNextSecond);
    *activePlayerNanosecondsLeft = currentNanosecondsLeft;
    g_turnIsOver = false;
    InvalidateRect(g_mainWindowHandle, displayRect, FALSE);
}


Each time the main thread moves a piece, it sets g_turnIsOver to true, calls g_timerConditional.notify_one(), calls join() on the current timer thread, and starts a new timer thread for the new active player. While this approach seems to work well for the majority of moves, for the rest there is a generous fraction of a second of lag between clicking to make a move and seeing the board update. I'm quite confident the timer code is the culprit in some capacity. Is this likely to be an example of what the reference for wait_until() means when it says "the function also may wait for longer than until after timeout_time has been reached due to scheduling or resource contention delays"? Is there a way to fix it, or will the entire approach of using wait_until() not work?

modified 16-Dec-18 10:48am.

AnswerRe: eliminating lag from a chess clock Pin
Daniel Pfeffer16-Dec-18 4:43
professionalDaniel Pfeffer16-Dec-18 4:43 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 7:09
memberAlexander Kindel16-Dec-18 7:09 
GeneralRe: eliminating lag from a chess clock Pin
Randor 16-Dec-18 11:56
professional Randor 16-Dec-18 11:56 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 14:51
memberAlexander Kindel16-Dec-18 14:51 
GeneralRe: eliminating lag from a chess clock Pin
Randor 16-Dec-18 23:09
professional Randor 16-Dec-18 23:09 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel18-Dec-18 14:41
memberAlexander Kindel18-Dec-18 14:41 
GeneralRe: eliminating lag from a chess clock Pin
Daniel Pfeffer18-Dec-18 20:27
professionalDaniel Pfeffer18-Dec-18 20:27 
QuestionWiFi Notifications [SOLVED] Pin
Richard Andrew x6414-Dec-18 16:13
professionalRichard Andrew x6414-Dec-18 16:13 
QuestionVC++ Word automation issues on Win 10 Pin
narasingubhanu14-Dec-18 2:25
membernarasingubhanu14-Dec-18 2:25 
AnswerRe: VC++ Word automation issues on Win 10 Pin
Richard MacCutchan14-Dec-18 4:47
protectorRichard MacCutchan14-Dec-18 4:47 
GeneralRe: VC++ Word automation issues on Win 10 Pin
narasingubhanu14-Dec-18 19:04
membernarasingubhanu14-Dec-18 19:04 
QuestionInitializing struct (in C++) Pin
Vaclav_12-Dec-18 6:22
memberVaclav_12-Dec-18 6:22 
AnswerRe: Initializing struct (in C++) Pin
leon de boer12-Dec-18 6:28
memberleon de boer12-Dec-18 6:28 
QuestionList HDD files in CListView Pin
_Flaviu11-Dec-18 1:22
member_Flaviu11-Dec-18 1:22 
AnswerRe: List HDD files in CListView Pin
Richard MacCutchan11-Dec-18 6:00
protectorRichard MacCutchan11-Dec-18 6:00 
GeneralRe: List HDD files in CListView Pin
_Flaviu11-Dec-18 7:52
member_Flaviu11-Dec-18 7:52 
GeneralRe: List HDD files in CListView Pin
Richard MacCutchan11-Dec-18 22:01
protectorRichard MacCutchan11-Dec-18 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.