Announcement

Collapse
No announcement yet.

Extra thread?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extra thread?

    I'm having a heck of a time with something I'm sure should be much easier. I have a session timer in a communications routine. When a session times out I want to clear any string data associated with that session. Since each session is processed in it's own thread, I have the threadcount constantly displayed as a diagnostic. I noticed something odd, and finally reduced it to the code below.

    Clearing the string (doesn't actually matter which string in the array) creates a second thread, which doesn't go away. What incredibly obvious thingy am I missing?

    Also, neat project I worked on with my uncle here.

    Code:
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "WIN32api.inc"
    
    ' Console Tools Pro Library (mainly for Timer functions)
    $INCLUDE "CT_PRO.INC"
    
    ' session timer stuff
    %timerMode      = 2             ' 0=No Timeouts, 1=Absolute session limit, 2=seconds since last data receive
    %clientTimeout  = 10            ' timeout measured in seconds
    
    GLOBAL cStrings()   as string   ' holds incoming data from each client
     
    '=========================================================================
    ' main program loop
    FUNCTION PBMAIN () AS LONG
        local myKey AS STRING
    
        REDIM cStrings(0 to 6) ' 
        static i as long
        
        ' authorize Console Tools Pro
        ConsoleToolsAuthorize %MY_CT_AUTHCODE
        InitConsoleTools 0, 0, 0, 0, 0, 0 ' varies according to feature set used...
        
        if istrue(%timerMode > 0) then
            stdout "Starting client timeout timer..."
            OnTimer 1, codeptr(myTimer) ' run timer event once per second...
        end if
        
        ' exit on esc press
        DO
            myKey = INKEY$
            
            SLEEP 0
        LOOP UNTIL myKey = chr$(27)
    
    END FUNCTION
    
    '=========================================================================
    FUNCTION myTimer(BYVAL lTime AS LONG) AS LONG
        static i as long
        
        incr i
        if i > 5 then i = 0
        
        stdout "++++++++++++++++++++++++++++++++++++++"
        stdout "Index =" & str$(i) & " - Threadcount =" & str$(threadcount)
    
        if i = 4 then cleardata(i)
        
        stdout "Index =" & str$(i) & " - Threadcount =" & str$(threadcount)
        
    end function
    
    '=========================================================================
    sub cleardata(ndx as long)
        
        cStrings(ndx) = ""
        
    end sub
    Rick

  • #2
    I think it depends on how you implement the initialization and execution of your threads...maybe it would be clearer if you posted this part?
    Hobbyist progammer

    Comment


    • #3
      That could be, but I don't think so. And in any case, the above code fails in the same way without extra threads.

      I say extra threads because the Console Tools timer is still running in a seperate thread. It is interesting (to me, anyway) to note that if I use:

      Code:
      reset var
      instead of:

      Code:
      var = ""
      then there are no extra threads produced.
      Rick

      Comment


      • #4
        Hmm yeah, strange...and interesting
        Hobbyist progammer

        Comment


        • #5
          > OnTimer 1, codeptr(myTimer) ' run timer event once per second...

          What is "onTimer"?? That's not a PB verb or a WinAPI function I can find. Is that a Console Tools function?

          Regardless, It's probably doing something like this....

          Waitable Timer Object Demo June 2005

          .. which pretty much has to be done in an additional thread of execution.

          DUH... I just saw this...
          > 'Console Tools Pro Library (mainly for Timer functions)

          also...the PB THREADCOUNT function does NOT include threads created from another module. (or at least it didn't last time I tested it).

          I thought I posted a "threadcount" function which did but it's not in my file. I guess I will have to search here.... there it is! I thought I had done that.....but it's not in the source code forum, it's here....

          http://www.powerbasic.com/support/pb...5&postcount=22

          MCM
          Michael Mattias
          Tal Systems Inc.
          Racine WI USA
          mmattias@talsystems.com
          http://www.talsystems.com

          Comment


          • #6
            Checked out invention

            That is an amazing invention, Rick... I can see where it can be adapted to a lake or other clear waterway...

            Good luck spending your first million...

            Izzy

            Comment


            • #7
              MCM, Thanks. You're probably right. I'll give it a shot Monday and let you know how it goes.

              Izzy, wanna give me a lone on that 1st million? It's being held up in my Nigerian bank at the moment. Any day now tho! :-)
              Rick

              Comment


              • #8
                Clearing the string (doesn't actually matter which string in the array) creates a second thread, which doesn't go away. What incredibly obvious thingy am I missing?
                Just looking at this, at a minimum your THREAD CREATE and [THREAD] FUNCTION code is going to be required to solve whatever your problem is. Setting a string to length zero is not going to do anything to the operation of a thread function unless that funcyion is sitting in a loop testing that length.

                Assuming "OnTimer" is a CT function, I'm sure Eric would have some kind of "stop that timer" function and without some execeptionally "weird" code on your part you are not going to be able to do anything with the way that works, either deliberately or accidentally.

                That is, your problem is almost certainly located in the "code not shown."

                MCM
                Michael Mattias
                Tal Systems Inc.
                Racine WI USA
                mmattias@talsystems.com
                http://www.talsystems.com

                Comment


                • #9
                  I wrote the code not shown. So I'm sure it does have some errors. :-)

                  But the code operates exactly as shown above. See screenshot.
                  Attached Files
                  Rick

                  Comment


                  • #10
                    That has to be a problem with the THREADCOUNT function or somewhere else, because the code you posted in #1 does nothing which can affect the return value of the THREADCOUNT function.

                    If this PBCC v 5 or v 6....

                    Return the number of PowerBASIC-created active threads that exist in a module.
                    The number of PowerBASIC-created active threads in the module created by the code posted in #1 will always be one (1).

                    The return value will (should) be incremented by one (1) for each successful THREAD CREATE and decremented by one (1) for each succesful THREAD CLOSE. You execute neither.

                    I'd suggest you submit a bug report.... you have a compilable example of code demonstrating the problem so it should not take long at all.

                    (I am assuming the output shown in post #9 was created by the code in post #1. I am not a CT licensee or I'd run it myself. )
                    Last edited by Michael Mattias; 16 Apr 2012, 08:59 AM.
                    Michael Mattias
                    Tal Systems Inc.
                    Racine WI USA
                    mmattias@talsystems.com
                    http://www.talsystems.com

                    Comment


                    • #11
                      Michael,

                      Now that I have an endorsement by one of the Masters, I will do that.. I just wanted to be sure I wasn't missing something. I usually am.

                      Thanks for the input.
                      Rick

                      Comment


                      • #12
                        Waiting for someone else to "confirm" something is probably not a good idea.

                        First of all, any publisher WANTS to know if there is a bug in his software.

                        Second, if you have read the documentation and you can't get it to match the behavior of the software AND there are no bugs in the compiler, then there is probably something wrong with the documentation (probably something missing), and those errors have to be fixed, too.

                        MCM
                        Michael Mattias
                        Tal Systems Inc.
                        Racine WI USA
                        mmattias@talsystems.com
                        http://www.talsystems.com

                        Comment

                        Working...
                        X