[Harbour] Example MutiThread Index

2010-04-06 Thread rafa
I believe this example is interesant.

Regards
Rafa Carmona

/*
   Example multiThreads index.
   One thread by table , and one thread by index.
   2010 Rafa Carmona

   Thread Main
|-  Thhread child table for test.dbf
|| Thread child index fname
||
||-Thread child index fcode
|
|-  Thhread child table for test2.dbf
|-Thread child index fname2


 */
#include hbthread.ch

proc Main( uCreate )
Local nSeconds
Local aFiles  := { test, test2 }// Arrays files dbf
Local aNtx:= { { fname, fcode },;   // files index for test
   { fName2 } }   // files index for test2
Local aExpr   := { { name, code },;
   { dtos(fecha)+str(code) }  } // Expresions
Local cDbf

if empty( lCreate )
   lCreate := 0
endif

setmode( 25,80 )
cls

if uCreate = 1
   ? Create test.dbf and test2.dbf
   dbCreate(test,{ {name,C,1,0 },{code,N,7,0 } } )
   use test
   while lastRec()  100
 dbAppend()
 field-name := chr( recno() )
 field-code := recno()
   enddo
   close
   dbCreate(test2,{ {fecha,D,8,0 },{code,N,7,0 } } )
   use test2
   while lastRec()  100
 dbAppend()
 field-fecha := date() + recno()
 field-code := recno()
   enddo
   close
endif

cls
// Threads
nSeconds := Seconds()
for each cDbf in aFiles
   ? Process.:  + cDbf
   hb_threadStart( @aCreateIndexe(), cDbf, aNtx[ cDbf:__enumindex ],
aExpr[ cDbf:__enumindex ], cDbf:__enumindex  )
next

? Wait for threads 
hb_threadWaitForAll()

? hb_valTostr( Seconds() - nSeconds )

? finish

return

function aCreateIndexe( cFile, aNtx, aExpr, nPosDbf )
   Local nContador := 1
   Local cFileNtx, cExpr
   Local nLong := Len( aNtx )
   Local aThreads := {}
   Local cAlias

   use ( cFile )
   cAlias := alias()
   hb_dbDetach( cAlias )  // Libero el alias

   for each cFileNtx in aNtx
   cExpr  := aExpr[ cFileNtx:__enumindex ]
   nContador := 1
   nPos := cFileNtx:__enumindex
   aadd( aThreads, hb_threadStart( @crea(), cAlias,cExpr,
cFileNtx, nPos, nPosDbf ) )
   next

   aEval( aThreads, { |x| hb_threadJoin( x ) } )  // wait threads childs
   hb_dbRequest( cAlias, , , .T.)  // Restaura el alias
   close

RETURN NIL

proc crea( cAlias, cExpr, cFileNtx, nPos, nPosDbf )
  Local nContador := 1

   hb_dbRequest( cAlias, , , .T.)  // Restaura el alias

   INDEX ON (cExpr) TO (cFileNtx) ;
 EVAL {|| hb_dispOutAt( nPosDbf, iif( nPos = 1, 20, 40 ),
alltrim( hb_valtostr( nContador) ), GR+/N ), nContador += INT(
LASTREC() / 100 ) , .T. } ;
 EVERY INT( LASTREC() / 100 )

   hb_dbDetach( cAlias )  // Libera el alias


return

___
Harbour mailing list (attachment size limit: 40KB)
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour


Re: [Harbour] Example MutiThread Index

2010-04-06 Thread Massimo Belgrano
Confirm is inrteressant
I have added to my blog
http://harbourlanguage.blogspot.com/search/label/MultiThread

2010/4/6 rafa r...@tesipro.com:
 I believe this example is interesant.

 Regards
 Rafa Carmona

 /*
   Example multiThreads index.
   One thread by table , and one thread by index.
   2010 Rafa Carmona

   Thread Main
        |-  Thhread child table for test.dbf
        |                        | Thread child index fname
        |                        |
        |                        |-Thread child index fcode
        |
        |-  Thhread child table for test2.dbf
                                |-Thread child index fname2


  */
 #include hbthread.ch

 proc Main( uCreate )
    Local nSeconds
    Local aFiles  := { test, test2 }        // Arrays files dbf
    Local aNtx    := { { fname, fcode },;   // files index for test
                       { fName2 } }           // files index for test2
    Local aExpr   := { { name, code },;
                       { dtos(fecha)+str(code) }  }     // Expresions
    Local cDbf

    if empty( lCreate )
       lCreate := 0
    endif

    setmode( 25,80 )
    cls

    if uCreate = 1
       ? Create test.dbf and test2.dbf
       dbCreate(test,{ {name,C,1,0 },{code,N,7,0 } } )
       use test
       while lastRec()  100
             dbAppend()
             field-name := chr( recno() )
             field-code := recno()
       enddo
       close
       dbCreate(test2,{ {fecha,D,8,0 },{code,N,7,0 } } )
       use test2
       while lastRec()  100
             dbAppend()
             field-fecha := date() + recno()
             field-code := recno()
       enddo
       close
    endif

    cls
    // Threads
    nSeconds := Seconds()
    for each cDbf in aFiles
       ? Process.:  + cDbf
       hb_threadStart( @aCreateIndexe(), cDbf, aNtx[ cDbf:__enumindex ],
 aExpr[ cDbf:__enumindex ], cDbf:__enumindex  )
    next

    ? Wait for threads 
    hb_threadWaitForAll()

    ? hb_valTostr( Seconds() - nSeconds )

    ? finish

 return

 function aCreateIndexe( cFile, aNtx, aExpr, nPosDbf )
       Local nContador := 1
       Local cFileNtx, cExpr
       Local nLong := Len( aNtx )
       Local aThreads := {}
       Local cAlias

       use ( cFile )
       cAlias := alias()
       hb_dbDetach( cAlias )  // Libero el alias

       for each cFileNtx in aNtx
           cExpr  := aExpr[ cFileNtx:__enumindex ]
           nContador := 1
           nPos := cFileNtx:__enumindex
           aadd( aThreads, hb_threadStart( @crea(), cAlias,cExpr,
 cFileNtx, nPos, nPosDbf ) )
       next

       aEval( aThreads, { |x| hb_threadJoin( x ) } )  // wait threads childs
       hb_dbRequest( cAlias, , , .T.)  // Restaura el alias
       close

 RETURN NIL

 proc crea( cAlias, cExpr, cFileNtx, nPos, nPosDbf )
      Local nContador := 1

       hb_dbRequest( cAlias, , , .T.)  // Restaura el alias

       INDEX ON (cExpr) TO (cFileNtx) ;
             EVAL {|| hb_dispOutAt( nPosDbf, iif( nPos = 1, 20, 40 ),
 alltrim( hb_valtostr( nContador) ), GR+/N ), nContador += INT(
 LASTREC() / 100 ) , .T. } ;
             EVERY INT( LASTREC() / 100 )

       hb_dbDetach( cAlias )          // Libera el alias


 return

 ___
 Harbour mailing list (attachment size limit: 40KB)
 Harbour@harbour-project.org
 http://lists.harbour-project.org/mailman/listinfo/harbour




-- 
Massimo Belgrano

Iscritto all'albo dei CTU presso il Tribunale di Novara per materia Informatica
Delta Informatica S.r.l. (http://www.deltain.it/) (+39 0321 455962)
Analisi e sviluppo software per Lan e Web -  Consulenza informatica - Formazione
___
Harbour mailing list (attachment size limit: 40KB)
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour