Jump to content

If condition check in a interruptible function. Is it safe?


binhnx
 Share

Go to solution Solved by JohnOne,

Recommended Posts

@BrewmanNH: I do not know about how AutoIt internal work, so I must guess. It's the reason i need the developer's answer.

About the CriticalSection, I'm wrong:

  • Case 1: Not change
  • Case 2: I had thought that AutoIt internal actually implement multi-threading to simulating muli-thread with HotKey/Adlib/Event/Callback. But now I realize I was wrong. It's more probability that It using only one thread, so multiple call to EnterCriticalSection will not cause dead-lock occur, but in that case, that function has no effect to the script. 
  • Case 3: Not change.

So it must be a reason why the Overlord state that 2 function in his sentence. I don't think that is about our script, but the internal underlying AutoIt source code.

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

Actually, I'm definitely overthinking. I'm sorry about any inconvenience, to everyone has spent time to read and give me suggestion. And want to say thanks, many thanks.

But I still need the correct answer, simply no or yes (also 1^-n % still yes)

This problem should not effect me much, because mainly I only write UDF. So any one use my UDF should be the victims :)

I only want an answer, to warn them about that.

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

:)

I also want to be a good man. 

But my probability teacher said that, a probability of 0% in theory can also occur in real. And a probability of 100% may not occur.

So, I only ask for a theory probability, I guess, at least I'm a better man than my teacher :)

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

You can still restrict (or even close, I don't know for sure) the time window:

Set()

While 1
    Sleep(43)
WEnd

Func Set()
    AdlibRegister("Func1", 1111)
    HotKeySet("{Esc}", "Func2")
    AutoItSetOption("GUIOnEventMode", 1)    ; for illustration only
EndFunc

Func Func1()
    Local $Once = [AdlibUnRegister("Func1"), HotKeySet("{Esc}"), AutoItSetOption("GUIOnEventMode", 0)]
    ConsoleWrite("Alone in the clear" & @LF)
    Set()
EndFunc

Func Func2()
    Local $Once = [AdlibUnRegister("Func1"), HotKeySet("{Esc}"), AutoItSetOption("GUIOnEventMode", 0)]
    ConsoleWrite("Alone in the dark" & @LF)
    Set()
EndFunc

BTW your case 2 doesn't work: Static initialization occurs only once for all.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd: My 3 examples is example only. It describe the problems, all that may not work

In your example (this should be the example too, not the solution). Actually, any script written by AutoIt should be example, not solution. It may be OK, if AutoIt, internally, handle it well. But it also may not be OK, the problem may comes if an un-normal user have ability to click too fast as two event/hotkey reach the function at the same time, and it will call twice. 

With the Hotkey, my static-count-variable trick will solve (100%) the problem. But, in other cases, it not.

It's may be 0.0000000.....00000... 0000.....0000...01% for error to occur, I know, but if a developer state that it's 0%, it should be 0%. So I need the comment of a developer, who know how AutoIt internal work ^^ 

Sorry for my information-greed. And thanks you all so much (special to BrewmanNH, who has much patience to reply me). If anyone can ask the dev, it's a happy ending

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

  • Solution

The answer to the question is no, it is not safe, that's the 0 or 1 answer.

I don't need to be an AutoIt developer to answer that.

Anything else now is nothing more than bitching.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

AutoIt is single threaded, it process in sequence from the start of the script to the end of it, jumping around the code by function calls to other locations. There is no possibility for 2 functions to operate at the same time, because even when using a hotkey or an adlib function, that function will be called, executed, and if written correctly end, the processing of the code will continue where it was when that adlib or hotkey function was called. If that's not good enough for you, use another language, and your desire to use AutoIt is a moot point, either it's good enough for what you're doing or it's not. If it's good enough, then use it. If it's not good enough to satisfy your unreasonable requirements, then use another language. It really is just that simple.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewmanNH: Thank you.

@All: I need an answer, simple and exactly what JohnOne wrote (the first line he wrote). So the topic finally has an answer.

@JohnOne: But your 3rd is totally f**king bullshit. In any case, BrewmanNH should be the angriest man when reply me, but he didn't!

@All, again: I created this topic, because after searching around the forum, I found very few topic about this problem. Also the Wiki didn't cover enough for this. So I created this, not only for me, but for many one can search for problem. After ensure that the script is not safe, although very in a small case, we provide script to work around for each specified kind: queue by Event/Adlib, or interrupted by HotKey.

I planed about creating a new thread about the work around, if the answer I given is no. If it is yes, everything don't need to be cared anymore. But it seems that i don't need to. An idea is raised on my head. It should be simple as it should:

; No matter is Hotkey, Event, Adlib, it should work
Func CriticalFunction()
  ; Can use global too, but it's less nice
  Static $iFlag = 0
  $iFlag += 1
  If ($iFlag = 1) Then
    ; Do whatever you want
    If ($bConditionCheck) Then
      ; Set condition to false so any queued function cannot use it
      ; Do whatever you want
    EndIf
  EndIf
  $iFlag -= 1
EndFunc

When called with HotKey, if two HotKey is pressed very quickly, and the function is called overlapped, then the second cannot get over the first If-check on $iFlag, and return. Now the first will be called, and because $iFlag is decreased to 1, it pass the check and continue to do work.

When called with Adlib/Event, function will be queued, and call after one finished. Now the $iFlag check is useless, but since the function is queued, not interrupted, the check on condition should work.

Now a more complex case, if the function is called by the Adlib/Event, and the HotKey is pressed right after check condition line is executed, and before the set condition line is executed. If we don't have int check by $iFlag, HotKey function is called, see $bConditionCheck as allowed, and continue, so we execute function twice or more, overllaped. But with $iFlag, the HotKey function is terminated right after the $iFlag check, since it must be 2 or later.

Any better idea?

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

Yes, disable the hotkey as soon as the function is called, that will completely eliminate any possibility of the hotkey actioning again. Which is what I suggested to you at the start of this thread. Your method doesn't do anything that your first code didn't do, it will operate exactly as that one did, you just wrote it differently, you haven't improved it, you've just reconfigured it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewmanNH: The reason is, although check and set at the very first, it also not safe, with a very small change.

Addition to that, sometime you cannot placed the check statement at that first, and I think to improve it so we can check in everywhere in our function. Rethink about when the function is interrupted and you will see that it is entirely different!

@JohnOne: When started the topic, I hope any chance that the developer had implemented it. But now, after a very long time we cannot write safe function that way, than it's by design and re-implemented in that case is difficult. We can use that time and resource to do other better thing. All we can do is provide work around in our script. :)

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

I thought about it, and I still think you have no idea what you're talking about and your work around is exactly what you wrote in the first post. BTW, you could just as easily set your $iFlag like this, and it wouldn't make one iota of difference to how the script runs.

 

; No matter is Hotkey, Event, Adlib, it should work
Func CriticalFunction()
  ; Can use global too, but it's less nice
  Static $iFlag = False
  $iFlag = Not $iFlag
  ; or like this
;  Static $iFlag = 0
;  $iFlag = 1
  If $iFlag Then
    ; Do whatever you want
    If ($bConditionCheck) Then
      ; Set condition to false so any queued function cannot use it
      ; Do whatever you want
    EndIf
  EndIf
  $iFlag = Not $iFlag
; $iFlag = 0
EndFunc
Either of those methods would work the same, and probably work just a tiny little bit faster than your method which is using addition and subtraction when not necessary.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewmanNH: Totally different between set and increasement.

You set the flag, do not care if its current value is. So when a reentrancy occur, you silently set without notice, the only thing you know is once set

You increase and decrease the flag then AutoIt must care about it, increase, or decrease it, and we can notice the flag is decrease/increase how much time, and we can verify the value. It's totally different than only a simple set.

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

sigh - this behavior of you trying to strive to perfection with code and annoying several people with this "getting to be stupid with you going on like this" question is getting old. REAL OLD. You have been given an answer  OVER AND OVER and yet you keep arguing trying to justify your desire for perfection. You are NOT going to convince us that this pursuit of yours is worthy. In our mind it isn't worth the time for we know what the answer is and we are OK with that.

To put it simply - stop arguing and pissing people off please. You got your answer. Please move on. Thank you.

Edited by DarthCookieMonster
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...