Multithreads with FWH

Postby Antonio Linares » Thu Apr 10, 2008 6:26 pm

Reinaldo,

Threads are very usefull but they are not ready yet in Harbour/xharbour
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby modicr » Sun Apr 13, 2008 9:29 pm

Hello!
Antonio Linares wrote:Nageswararao,

Using the Windows API: ( CreateThread() )
http://msdn2.microsoft.com/en-us/library/ms682453(VS.85).aspx

Without using the Windows API:
http://en.wikipedia.org/wiki/Beginthread

Simple Multithreaded Application in pure C, Win32 and MFC
http://www.codeproject.com/KB/threads/c ... ading.aspx
Selvam is very much interested in sharing his knowledge with his peers and has been awarded as a Microsoft India Community Star in 2004

There are also some useful comments:
http://www.codeproject.com/KB/threads/c ... xx679159xx
Additionally, I would recommed to everyone who wants to learn about multithreading to read more than one article. Most articles that I've read are missing some important points; so by reading several of them, you should get the whole picture. This article, for example, doesn't mention the most important aspect of multithreading: synchronization. Additionally, reading different articles provides you with different techniques and approaches.
Especially in a complicated field like multithreading, the more you know, the better it is.


Thread Synchronization for Beginners
http://www.codeproject.com/KB/threads/S ... ation.aspx

Regards, Roman
© I'm not patented!
User avatar
modicr
 
Posts: 207
Joined: Fri Oct 07, 2005 7:58 am
Location: ljubljana, barje, slovenia

Re: Multithreads with FWH

Postby angelo.c » Fri Feb 13, 2009 6:55 am

Hello,

I was wondering if the multi-threading capability of xHarbour has advanced to a usable form.
I am using Harbour/xHarbour9.01 and BCC55

I tried to compile and run the samples mttest.prg and client.prg in the ..\xharbour\test directories. I get:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
mttest.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external '_HB_FUN_STARTTHREAD' referenced from D:\FIVETECH\XHARBOUR\TESTS\MTTEST.OBJ
Error: Unresolved external '_HB_FUN_WAITFORTHREADS' referenced from D:\FIVETECH\XHARBOUR\TESTS\MTTEST.OBJ
* Linking errors *


I then add in the the thread libraries vmmt.lib, rtlmt.lib, ppmt.lib, rddmt.lib, dbfntxmt.lib and dbcdxmt.lib and get:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
mttest.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external '__endthreadex' referenced from D:\FIVETECH\XHARBOUR\BETA\LIB\VMMT.LIB|thread
Error: Unresolved external '__beginthreadex' referenced from D:\FIVETECH\XHARBOUR\BETA\LIB\VMMT.LIB|thread
* Linking errors *


Is there any library(s) that I can use to resolve these two funcion calls.


Thanks, Angelo.C
angelo.c
 
Posts: 36
Joined: Thu Mar 30, 2006 11:19 am

Re: Multithreads with FWH

Postby Antonio Linares » Fri Feb 13, 2009 7:26 am

Angelo,

Please try to link Borland cw32mt.lib instead of cw32.lib
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Multithreads with FWH

Postby angelo.c » Mon Feb 16, 2009 1:03 am

Hello Antonio,

Thank you for your suggestion. I linked in cw32mt.lib => links OK. But when it runs under Windows XP (SP3) I get the following the standard XP "encountered a problem" window saying:

"mttest.exe has encountered a problem and needs to close. We are sorry for the inconvenience. etc etc etc "

Has anyone else run this test programme (mttest.prg under xHarbour) successfully using Win XP (SP3)?

Best Regards, Angelo.C
angelo.c
 
Posts: 36
Joined: Thu Mar 30, 2006 11:19 am

Re: Multithreads with FWH

Postby Antonio Linares » Mon Feb 16, 2009 7:39 am

Angelo,

No, we have not tested it, as we tend to support functionalitites that are available for both Harbour and xHarbour, as we keep FWH compatible with both of them.

