@Benjamin - after spending some time with the idea, I agree with you on the complexity issue. As far as a global data structure for objects I also agree, it would be better to keep all things that require the object's data directly together or else risk having to pass a local version from thread to thread each loop.
@IanM - It seems like the synchronization should work as long as you create all of your mutexes before the other threads are made and delete them after the threads no longer exist. The ticker commands were more of a nicety than anything.
I do know one thing, it's a beast to impliment safely and efficiently.(why i'm trying to find a good method for it)
[edit]
You are right guys, unless i were to lock the whole game system for each thread's loop (which would defeat the purpose), there is no way to gaurantee stability in dbpro. There is no telling which dbpro commands will work and which won't, and which ones even look like they work for a while but bite ya later.
example..
REM Project: multithreading test
REM Created: 3/25/2007 10:54:01 PM
REM
REM ***** Main Source File *****
REM
disable escapekey
sync on
sync rate 0
type position
X as float
Y as float
Z as float
endtype
type rotation
X as float
Y as float
Z as float
endtype
type generic_flags
exist as boolean
visible as boolean
endtype
type Entity
pos as position
rot as rotation
flags as generic_flags
objindex as integer
endtype
global dim Players(20) as Entity
Players(1).flags.exist = 1
Players(1).flags.visible = 1
Players(1).objindex = 1
make sysobj mutex 1, "Player Entity Table"
global dim keys(20) as boolean
make sysobj mutex 2, "Keyboard Stream"
IH = make function thread(1)
PH = make function thread(2)
make object cube players(1).objindex, 10
local dim local_players(20) as Entity
local a as integer
do
lock sysobj 1
for a = 1 to 20
local_players(a).pos.x = players(a).pos.x
local_players(a).pos.y = players(a).pos.y
local_players(a).pos.z = players(a).pos.z
local_players(a).rot.x = players(a).rot.x
local_players(a).rot.y = players(a).rot.y
local_players(a).rot.z = players(a).rot.z
local_players(a).flags.exist = players(a).flags.exist
local_players(a).flags.visible = players(a).flags.visible
local_players(a).objindex = players(a).objindex
next a
unlock sysobj 1
for a = 1 to 20
if local_players(a).flags.exist = 1
if local_players(a).flags.visible = 1
position object local_players(a).objindex, local_players(a).pos.x, local_players(a).pos.y, local_players(a).pos.z
rotate object local_players(a).objindex, local_players(a).rot.x, local_players(a).rot.y, local_players(a).rot.z
endif
endif
next a
sync
if escapekey() = 1
nice sleep 500
delete sysobj 1
delete sysobj 2
end
endif
loop
function InputHandler()
local dim local_keys(20) as boolean
do
nice sleep 20
for a = 1 to 4
local_keys(a) = 0
next a
if leftkey() = 1 then local_keys(1) = 1
if rightkey() = 1 then local_keys(2) = 1
if upkey() = 1 then local_keys(3) = 1
if downkey() = 1 then local_keys(4) = 1
lock sysobj 2
for a = 1 to 20
keys(a) = local_keys(a)
next a
unlock sysobj 2
if escapekey() = 1 then exit
loop
delete thread IH
endfunction
function PhysicsHandler()
local dim local_keys(20) as boolean
local dim local_players(20) as Entity
lock sysobj 1
for a = 1 to 20
local_players(a).pos.x = players(a).pos.x
local_players(a).pos.y = players(a).pos.y
local_players(a).pos.z = players(a).pos.z
local_players(a).rot.x = players(a).rot.x
local_players(a).rot.y = players(a).rot.y
local_players(a).rot.z = players(a).rot.z
local_players(a).flags.exist = players(a).flags.exist
next a
unlock sysobj 1
do
nice sleep 10
lock sysobj 2
for a = 1 to 20
local_keys(a) = keys(a)
next a
unlock sysobj 2
if local_keys(1) = 1
local_players(1).rot.y = local_players(1).rot.y - 1.0
endif
if local_keys(2) = 1
local_players(1).rot.y = local_players(1).rot.y + 1.0
endif
if local_keys(3) = 1
local_players(1).pos.x = local_players(1).pos.x + sin(local_players(1).rot.y)
local_players(1).pos.z = local_players(1).pos.z + cos(local_players(1).rot.y)
endif
if local_keys(4) = 1
local_players(1).pos.x = local_players(1).pos.x - sin(local_players(1).rot.y)
local_players(1).pos.z = local_players(1).pos.z - cos(local_players(1).rot.y)
endif
lock sysobj 1
for a = 1 to 20
players(a).pos.x = local_players(a).pos.x
players(a).pos.y = local_players(a).pos.y
players(a).pos.z = local_players(a).pos.z
players(a).rot.x = local_players(a).rot.x
players(a).rot.y = local_players(a).rot.y
players(a).rot.z = local_players(a).rot.z
players(a).flags.exist = local_players(a).flags.exist
next a
unlock sysobj 1
if escapekey() = 1 then exit
loop
delete thread PH
endfunction
this appears to work okay until you press CTRL-ALT-DLT(silent crash)
simply adding a command like
Set Camera to Follow causes an instant crash and memory error(it probably uses a maths dll for calculations which i am using elsewhere).
Oh well, it was fun trying.
Do, or do not. There is no try.
-Yoda