Handmade Network»Forums
60 posts
None
InterlockedIncrement Doubt
Edited by erpeo93 on
Hi all,

Just finished watching episode 124 of handmade hero.
Casey shows what use can we make of InterlockedIncrement to Synchronize threads.
The mechanism of InterlockedIncrement per se is clear enough to me, what makes me think is this piece of code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
global_variable nextEntryIndex;
global_variable countEntries;

DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
    Win32ThreadInfo* myInfo = ( Win32ThreadInfo* ) lpParameter;
    
    for(;;)
    {
        if( nextEntryIndex < countEntries )
        {
            uint32 entryIndex =InterlockedIncrement( ( LONG volatile* ) &nextEntryIndex ) - 1;
            DOStuff();
        }
    }
}


Let's say nextEntryIndex is 0, while countEntries is 1.
Thread 0 surpasses the if, but then, before making the InterlockedIncrement, is blocked and now it's thread 1 time.
Of course, thread 1 it's also going to pass the if, and so at this point everything is gone.
I mean, it's a really remote possibility that thread 0 gets interrupted EXACTLY after the if but between the InterlockedIncrement, but it could happen right?
Considering this remote case, my solution has been:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
    Win32ThreadInfo* myInfo = ( Win32ThreadInfo* ) lpParameter;
    for(;;)
    {
        if( nextEntryIndex < countEntries )
        {
            uint32 oldEntryIndex = nextEntryIndex;
            uint32 nextIndex =InterlockedIncrement( ( LONG volatile* ) &nextEntryIndex );
            if( nextIndex > oldEntryIndex )
            {
                DOStuff();
            }
        }
    }
}

This way, even in that remote case, the second if will fail because InterlockedIncrement will return the old value.
Again, probably just me misunderstanding something, in fact I opened this post just to clarify this thing.

Thank you.
Leonardo

Asaf Gartner
64 posts / 2 projects
Handmade Network Staff
InterlockedIncrement Doubt
Casey replaced it with an InterlockedCompareExchange in day 126.
60 posts
None
InterlockedIncrement Doubt
Ops, sorry for spamming this useless thread then.

Thanks for the quick reply anyway.
117 posts
Code hacker/developer
InterlockedIncrement Doubt
Edited by Todd on
erpeo I think you shouldn't worry, I don't know how in God's name anyone could expect anyone else to "check the vids" before posting here, seeing as there is literally over 800 hours of content at this point. In fact, the stream time certainly outlasts university undergraduate compsci classes and maybe even graduate lecture time by a long shot.