Once multithreading is fully functional for both compilers, then we will recommend it and support it from FWH :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Multithreads with FWH

Postby angelo.c » Mon Feb 16, 2009 9:42 am

Antonio,

In that case, I will put on hold any code I need that requires multi-threading and change my design to accommodate new approaches to overcome this limitation.

Hopefully you will not leave it too long to implement multi-threading in both Harbour and xHarbour. That capability makes the code much more useful.

I want to compile and run my code that I have written in FWH with xHarbour since I really like the programming extensions that are available in xHarbour. It looks like it would make my code easier to write; be in line with "newer programming constructs" and be more versatile in interfacing with other programming languages such as c/c++.

Best Regards, Angelo.C
angelo.c
 
Posts: 36
Joined: Thu Mar 30, 2006 11:19 am

Re: Multithreads with FWH

Postby Antonio Linares » Mon Feb 16, 2009 11:06 am

Angelo,

If you review all the work done lately on Harbour, you will be greatly surprised :-)

Both compilers are very powefull, thats why we want to support and keep compatibility for both of them.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re:

Postby fraxzi » Tue Jun 22, 2010 6:24 am

reinaldocrespo wrote:Antonio/All;

Here is another example where threads could be very useful:

Suppose you have an xbrowse on a table of real time data, and while you have the browse in idle mode, someone inserts a new record to the table that pertains the data you are browsing. At the very least it would be nice to show a red dot somewhere to alert the user that he needs to refresh the browse to see newly inserted records.

So far I've been doing this with timers, but it is very inefficient and awkard at best.

This could be done with threads by having the app issue an "event" and have the browsing app listen for that specific threaded-event. ADS 9 adds support for notifications. A notification could be used on this example to inform a client application that data in a critical table has changed. The client app can then use this information to refresh the browse or turn a red dot somewhere without pausing to re-read the table every n seconds for critial updates. ie... no polling.

But ADS notifications are blocking calls (the call does not complete until a signal is received) therefore we would need to issue a new thread to attend this notifications and act accordingly all while the rest of the app goes on processing keystokes or whatever else.

Would you or someone else mind helping a bit more on how to use the C calls for multithreads from a fw app? I'd certainly like to test event notifications with ADS.

Thank you,

Reinaldo.


Dear Reinaldo,

I dug this old topic because right now I need the notification event of ADS9.1 to my FWH apps..
I'm using background processing with xharbour but these functions are making the app sluggish and not really multi-threading..

Can we revive this topic..

Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Multithreads with FWH

Postby reinaldocrespo » Tue Jun 22, 2010 1:19 pm

Frances;

Hi.

Threads is the natural way to consume a server message. The information about threading with xharbour is somewhat shady or at least confusing to me. Fortunately I "discovered" a way to listen to ADS server messages w/out spawning a new thread. Although it works for me (at least for now), I will eventually want to experiment with threads from xharbour. I will appreciate any info you finally come up with. Here is my trick:

sp_WaitForEvent() allows a client to synchronously wait for an event notification to be sent by the server. If an event notification for the specified event has already been sent, then this will return immediately (and that is the heart of my trick). In other words, the event notification does not have to occur while this procedure is actively waiting. This means that an application can check for an event periodically and use a small (or zero) timeout period. I set a fw-timer that executes the ADS stored procedure sp_WaitForEvent() with zero timeout period.

In a nutshell, I'm polling every x seconds for the event message. Once the message is received, I then trigger the desired action.

I've learned to look for ways to solve problems and sometimes I simply figure a work-around. I know this is not exactly what we are looking for, but for now it yields the desired results. I hope this helps.

Take care,


Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Multithreads with FWH

Postby sambomb » Tue Jun 22, 2010 4:53 pm

I have about 50 DBF's, and I delete and create again the index for each DBF at the first run of the day. Will multi-thread increase the speed? Since the threads don't call any visual component it'll no cause any trouble with fivewin right?
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
User avatar
sambomb
 
Posts: 388
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

Re: Multithreads with FWH

