Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.clipper.visual-objects > #5391

Re: Thread safe code - any caveats?

From Martin <spam@spam.spam>
Newsgroups comp.lang.clipper.visual-objects
Subject Re: Thread safe code - any caveats?
References (5 earlier) <51c194b1$0$38134$c3e8da3$f6268168@news.astraweb.com> <vMgwt.107261$4C2.78184@fx23.fr7> <51c1b9c8$0$57971$c3e8da3$eb767761@news.astraweb.com> <QGkwt.122934$G56.87814@fx32.fr7> <51c2e5ec$0$63236$c3e8da3$3a1a2348@news.astraweb.com>
Message-ID <ARFwt.114422$P_4.83439@fx31.fr7> (permalink)
Date 2013-06-20 17:12 +0100

Show all headers | View raw


Thanks Ginny

Just been testing since we have got the server running (blew up after 
power cut despite UPS), with thread running the scanning code (main 
.exe) crashes accessing the barcode system. thread carries on running 
until I quit.

Not using servers in thread, but Clipper style, any access controlled 
with critical sections

Note the same type of error in two files, both are open in the thread 
with their own opens.

Arg:             uField
ArgNum:          0
Args:          1 BC_ORDER : SYMBOL
ArgType:         NIL
ArgTypeReq:      NIL
CallFuncSym:     FIELDGET
CanDefault:      .F.
CanRetry:        .F.
CanSubstitute:   .F.
Description:     Wrong field specification



Error Detail
Arg:             uField
ArgNum:          0
Args:          1 PL_DATE : SYMBOL
ArgType:         NIL
ArgTypeReq:      NIL
CallFuncSym:     FIELDGET
CanDefault:      .F.
CanRetry:        .F.
CanSubstitute:   .F.
Description:     Wrong field specification
FileHandle:      0
FileName:
FuncPtr:         0x00000000
FuncSym:         FIELDGET
GenCode:         1 : Argument error
MaxSize:         0
MethodSelf:      BarList
Operation:
OsCode:          0 : Successful completion
Severity:        ERROR
SubCode:         0
SubCodeText:
SubstituteType:  NIL
SubSystem:       Database
Tries:           0




