FreeBasic: Multithreading

Aus Wikibooks

 Multithreading

Hinweis: FreeBASIC in Verbindung mit fbGFX ist noch nicht Threadsafe, das heißt, der Umgang mit Threads ist derzeit (0.18 CVS) höchst instabil, zumindest, wenn mehr als 1 Thread FBGFX-Funktionen nutzt. Dazu gehören auch Sleep und Inkey.

Uhrzeit[Bearbeiten]

declare sub uhr()
Dim Shared WriteRigth As any ptr
Dim        handler    as any ptr
Dim        i          as integer

WriteRigth = MUTEXCREATE

handler = THREADCREATE(@Uhr)

MUTEXLOCK WriteRigth
    locate 2,1
MUTEXUNLOCK WriteRigth

for i=0 to 20
    MUTEXLOCK WriteRigth
        ? "Hallo Welt Nummer ";i;" Test"
    MUTEXUNLOCK WriteRigth
    sleep 500
next i

sub uhr()
    dim csl as integer
    dim p   as integer
    Dim timeold as string

    do
        if timeold<>time$ then
            MUTEXLOCK WriteRigth
                csl=Csrlin
                p=Pos
                locate 1,1
                ? time$
                locate csl, p
                timeold=time$
            MUTEXUNLOCK WriteRigth
        end if
        sleep 10
    loop
end sub

Nachteile:

  • Probleme mit Input
  • ...

Beenden bei Tastendruck[Bearbeiten]

declare sub Test()
Dim handler as any ptr
dim i       as integer

screen 19

handler = THREADCREATE(@Test())

for i=0 to 200
    color i mod 15
    ? i
    sleep 500,1
next i

sub test
    do
        if inkey$<>"" then end
        sleep 1
    loop
end sub