Postby fraxzi » Tue Jul 06, 2010 6:28 am

reinaldocrespo wrote:Frances;

Hi.

Threads is the natural way to consume a server message. The information about threading with xharbour is somewhat shady or at least confusing to me. Fortunately I "discovered" a way to listen to ADS server messages w/out spawning a new thread. Although it works for me (at least for now), I will eventually want to experiment with threads from xharbour. I will appreciate any info you finally come up with. Here is my trick:

sp_WaitForEvent() allows a client to synchronously wait for an event notification to be sent by the server. If an event notification for the specified event has already been sent, then this will return immediately (and that is the heart of my trick). In other words, the event notification does not have to occur while this procedure is actively waiting. This means that an application can check for an event periodically and use a small (or zero) timeout period. I set a fw-timer that executes the ADS stored procedure sp_WaitForEvent() with zero timeout period.

In a nutshell, I'm polling every x seconds for the event message. Once the message is received, I then trigger the desired action.

I've learned to look for ways to solve problems and sometimes I simply figure a work-around. I know this is not exactly what we are looking for, but for now it yields the desired results. I hope this helps.

Take care,


Reinaldo.


Dear Mr. Reinaldo,

I used this pseudo code

Code: Select all  Expand view

...
created another ADS handle for the event notification
...
update trigger has been created to table.. ' EXECUTE PROCEDURE sp_SignalEvent('tablename', FALSE, 0);'
...
Set( _SET_BACKGROUNDTASKS, .T. )
...
nTask := HB_BackGroundAdd( {|| uEvent() }, 5000, .F. )    //add to background task,
...
HB_BackGroundActive( nTask, .T. )

...
created 'EXECUTE PROCEDURE sp_CreateEvent( 'tablename', 0 );'
...

function uEvent()
 created 'EXECUTE PROCEDURE sp_WaitForAnyEvent(0,0,2)'
 ...
 here after the sp_WaitForAnyEvent ends..
 how can I call a UDF? or how to know EventCount or EventName?

return
 


the event occurs but dunno how/when a UDF must execute...

Im using ads 9.1


what's your solution?


Best Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Multithreads with FWH

Postby fraxzi » Tue Jul 06, 2010 7:34 am

Dear Mr. Reinaldo,

I GOT IT! :lol:


the return of sp_WaitForAnyEvent() in SQLALIAS are SQLALIAS->eventname and SQLALIAS->eventcount..
now if SQLALIAS->eventcount > 0 then call my UDF..


you made me think hard for this huh... hehehe


Thanks for the help and idea.

Best Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Multithreads with FWH

Postby fraxzi » Tue Jul 06, 2010 8:30 am

Dear All,

The problem now is the "2nd" thread..


:?:


Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Multithreads with FWH

Postby sambomb » Tue Jul 06, 2010 6:09 pm

I Need to do this, but the StartThread and WaitThread is not recognized
Code: Select all  Expand view


procedure RodaTabelas()
Local i := 0, aDbfs := {}, cTime := Time()

   aAdd(aDbfs,"ESTOQUE")
   aAdd(aDbfs,"CAIXA")
   aAdd(aDbfs,"PEDIDOS")
   aAdd(aDbfs,"TITULOS")
   
   //-- Open my DBFS and return an array with the Alias of each opened
   aDbfs := OpenDbfs(aDbfs)

   if .not. aDbfs[1]
      Dbcloseall()
      Return nil
   end
   
   For i := 1 to Len(aDbfs)

      StartThread({||RodarDbf(aDbfs[i])})
     
   end
   
   WaitForThreads()
   
   Msg("Rodou todos!;Tempo: " +AllTrim(Str(TimeFromStart(cTime))))
   
Return nil
     
     
Procedure RodarDbf(cDBF)

   Select(cDbf)
   
   DbGoTop()
   
   While !Eof()
      DbSkip()
   end
   
Return

 
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
User avatar
sambomb
 
Posts: 388
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

PreviousNext

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 33 guests