Page 1 of 1

[Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 11:51 am
by AndyMK
The WaitOnAddress function can be used by a thread to wait for a particular value to change from some undesired value to any other value. WaitOnAddress is more efficient than using the Sleep function inside a while loop because WaitOnAddress does not interfere with the thread scheduler. WaitOnAddress is also simpler to use than an event object because it is not necessary to create and initialize an event and then make sure it is synchronized correctly with the value. WaitOnAddress is not affected by low-memory conditions, other than potentially waking the thread early as noted below.

Any thread within the same process that changes the value at the address on which threads are waiting should call WakeByAddressSingle to wake a single waiting thread or WakeByAddressAll to wake all waiting threads. If WakeByAddressSingle is called, other waiting threads continue to wait.
See here for more information https://learn.microsoft.com/en-us/windo ... tonaddress
Download the dll here https://1drv.ms/u/s!AufD7SJtv9Nuj5Uha2Y ... Q?e=y2YG3y

Code: Select all

OpenLibrary(0, "api-ms-win-core-synch-l1-2-0.dll")
Prototype WaitOnAddress(*Address, *CompareAddress, AddressSize.l, dwMilliseconds.l)
Prototype WakeByAddressSingle(*Address)

Global WaitOnAddress.WaitOnAddress = GetFunction(0, "WaitOnAddress")
Global WakeByAddressSingle.WakeByAddressSingle = GetFunction(0, "WakeByAddressSingle")
Global g_TargetValue

Procedure Thread(val)
  
  Debug "Waiting for value at address " + @g_TargetValue + " to change"
  UndesiredValue = 0 ;<-- if this value matches g_TargetValue, thread will sleep and wait for change
  WaitOnAddress(@g_TargetValue, @UndesiredValue, 1, #INFINITE)
  Debug "Value at address " + @g_TargetValue + " changed"
  
EndProcedure

Thread = CreateThread(@Thread(), 0)

g_TargetValue = 0 ;<-- same as Undesired value so thread will sleep
WakeByAddressSingle(@g_TargetValue)

Delay(3000)

g_TargetValue = 1 ;<-- different to Undesired value so thread will resume
WakeByAddressSingle(@g_TargetValue)

WaitThread(Thread)
*UPDATED*

Re: [Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 12:16 pm
by Caronte3D
Thi's correct? :?

Code: Select all

    CapturedValue = g_TargetValue
    UndesiredValue = g_TargetValue

Re: [Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 2:45 pm
by AndyMK
Caronte3D wrote: Tue Feb 14, 2023 12:16 pm Thi's correct? :?

Code: Select all

    CapturedValue = g_TargetValue
    UndesiredValue = g_TargetValue
I redone it. It should be easier to understand now.

Re: [Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 4:33 pm
by Caronte3D
Ah! Ok, I understand better now :D
Just curious... What is the advantage of using this dll instead of simply check directly if the variable was changed?

Re: [Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 4:56 pm
by AndyMK
Caronte3D wrote: Tue Feb 14, 2023 4:33 pm Ah! Ok, I understand better now :D
Just curious... What is the advantage of using this dll instead of simply check directly if the variable was changed?
All the info is here https://learn.microsoft.com/en-us/windo ... tonaddress.

Re: [Windows x64]WaitOnAddress

Posted: Tue Feb 14, 2023 9:40 pm
by Mijikai
Thanks for sharing.

Re: [Windows x64]WaitOnAddress

Posted: Sat Mar 18, 2023 10:23 am
by Lunasole
Interesting, but too much probably for small threaded stuff I recently did