a few procedures that need more testing...

Everything else that doesn't fall into one of the other PB categories.
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

a few procedures that need more testing...

Post by aszid »

*oops! edited the code slightly*

well, i created a few procedures that SEEM to work great for sharing global strings between threads, but i've only tested it on one system so far (win2k) and i don't have access to any machines that run win98/me

so it would be much appreciated if someone else could give this a go

this example is running 4 additional threads (other than the main thread) and it seems to work great on my work computer.

please let me know if you can run this, and let me know what OS you're running.

Make sure you have the debugger enabled.

basically what you should see, is lines of text in the debugger window, with different characters (5 of them) in the same order over and over. i set the string to reset to blank when the length gets to 50.

Code: Select all

Global varhold.b, runreq.b 

Procedure.b request(varnum.b,type.b) ; requests access to a variable 
  ;varnum = number representing the variable you want to lock (only 1 - 8 for now) 
  ;type 1 waits until the variable is available, then gets it 
  ;type 2 returns a 1 if access is granted, else returns a 0 (doesn't wait) 
  
  Repeat 
    If runreq = 1 
      Repeat 
        Delay(5) 
        If runreq = 0 Or runreq > 1
          breakout = 1
        EndIf
      Until breakout = 1 
      breakout = 0
    EndIf 
    runreq + 1 
    Delay(Random(5)) 
    If runreq > 1 
      runreq - 1 
      Debug "bing!" 
    Else 
      breakout = 1 
    EndIf 
  Until breakout = 1 
  
  If varnum <= 8 
    Select type 
      Case 1 ; wait until available. 
        Repeat 
          If Left(Right(Bin(varhold),varnum),1) = "1" 
            Delay(5) 
          Else 
            varhold + Pow(varnum,2) 
            lock = 1 
          EndIf 
          
        Until lock = 1 
        
      Case 2 ; immediate (with return code) 
        If Left(Right(Bin(varhold),varnum),1) = "1" 
          ProcedureReturn 0 
        Else 
          varhold + Pow(varnum,2) 
          ProcedureReturn 1 
        EndIf 
        ProcedureReturn 1
    EndSelect 
  Else 
    ProcedureReturn 0
  EndIf 
  
  
  
  runreq -1 
  
EndProcedure 

Procedure release(varnum.b) ;releases the var for other use 
  
  Repeat 
    If runreq = 1 
      Repeat 
        Delay(5) 
        If runreq = 0 Or runreq > 1
          breakout = 1
        EndIf
      Until breakout = 1 
      breakout = 0
    EndIf 
    runreq + 1 
    Delay(Random(5)) 
    If runreq > 1 
      runreq - 1 
      Debug "bing!" 
    Else 
      breakout = 1 
    EndIf 
  Until breakout = 1 
  
  If varnum <= 8 
    varhold - Pow(varnum,2) 
  EndIf 
  
  Delay(20) 
  runreq - 1 
EndProcedure 

Global blah$ 

Procedure testthread(a) 
  Repeat 
    a = request(2,1) 
    blah$ = blah$ + "-" 
    release(2) 
    Delay(25) 
  ForEver 
EndProcedure 

Procedure testthread1(a) 
  Repeat 
    a = request(2,1) 
    blah$ = blah$ + "#" 
    release(2) 
    Delay(25) 
  ForEver 
EndProcedure 

Procedure testthread2(a) 
  Repeat 
    a = request(2,1) 
    blah$ = blah$ + "$" 
    release(2) 
    Delay(25) 
  ForEver 
EndProcedure 

Procedure testthread3(a) 
  Repeat 
    a = request(2,1) 
    blah$ = blah$ + "&" 
    release(2) 
    Delay(25) 
  ForEver 
EndProcedure 

If OpenWindow(0,0,0,100,100,#PB_Window_SystemMenu,"bleh") = 0 
  Debug "couldn't make window" 
  End 
EndIf 

CreateThread(@testthread(),0) 
CreateThread(@testthread1(),0) 
CreateThread(@testthread2(),0) 
CreateThread(@testthread3(),0) 

Repeat 
  WindowEventv = WindowEvent() 
  a = request(2,1) 
  blah$ = blah$ + "+" 
  Debug blah$ 
  
  If Len(blah$) > 50 
    blah$ = "" 
  EndIf 
  release(2) 
  Delay(25) 
Until WindowEventv = #PB_Event_CloseWindow
thanks in advance
Last edited by aszid on Sat Sep 20, 2003 3:23 am, edited 2 times in total.
--Aszid--

Making crazy people sane, starting tomorrow.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

first of all: Here is a problem

Code: Select all

If runreq = 1 
  Repeat 
  Delay(5) 
  Until runreq = 0 
  runreq = 1 
 EndIf 
I think, it should be

Code: Select all

If runreq = 1 
  Repeat 
  Delay(5) 
  Until runreq = 0 
 endif
  runreq = 1 
But there is a other problem: Windows is a Multitasking-System. So you get problems, because after the command

if runreq=1

it is possible, that the CPU switch to a other Task and there is probly the same

if runreq=1
but this time from a other Task

So both get a Ok, no problem.

GPI
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post by aszid »

d'oh, you're right... i guess that's what i get for throwin it together so fast.

well, i'm working on it now, hoping i can resolve it, i've got a few other ideas that i'll likely have to impliment....
--Aszid--

Making crazy people sane, starting tomorrow.
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post by aszid »

well, i updated the code in my first post of this thread, and here's what i've noticed so far. each thread does NOT get "equal" access to the string. essentially, it's first come-first serve, and the main loop has a few other things to do other than what the threads do.

i did notice a bug in my prior code, where on occasion i would get a blank line in the debugger, but i have not seen this in my current code.

i'm not 100% sure that this will work all that well either, but i think i have taken a few steps in the right direction. The main limit to this is that the more threads you use, the more wait times for each thread. i tested this example with one difference, i made each thread start about 5 times each (so 20 threads + main prog) the string was still updated, but the main loop seemed not to output the string to the debug window very often.

i think i'm going to add a "priority" setting, that would just lessen the delays while waiting for access. i'll probably also make it so that after so many loops, while waiting for access, the delay will shorten automatically to help keep one thread from stalling for too long.


anyhow... i know this idea has a ways to go. If i could get more people to test this code just to see if it will crash, it would be much appreciated.


*edit! undated code again, to hopefully get rid of an infinate loop problem*
--Aszid--

Making crazy people sane, starting tomorrow.
TronDoc
Enthusiast
Enthusiast
Posts: 310
Joined: Wed Apr 30, 2003 3:50 am
Location: 3DoorsDown

Post by TronDoc »

ran on WinDoze98 1stEd
only tried it for about 30 seconds
as I have huge downloads in progress
and didn't want to risk crashing them.

if you want, I can run it again for
a longer period later.

let me know the proper way
to test this for you.

Joe
peace
[pI 166Mhz 32Mb w95]
[pII 350Mhz 256Mb atir3RagePro WinDoze '98 FE & 2k]
[Athlon 1.3Ghz 160Mb XPHome & RedHat9]
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post by aszid »

cool, i'm glad it didn't crash on w98, thanks for giving it a go. if you wouldn't mind running it for a bit longer that would be awesome. maybe 5 minutes or so? anyhow, what i need to watch for is blank lines in the debug window as well as corruption and infinite loops... i did have that happen to me once. and naturally it would be good to know if it crashes.

anyhow... i'll put some more time into this later tonight, and see if i can't make it work better.
--Aszid--

Making crazy people sane, starting tomorrow.
Post Reply