Oxygen Basic
Information => Development => Topic started by: Brian Alvarez on May 07, 2019, 10:09:53 PM
-
Charles, is there currently a way to implement thread-local variables?
I mean, variables that are global to each module, and function and class method, but are independent for each different thread? (GLOBAL variables are shared across threads)
-
o2 does not support thread_local variables but you could create thread objects quite easily, then the object members would have similar scope to thread_local, with additional advantages.
-
Thanks! Can you show me an example Charles?
-
This demo increments a counter in a thread, which can be monitored from the console.
uses corewin
uses console
extern
'
class ThreadDemo
================
int counter
int running
sys thread
'
function constructor()
counter=0
running=1
thread=CreateThread(0,0,@threadcall,@this,0,0)
end function
'
function destructor()
CloseHandle(thread)
end function
'
function threadcall()
while running
act()
wend
end function
'
function act()
counter++
sleep 20
end function
'
function stop()
running=0
end function
'
end class
'
'TEST:
new ThreadDemo td()
int k
printl "Press [space] to show count, [enter] to close"
do
k=waitkey
select k
case 3,13 : td.stop() : exit do
case 32 : printl td.counter
end select
loop
printl "Final count " td.counter
waitkey
del td
-
In order to implement it, i would have to make every sub and function a method of a global class... kind of like java.
I think i will have to create my own variables, and manage memory adresses manually.... No biggie, i just wanted to know if this
was already possible, if not, i will do it today.
-
Yes. I agree. o2 has no core thread constructs. But OOP seems to be a perfect fit for clean threading.
-
It already has under PluriBASIC. And they work absolutely fantastic. ;D
-
Charles, i just noticed RND started misbehaving... i will post a new thread.