handmade.network » Forums » InterlockedIncrement Doubt
erpeo93
leonardo lucania
32 posts

None

#11406 InterlockedIncrement Doubt
7 months ago Edited by leonardo lucania on March 20, 2017, 9:54 p.m.

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


None
AsafG
Asaf Gartner
29 posts
#11407 InterlockedIncrement Doubt
7 months ago

Casey replaced it with an InterlockedCompareExchange in day 126.
erpeo93
leonardo lucania
32 posts

None

#11408 InterlockedIncrement Doubt
7 months ago

Ops, sorry for spamming this useless thread then.

Thanks for the quick reply anyway.

None
Todd
94 posts

I use x86-64 assembly, Python, C, and SQL to fight malware.

#11420 InterlockedIncrement Doubt
7 months ago Edited by Todd on March 22, 2017, 7:07 a.m.

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.

I'm the smartest man in Athens because I know that I know nothing. ~ Socrates