On 20/06/2013 12:22, Ginny Caughey wrote:
> Here are the wrapper functions I'm using:
>
> FUNCTION AddDbf()
>     LOCAL dwStop AS DWORD
>
>     WHILE TRUE
>         dwStop := GetTickCountLow()+5000
>         WHILE GetTickCountLow() < dwStop
>                DBAPPEND()
>                IF ! NetErr()
>                   RETURN TRUE
>                ENDIF
>                Sleep(100)
>         ENDDO
>         IF MessageBox(0, PSZ("Unable to add a record to file"), ;
>             PSZ("Append Failure"), MB_RETRYCANCEL + MB_ICONEXCLAMATION)
> != IDRETRY
>             EXIT
>         ENDIF
>     END
>     RETURN FALSE
> STATIC FUNCTION ConstructUniqueAlias( cFileName AS STRING ) AS SYMBOL
> PASCAL
>     // Borrowed from 1.0c SDK in dbfsrvr.prg
>     LOCAL cTryNewAlias AS STRING
>     LOCAL w AS DWORD
>
>     IF IsNil(cFileName)
>         cFileName := "NOFILENAME"
>     ELSEIF ( w := At( ".", cFileName ) )>0
>         cFileName := SubStr( cFileName, 1, w-1 )
>     ENDIF
>     w := 1
>     cTryNewAlias := Upper(cFileName)
>
>     DO WHILE VODBSymSelect( String2Symbol( cTryNewAlias ) )>0
>         cTryNewAlias := Upper(cFileName)+"_"+AllTrim( Str( w++ ) )
>     ENDDO
>
>     RETURN String2Symbol(cTryNewAlias)
> FUNCTION XOpenDBF(cFilename)
>     RETURN OpenDbf(cFilename, , TRUE)
> FUNCTION OpenDbf(cFilename, cAliasname, lExclusive)
>     LOCAL dwStop AS DWORD
>     LOCAL lRet AS LOGIC
>
>     IF IsNil(lExclusive)
>         lExclusive := FALSE
>     ENDIF
>     IF IsNil(cAliasname)
>         cAliasname := cFilename
>     ENDIF
>     IF SELECT(cAliasname) != 0    // Alias already in use
>         cAliasname := Symbol2String(ConstructUniqueAlias(cFilename))
>     ENDIF
>     BEGIN SEQUENCE
>         WHILE TRUE
>             dwStop := GetTickCountLow()+5000 // allow .5 sec. retry
>             WHILE GetTickCountLow() <= dwStop
>                 Sleep(100)
>                 DBUSEAREA(TRUE, , cFilename, cAliasname, !lExclusive,
> FALSE)
>                 // use (cFilename) ALIAS (cAliasname) shared new
>                 IF ! NetErr()
>                     lRet := TRUE
>                     BREAK
>                 ENDIF
>             END
>             IF MessageBox(0, PSZ("Unable to open file "+cFilename), ;
>                 PSZ("File Open Error"), MB_RETRYCANCEL +
> MB_ICONEXCLAMATION) != IDRETRY
>                 EXIT
>             ENDIF
>         ENDDO
>     END SEQUENCE
>     IF lRet
>         cFilename := Upper(cFilename)
>         DO CASE
>             CASE cFilename = "RATECODE"
>                 DBFIELDINFO(DBS_ALIAS, FieldPos("code"), "_code")
>             CASE cFilename = "TICKET"
>                 DBFIELDINFO(DBS_ALIAS, FieldPos("date"), "_date")
>             CASE cFilename = "INVOICE"
>                 DBFIELDINFO(DBS_ALIAS, FieldPos("date"), "_date")
>             CASE cFilename = "HISTORY"
>                 DBFIELDINFO(DBS_ALIAS, FieldPos("date"), "_date")
>         ENDCASE
>     ENDIF
>     RETURN lRet
> FUNCTION AddDbfQuiet()
>     LOCAL nTries := 0 AS INT
>
>     EnterCriticalSection(@CS_WATCHERR)
>     WHILE nTries < 10
>
>            DBAPPEND(TRUE)
>            IF !NetErr()
>                LeaveCriticalSection(@CS_WATCHERR)
>               RETURN TRUE
>            ENDIF
>            ++nTries
>            Sleep(50)
>     ENDDO
>     LeaveCriticalSection(@CS_WATCHERR)
>     RETURN FALSE
> FUNCTION EditDbf()
>     LOCAL nTries := 0 AS INT
>
>     WHILE nTries < 10
>         ++nTries
>         //IF DBRLOCK(RECNO())
>         IF DBRLOCK()
>             RETURN TRUE
>         ENDIF
>         IF nTries < 5
>             Sleep(100)
>             LOOP
>         ENDIF
>         IF MessageBox(0, PSZ("Unable to lock record"), ;
>             PSZ("Record Lock Failure"), MB_RETRYCANCEL +
> MB_ICONEXCLAMATION) != IDRETRY
>             RETURN FALSE
>         ENDIF
>     ENDDO
>     RETURN FALSE
> FUNCTION OpenDbfQuiet(cFilename, cAliasname, lExclusive)
>     LOCAL nTries := 0 AS INT
>
>     EnterCriticalSection(@CS_WATCHERR)
>     IF IsNil(lExclusive)
>         lExclusive := FALSE
>     ENDIF
>     IF IsNil(cAliasname)
>         cAliasname := cFilename
>     ENDIF
>     IF SELECT(cAliasname) != 0    // Alias already in use
>         cAliasname := Symbol2String(ConstructUniqueAlias(cFilename))
>     ENDIF
>
>     WHILE nTries < 10
>         ++nTries
>         DBUSEAREA(TRUE, "DBFCDX", cFilename, cAliasname, !lExclusive,
> FALSE)
>         IF !NetErr()
>             LeaveCriticalSection(@CS_WATCHERR)
>             RETURN TRUE
>         ENDIF
>         Sleep(50)
>     END
>     LeaveCriticalSection(@CS_WATCHERR)
>     RETURN FALSE
>
> Ginny
>
> "Martin"  wrote in message news:QGkwt.122934$G56.87814@fx32.fr7...
>
> Very interesting, you are using direct DBF rather than servers, I will
> try that thankyou
>
> On 19/06/2013 15:02, Ginny Caughey wrote:
>> Martin,
>>
>> I'm really not seeing anything obviously wrong here. Have you tried it
>> without the Dynlock/Dynunlock? Here's my threaded code that ran without
>> issues for several years. Maybe you'll spot something useful:
>>
>> FUNCTION RunPollingLoop( pInfo AS WatchInfo ) AS DWORD PASCAL    //
>> Threaded function
>>     LOCAL uMsg IS _winMsg    // used by thread message loop
>>     LOCAL aParity AS ARRAY
>>     LOCAL i AS INT
>>     LOCAL cScale, cParity AS STRING
>>     LOCAL nWeight, nTopWeight, nMinimumWt, nDelay AS INT
>>     LOCAL dwStop AS DWORD
>>     LOCAL dDate AS DATE
>>     LOCAL cTime AS STRING
>>     LOCAL oReader AS ScaleWatcher    // object that actually reads the
>> scale and performs NTEP weight validation
>>     LOCAL control, watchman, wconfig AS DWORD    // workareas for dbf
>> files
>>     LOCAL lError AS LOGIC
>>
>>     // Update status bar with scale started text
>>     PostMessage(pInfo.hWnd, MSG_StartStop, pInfo.ScaleNumber,
>> ScaleStarted)
>>
>>     // read paramters for this scale from wconfig and control files
>>     aParity := { {"E", EvenParity}, ;
>>                 {"M", MarkParity}, ;
>>                 {"N", NoParity}, ;
>>                 {"O", OddParity}, ;
>>                 {"S", SpaceParity}     }
>>
>>     BEGIN SEQUENCE
>>         IF !OpenDbfQuiet("control")
>>             BREAK
>>         ENDIF
>>         control := DBGETSELECT()
>>         IF !OpenDbfQuiet("wconfig")
>>             BREAK
>>         ENDIF
>>         wconfig := DBGETSELECT()
>>         IF !OpenDbfQuiet("watchman")
>>             BREAK
>>         ENDIF
>>         watchman := DBGETSELECT()
>>     RECOVER
>>         lError := TRUE
>>         HandleError("Unable to open a file needed to run Watchman Mode
>> processing.")
>>         IF control > 0
>>             DBSELECTAREA(control)
>>             DBCLOSEAREA()
>>         ENDIF
>>         IF wconfig > 0
>>             DBSELECTAREA(wconfig)
>>             DBCLOSEAREA()
>>         ENDIF
>>         IF watchman > 0
>>             DBSELECTAREA(watchman)
>>             DBCLOSEAREA()
>>         ENDIF
>>     END SEQUENCE
>>
>>     IF lError
>>         RETURN 0
>>     ENDIF
>>
>>     PostMessage(pInfo.hWnd, MSG_GettingData, pInfo.ScaleNumber, 0)
>>     oReader := ScaleWatcher{pInfo.hWnd}
>>     oReader:CommPort := "Com"+ NTrim(pInfo.PortNumber)
>>     Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - Getting info for
>> scale")
>>
>>     // Get info for this scale port from control file
>>     cScale := Str(pInfo.ScaleNumber,1,0)
>>     nMinimumWt := (wconfig)->minimumwt
>>     nDelay := (wconfig)->interval
>>     (wconfig)->(DBCLOSEAREA())
>>
>>     oReader:Baud := (control)->(FIELDGET(FieldPos("baud"+cScale)))
>>     oReader:Databits :=
>> (control)->(FIELDGET(FieldPos("databits"+cScale)))
>>     oReader:Stopbits :=
>> (control)->(FIELDGET(FieldPos("stopbits"+cScale)))
>>     IF oReader:Stopbits = 1
>>         oReader:Stopbits := OneStopBit    // convert to Windows value (2
>> = TwoStopBits already)
>>     ENDIF
>>     cParity := (control)->(FIELDGET(FieldPos("parity"+cScale)))
>>     i := AScan(aParity, {|aVal| aVal[1] = cParity})
>>     IF i > 0
>>         oReader:Parity := aParity[i,2]
>>     ELSE
>>         oReader:Parity := NoParity
>>     ENDIF
>>     oReader:Buffsize :=
>> (control)->(FIELDGET(FieldPos("buffsize"+cScale)))
>>     oReader:WtBegin :=  (control)->(FIELDGET(FieldPos("wtbegin"+cScale)))
>>     oReader:WtLength :=
>> (control)->(FIELDGET(FieldPos("wtlength"+cScale)))
>>     oReader:CheckChar :=
>> (control)->(FIELDGET(FieldPos("poll1chr"+cScale)))
>>     oReader:CheckPosition :=
>> (control)->(FIELDGET(FieldPos("poll2chr"+cScale)))
>>     oReader:MaxRetries :=
>> (control)->(FIELDGET(FieldPos("poll3chr"+cScale)))
>>     oReader:MotionChar :=
>> (control)->(FIELDGET(FieldPos("poll4chr"+cScale)))
>>     oReader:MotionByte := (control)->(FIELDGET(FieldPos("ack"+cScale)))
>>     oReader:GrossLoc :=
>> (control)->(FIELDGET(FieldPos("grossloc"+cScale)))
>>     oReader:UnitLoc := (control)->(FIELDGET(FieldPos("unitloc"+cScale)))
>>     oReader:ScaleUnit := (control)->(FIELDGET(FieldPos("scaleunit")))
>>     (control)->(DBCLOSEAREA())
>>
>>     // Run polling loop until WM_QUIT received from primary thread
>>     // Polling is a two step process here:
>>     //    1. Sample every nDelay seconds for a weigh large enough to
>> indicate a vehicle
>>     //    2. Once a vehicle is on the scale, sample at 1/10 second
>> intervals to catch maximum weight
>>     WHILE TRUE
>>         Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - In polling loop")
>>         //PostMessage(pInfo.hWnd, MSG_Polling, pInfo.ScaleNumber, 0)
>>         IF PeekMessage(@uMsg, 0, 0, 0, PM_REMOVE)    // PeekMessage
>> returns true if any messages in queue
>>             IF uMsg.message == WM_QUIT
>>                 EXIT
>>             ENDIF
>>         ENDIF
>>         Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - Getting first
>> weight")
>>         nWeight := oReader:ReadScale()
>>         Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - nWeight is
>> "+NTrim(nWeight))
>>         IF oReader:CancelRequested
>>             EXIT
>>         ENDIF
>>         IF nWeight = -1    // Error occurred
>>             PostMessage(pInfo.hWnd, MSG_ScaleError, pInfo.ScaleNumber, 0)
>>         ELSEIF nWeight < nMinimumWt
>>             dwStop := GetTickCountLow()+DWORD(nDelay * 1000)
>>             //PostMessage(pInfo.hWnd, MSG_Polling, pInfo.ScaleNumber, 0)
>>             WHILE GetTickCountLow() <= dwStop
>>                 IF PeekMessage(@uMsg, 0, 0, 0, PM_REMOVE)
>>                     IF uMsg.message == WM_QUIT
>>                         EXIT
>>                     ENDIF
>>                 ENDIF
>>             END
>>         ELSE
>>             // We have a vehicle on scale
>>             // Sample every 1/10 second until its maximum weight is
>> captured
>>             PostMessage(pInfo.hWnd, MSG_Sampling, pInfo.ScaleNumber, 0)
>>             nTopWeight := nWeight
>>             dDate := Today()
>>             cTime := Time24()
>>             WHILE TRUE
>>                 IF PeekMessage(@uMsg, 0, 0, 0, PM_NOREMOVE)
>>                     IF uMsg.message == WM_QUIT
>>                         EXIT
>>                     ENDIF
>>                 ENDIF
>>                 Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - Testing
>> for maximum weight")
>>                 nWeight := oReader:ReadScale()
>>                 IF oReader:CancelRequested
>>                     EXIT
>>                 ENDIF
>>                 IF nWeight < nMinimumWt .AND. !oReader:MotionDetected
>>                     EXIT
>>                 ENDIF
>>                 IF nWeight > nTopWeight
>>                     nTopWeight := nWeight
>>                     dDate := Today()
>>                     cTime := Time24()
>>                 ENDIF
>>                 Sleep(100)    // give time slice to other threads for
>> 1/10 second
>>             END
>>             IF uMsg.message != WM_QUIT .AND. !oReader:CancelRequested
>>                 // Write out weight to file using file sharing
>>                 IF (watchman)->(AddDbfQuiet())
>>                     (watchman)->today := dDate
>>                     (watchman)->time24 := cTime
>>                     (watchman)->weight := nTopWeight
>>                     (watchman)->scaleno := Str(pInfo.ScaleNumber,1,0)
>>                     (watchman)->(DBCOMMIT())
>>                     (watchman)->(DBUNLOCK())
>>                 ELSE
>>                     HandleError("Unable to add a record to the watchman
>> file.")
>>                 ENDIF
>>             ELSE
>>                 EXIT
>>             ENDIF
>>         ENDIF
>>         IF uMsg.message == WM_QUIT .OR. oReader:CancelRequested
>>             EXIT
>>         ENDIF
>>     END
>>     Output("Worker "+NTrim(pInfo.ScaleNumber)+"  - Shutting down thread")
>>     (watchman)->(DBCLOSEAREA())
>>     RETURN 0
>> FUNCTION Output( s AS STRING ) AS VOID PASCAL    // thread-safe way to
>> write to log file
>>     IF hDebug != NULL_PTR
>>         EnterCriticalSection(@CS_DEBUG)
>>         FWriteLine(hDebug, s)
>>         LeaveCriticalSection(@CS_DEBUG)
>>     ENDIF
>>     RETURN
>> FUNCTION HandleError(cError AS STRING) AS VOID PASCAL
>>     LOCAL hFile AS PTR
>>
>>     // Thread-safe write to log file
>>     EnterCriticalSection(@CS_WATCHERR)
>>     IF File(GetDefault()+"watcherr.log")
>>         hFile := FOpen(GetDefault()+"watcherr.log", FO_READWRITE)
>>         FSeek(hFile, 0, FS_END)
>>     ELSE
>>         hFile := FCreate(GetDefault()+"watcherr.log")
>>     ENDIF
>>     IF hFile != F_ERROR
>>         FWrite(hFile, DToC(Today())+" "+Time24()+"
>> "+cError+_CHR(13)+_CHR(10))
>>         FClose(hFile)
>>     ENDIF
>>     LeaveCriticalSection(@CS_WATCHERR)
>> Ginny
>>
>> "Martin"  wrote in message news:vMgwt.107261$4C2.78184@fx23.fr7...
>>
>> Here we go Ginny
>>
>>
>> Thanks
>>
>> Martin
>>

Back to comp.lang.clipper.visual-objects | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-18 11:36 +0100
  Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-18 06:59 -0400
    Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-18 12:05 +0100
      Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-18 12:41 -0400
        Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-19 09:14 +0100
          Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-19 07:24 -0400
            Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-19 12:40 +0100
              Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-19 10:02 -0400
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-19 17:07 +0100
                Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-20 07:22 -0400
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-20 17:12 +0100
                Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-20 14:04 -0400
                Re: Thread safe code - any caveats? "Martin \(BT\)" <martin-spam@btinternet.com> - 2013-06-20 20:27 +0100
                Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-21 06:54 -0400
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-21 15:39 +0100
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-21 15:54 +0100
                Re: Thread safe code - any caveats? "Ginny Caughey" <gcaughey@online.hotmail.com> - 2013-06-21 12:12 -0400
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-21 17:15 +0100
              Re: Thread safe code - any caveats? dlzc <dlzc1@cox.net> - 2013-06-19 08:04 -0700
                Re: Thread safe code - any caveats? Martin <spam@spam.spam> - 2013-06-19 17:04 +0100

csiph-web