PowerBASIC Peer Support Forums
 

Go Back   PowerBASIC Peer Support Forums > User to user Discussions > PowerBASIC Console Compiler

PowerBASIC Console Compiler User to user discussions about the PowerBASIC Console Compiler product line.

Reply
 
Thread Tools Display Modes
  #1  
Old Apr 13th, 2012, 04:47 PM
Rick McNeely Rick McNeely is offline
Member
 
Join Date: Oct 2006
Location: Oreana Illinois
Posts: 80
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
Reply With Quote
  #2  
Old Apr 13th, 2012, 05:23 PM
Yuzree Esmera Yuzree Esmera is offline
Member
 
Join Date: Sep 2004
Location: Kuala Lumpur, Malaysia
Posts: 48
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
Reply With Quote
  #3  
Old Apr 13th, 2012, 05:30 PM
Rick McNeely Rick McNeely is offline
Member
 
Join Date: Oct 2006
Location: Oreana Illinois
Posts: 80
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
Reply With Quote
  #4  
Old Apr 13th, 2012, 05:59 PM
Yuzree Esmera Yuzree Esmera is offline
Member
 
Join Date: Sep 2004
Location: Kuala Lumpur, Malaysia
Posts: 48
Hmm yeah, strange...and interesting
__________________
Hobbyist progammer
Reply With Quote
  #5  
Old Apr 13th, 2012, 06:55 PM
Michael Mattias Michael Mattias is offline
Member
 
Join Date: Aug 1998
Location: Racine WI USA
Posts: 36,919
> 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
Reply With Quote
  #6  
Old Apr 14th, 2012, 01:01 PM
Izzy Zeitz Izzy Zeitz is offline
Member
 
Join Date: Nov 2011
Location: Port Saint Lucie, FL
Posts: 261
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
Reply With Quote
  #7  
Old Apr 14th, 2012, 05:49 PM
Rick McNeely Rick McNeely is offline
Member
 
Join Date: Oct 2006
Location: Oreana Illinois
Posts: 80
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
Reply With Quote
  #8  
Old Apr 16th, 2012, 08:47 AM
Michael Mattias Michael Mattias is offline
Member
 
Join Date: Aug 1998
Location: Racine WI USA
Posts: 36,919
Quote:
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
Reply With Quote
  #9  
Old Apr 16th, 2012, 09:06 AM
Rick McNeely Rick McNeely is offline
Member
 
Join Date: Oct 2006
Location: Oreana Illinois
Posts: 80
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 Images
File Type: jpg sc.jpg (49.4 KB, 12 views)
__________________
Rick
Reply With Quote
  #10  
Old Apr 16th, 2012, 09:55 AM
Michael Mattias Michael Mattias is offline
Member
 
Join Date: Aug 1998
Location: Racine WI USA
Posts: 36,919
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....

Quote:
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; Apr 16th, 2012 at 09:59 AM.
Reply With Quote
  #11  
Old Apr 16th, 2012, 10:03 AM
Rick McNeely Rick McNeely is offline
Member
 
Join Date: Oct 2006
Location: Oreana Illinois
Posts: 80
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
Reply With Quote
  #12  
Old Apr 16th, 2012, 06:13 PM
Michael Mattias Michael Mattias is offline
Member
 
Join Date: Aug 1998
Location: Racine WI USA
Posts: 36,919
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1999-2011 PowerBASIC, Inc. All Rights Reserved.
Error in my_thread_global_end(): 1 threads didn't exit