[Windows x64]WaitOnAddress

Share your advanced PureBasic knowledge/code with the community.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

[Windows x64]WaitOnAddress

Post 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*
Last edited by AndyMK on Tue Feb 14, 2023 2:56 pm, edited 10 times in total.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: [Windows x64]WaitOnAddress

Post by Caronte3D »

Thi's correct? :?

Code: Select all

    CapturedValue = g_TargetValue
    UndesiredValue = g_TargetValue
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: [Windows x64]WaitOnAddress

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: [Windows x64]WaitOnAddress

Post 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?
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: [Windows x64]WaitOnAddress

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Windows x64]WaitOnAddress

Post by Mijikai »

Thanks for sharing.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: [Windows x64]WaitOnAddress

Post by Lunasole »

Interesting, but too much probably for small threaded stuff I recently did
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply