Qbasicnews.com

Full Version: Thread Timing?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am messing around with threading for something I am working on, and it works except the ouput is slightly garbled. Also depending on what delay I give the teletyping I get different results, all of which are garbled by at least 1 character. For the teletyping, the 2nd and 3rd lines are the threads and the 1st and 4th are regular called routines. Here are the different results I get and the code that I used:

This is what I get when running with Sleep 25
[Image: test25ms6wz.png]

This is what I get when running with Sleep 100
[Image: test100ms4hj.png]

This is what I get when running with Sleep 250
[Image: test250ms4zd.png]

And here is the code:

Code:
declare sub thread1(byval num as integer)
declare sub thread2(byval num as integer)
declare sub teletype(text as string,x,y)

i=threadcreate (@thread1,0)
if (i = 0) then
   print "Error creating thread1"
end if

ii=threadcreate (@thread2,1)
if (ii = 0) then
   print "Error creating thread2"
end if

threadwait(i)
threadwait(ii)

Call teletype ("testing.................",1,1)
Call teletype ("testing again...........",10,1)

sleep
end

sub thread1 (byval num as integer)
   Call teletype ("This is a test...",4,1)
end sub

sub thread2 (byval num as integer)
   Call teletype ("This is another test...",7,1)
end sub

sub teletype (text as string,x,y)
   text_length=len(text)
   for a = 0 to text_length
      temp_text$=mid$(text,a,1)
      Locate x,(y+a-1):Print temp_text$
   sleep 25
   next
end sub
Looks to be a glitch in FreeBasic. Maybe it only has one position variable, the one that tells it where it is on the screen.

Where are you putting the SLEEP statement?
I believe you have to use mutexes for this type of operation. There is a thread about this on the Freebasic forum:

http://www.freebasic.net/forum/viewtopic.php?t=1418