FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Una Sobre la Clase Thread
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Una Sobre la Clase Thread

Posted: Wed May 14, 2008 11:14 AM

Muy buenas foro, estoy interesado en utilizar hilos en mi proyecto pero la primera me da error al compilar me dice que no encuentra la clase Thread, yo compila con xHarbour v.1.1.0 y fwh712. ¿Algún ejemplo que no sea el que viene en samples de hilso?

Un Saludo.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Una Sobre la Clase Thread

Posted: Wed May 14, 2008 02:35 PM

Que necesitas hacer con los threads ?

Te lo pregunto porque algunas cosas podrás hacerlas, pero otras no.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Wed May 14, 2008 03:19 PM

Lo que necesito es mandar informacion a 6 aparatos, cuando le mando la informacion al 1º tengo que esperar a que me responda para poder mandar al 2º y así hasta el 6º y todo esto se tiene que ejecutar transparente al para el operador, es decir, en un timer se va mandando la informacion pero si no utilizo thread o algo parecido se queda un rato bloqueado el software.

Un Saludo. y Gracias Antonio.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Una Sobre la Clase Thread

Posted: Wed May 14, 2008 06:41 PM
El código es a nivel PRG ó desde C ?

Desde C funcionará correctamente pues el propio lenguaje C soporta la multitarea. Pero a nivel de PRG depende del multithreading de xHarbour que puede darte problemas. Tendrás que probar y ver si te funciona.

Aqui tienes un ejemplo de multithreading de xHarbour en modo consola:
*
* Complex example of Multi thread usage
*
* Giancarlo Niccolai
* $Id: mtcomplex.prg,v 1.17 2004/02/22 07:36:19 jonnymind Exp $
*
* Here we have a main thread counting, and some secondary
* threads counting too (in different fashons).
* A control thread is notified when each secondary
* thread finishes, and some of the secondary threads
* are killed before they are able to reach the end.
*

PROCEDURE Main()
   LOCAL i
   LOCAL Mutex := HB_MutexCreate()
   LOCAL Mutex2 := HB_MutexCreate()
   LOCAL Thread4Handle, MonitorHandle
   LOCAL bKill := .F.

   Mutex2 := NIL
   SET OUTPUT SAFETY ON

   set color to w+/b
   CLEAR SCREEN
   @1,15 SAY "X H A R B O U R - Complex multithreading test"
   @3,17 SAY "Press any key to terminate in every moment"

   StartThread ( @ThreadFunc(), 10, "1st. Thread", 100, Mutex )
   StartThread ( @ThreadFunc(), 11, "2nd. Thread", 250, Mutex )

   /* Test of the { codeblock } grammar */
   StartThread ( { | nRow, cName, nLoops, Mtx| ThreadFunc(nRow, cName, nLoops, Mtx) } ;
         , 12, "3rd. Thread", 300, Mutex )

   Thread4Handle := StartThread( @ThreadFunc(), 13, "4th. Thread", 700, Mutex )

   /* Notice the "function name" grammar */
   MonitorHandle := StartThread ( "MonitorFunc", Mutex )
   *MonitorHandle := StartThread ( @MonitorFunc() , Mutex )

   StartThread ( @FourthMonitor(), Thread4Handle, Mutex )

   FOR i := 0 TO 500
      @ 14, 10 SAY 'Main Thread:' + Str( i, 4 )

      IF i == 100
         StopThread( Thread4Handle )
         @ 14, 27 SAY "(Killed 4th. Thread!)" 
      ENDIF

      HB_GCAll( .T. )
      ThreadSleep( 30 )
      IF Inkey() != 0
         bKill := .T.
         EXIT
      ENDIF
   NEXT

   IF bKill
      @17, 10 SAY 'Killing all threads on user requests      '
      KillAllThreads()
   ELSE
      @ 17, 10 SAY 'Cycle over, stopping Monitor     '
      KillThread( MonitorHandle )
      @ 17, 10 SAY 'Cycle over, Monitor Stopped     '
   ENDIF
      
   @18, 10 SAY 'Waiting for thread termination                  '      
   WaitforThreads()

   @ 19, 10 SAY 'Program over - press a key    '
   Inkey( 0 )

   @ 24, 00

