I wonder how safe is this without any locks.With right timing all threads may finish and yet the running value can be 2.
AIUI that's what the GIL ( Global Interpreter Lock ) is for. That's a part of most Python interpreters, working under the hood to keeps things like that safe, allowing Python programmers to not worry over such things, and one of the things which can be argued to make Python better or easier to use than other languages.
I am not sure. So what is unit of execution in python when this lock is held?
You could well be right. I just threw that together. I'd do it properly if I ever needed to do something like that in production code, check each thread had ended, but couldn't be bothered looking up the documentation
Code: Select all
cd ~ wget -o scriptbasic-2.1-linux-armhf.deb "https://www.scriptbasic.org/forum/index.php?action=dlattach;topic=359.0;attach=421" sudo apt install ./scriptbasic-2.1-linux-armhf.deb
Code: Select all
cd ~ wget -o scriptbasic-rpi-zero.deb "https://www.scriptbasic.org/forum/index.php?action=dlattach;topic=359.0;attach=435" sudo apt install ./scriptbasic-rpi-zero.deb
Code: Select all
' based on https://www.nayuki.io/res/fast-fibonacci-algorithms/fastfibonacci.py function Lhs_Fibonacci(n) if n <> 0 then a = Lhs_Fibonacci(n >> 1) b = Rhs_Fibonacci(n >> 1) if ( n & 1 ) <> 0 then Lhs_Fibonacci = a * a + b * b else Lhs_Fibonacci = a * (b + b - a) end if else Lhs_Fibonacci = 0 end if end function function Rhs_Fibonacci(n) if n <> 0 then a = Lhs_Fibonacci(n >> 1) b = Rhs_Fibonacci(n >> 1) d = a * a + b * b if ( n & 1 ) <> 0 then Rhs_Fibonacci = a * (b + b - a) + d else Rhs_Fibonacci = d end if else Rhs_Fibonacci = 1 end if end function function Fibonacci(n) Fibonacci = Lhs_Fibonacci(n) end Function print Fibonacci(24)
Code: Select all
function shifts(v,p,ar) local bp,ba,co,cq,bi,x,y,d bp=1 x=0xffffffff and v for co=0 to 31 ba[co]=0 next for co=0 to 31 bi=x and bp cq=co+p if (bi<>0) then if ((cq>=0)and(cq<32)) then ba[cq]=1 end if end if bp = bp + bp next bp=1 y=0 ' 'SUPPORT FOR ARITHMETIC RIGHT SHIFTS ' d=100 if (ar) then if (x and 0x80000000) then d=31+p end if end if ' for co=0 to 31 if ((ba[co]<>0)or(co>=d)) then y=y or bp end if bp = bp + bp next shifts=y end function PRINT shifts(0x80000000,2),"\n" PRINT shifts(-32,-2,1),"\n" PRINT shifts(8,-2),"\n"
hippy wrote: ↑Sun Apr 14, 2019 6:57 pmAIUI that's what the GIL ( Global Interpreter Lock ) is for. That's a part of most Python interpreters, working under the hood to keeps things like that safe, allowing Python programmers to not worry over such things, and one of the things which can be argued to make Python better or easier to use than other languages.
Of course; there's also some criticism of Python having a GIL.
Is that the default run mode?Numba is an open-source NumPy-aware optimizing compiler for Python sponsored by Anaconda, Inc and a grant from the Gordon and Betty Moore Foundation. It uses the LLVM compiler infrastructure to compile Python to CPU and GPU machine code.
I know. I found it by commenting every line out and iteratively adding lines back in. That wouldn't have worked though on anything more substantial.
Oh dear — shotgun debugging is pretty much a last resort. While I'm not really a fan of Python's fulsome error verbiage, it never leaves you in any doubt what went wrong and (roughly) where.
Umm, maybe don't attack people who want to learn your language? Getting all fighty over an error might not be the best way to grow your language's popularity — think MINASWAN instead. It would have been as easy to put in a link to your manual (easier still if you used a browser extension such as Format Link) as kvetch about it.The ScriptBasic User Guide would have told you what are the valid operators for the language. Much easier than searching through the developer documentation or the source.
The Windows version of ScriptBasic has a great GUI IDE / Debugger. If you get stuck on the RPi, use the Windows version to debug your code.Oh dear — shotgun debugging is pretty much a last resort.
I don't know if this is a practical solution with ScriptBasic, but the BBC BASIC for SDL 2.0 IDE/debugger is itself written in BBC BASIC, ensuring that every (desktop) platform that can run the interpreter can run the IDE/debugger as well, with an identical look-and-feel in each case.
Which seems a shame when it could have printed the line number and given more details of what the error was. Rather than "(0): error &H1c:Command is not implemented and no currently loaded extension module defined behaviour for it" what I would have expected was something like - "fibo.sb:5: No extension module included which supports '>>' operator".
But it is a valid operator - or it would not have compiled successfully. The actual problem is it's an operator which isn't natively implemented and I provided no implementation for it.
The problem is not so much having not read the manual or knowing what's valid or not, but having been careless in switching between languages. There's knowing the language and trying to find where one has subconsciously strayed from what it should have been. No amount of knowing a language can prevent that.
As far as I can tell it provides a line number for compilation errors but not for runtime errors.
I'm not sure that's a great advert for something you are hoping could become a widely used development tool for the Pi.