PROCEDURE ThreadFunc( nRow, cName, nMax, Mutex )

   LOCAL i

   FOR i := 0 TO nMax
      @ nRow, 10 SAY cName + Str( i, 4 )
      WaitForThis( nRow ) // calling a proc inside a thread
   NEXT

   Notify( Mutex, cName )
   @ nRow, 10 SAY cName + ': DONE '+ space(20)
RETURN

PROCEDURE WaitForThis( nRow )

   ThreadSleep( 30  )

RETURN

PROCEDURE MonitorFunc( Mutex )

   LOCAL cName

   @ 5, 5 SAY "Entering Monitor Thread"
   @ 6, 5 SAY "->> Thread finished:"

   DO WHILE .T.
      cName := Subscribe( Mutex )

      IF cName != NIL
         @ 6, 26 SAY cName
      ELSE
         @ 6, 26 SAY "Unknown ..             " 
      ENDIF

      ThreadSleep( 1500 )
      @ 6, 26 SAY "                          "
   ENDDO

RETURN

PROCEDURE FourthMonitor( ThreadHandle, Mutex )

   @ 8, 5 SAY "Waiting for 4th. Thread to finish..."

   JoinThread( ThreadHandle )

   @ 8, 5 SAY "FourthMonitor: 4th. Thread Finished/Killed!"

RETURN
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Errores de link

Posted: Thu May 15, 2008 07:17 AM

Muy buenas, a la hora de link me da estos errores:
Error: Unresolved external '_HB_FUN_HB_MUTEXCREATE' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_STARTTHREAD' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_STOPTHREAD' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_KILLALLTHREADS' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_KILLTHREAD' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_WAITFORTHREADS' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_NOTIFY' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_SUBSCRIBE' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ
Error: Unresolved external '_HB_FUN_JOINTHREAD' referenced from Z:\FWH712\PROYECTOS\BUSCDI\FUENTES\THREAD.OBJ

Un Saludo.

Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Thu May 15, 2008 07:27 AM

¿Algún ejemplo en C?

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Una Sobre la Clase Thread

Posted: Thu May 15, 2008 08:55 AM

Tienes que enlazar las librerías de xHarbour cuyos nombres terminan en "MT" (multi threading).

Ejemplos de multithreads en lenguaje C:

Usando el API de Windows: ( CreateThread() )

http://msdn2.microsoft.com/en-us/library/ms682453(VS.85).aspx

Sin usar el API de Windows:

http://en.wikipedia.org/wiki/Beginthread

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Thu May 15, 2008 09:41 AM

Muy buenas Antonio, estoy buscando las librerías pero no las encuentro porfavor dime donde las puedo descargar.

Un Saludo.

Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Thu May 15, 2008 10:30 AM

Muy buenas ya he encontrado las librerias pero me dan este error

Error: Unresolved external '__endthreadex' referenced from Z:\FWH712\XHARBOUR\LIB\VMMT.LIB|thread
Error: Unresolved external '__beginthreadex' referenced from Z:\FWH712\XHARBOUR\LIB\VMMT.LIB|thread

¿Que puedo hacer?

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Una Sobre la Clase Thread

Posted: Thu May 15, 2008 10:36 AM

Tienes que enlazar cw32mt.lib de Borland, en vez de cw32.lib

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Thu May 15, 2008 10:47 AM

Antonio, ¿Me puedes pasar esta librería?

Un Saludo.

Posts: 161
Joined: Wed Jan 25, 2006 10:45 AM

Una Sobre la Clase Thread

Posted: Thu May 15, 2008 11:04 AM

c:\bcc55\lib

revisa en esa carpeta

Saludos

Saludos

Ricardo R.

xHarbour 1.1.0 Simplex , Microsoft Visual Studio 2008, Bcc55, Fwh Build. 9.01
Posts: 485
Joined: Fri Feb 09, 2007 10:34 AM

Re:

Posted: Thu May 15, 2008 11:34 AM

Muy buenas, ya he conseguido compilar con exito el ejemplo anterior pero al ejecutarlo se me sale del programa y el error por lo visto proviene de la libreria ModName: ntdll.dll.

Teneis un ejemplo simple con hilos que funcione.

UnSaludo.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Una Sobre la Clase Thread

Posted: Thu May 15, 2008 07:51 PM

Para probar ese ejemplo no enlaces las librerías de FWH

Es un ejemplo para modo consola

regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion