Competition between threads

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: Competition between threads

Postby rlebeau » Wed Aug 01, 2018 2:23 pm

mark_c wrote:but, I still have many doubts.


That is because you don't seem to understand how threads actually work in general.

mark_c wrote:I add another method, MyCycle, to understand which of the two threads does MyCycle for example:
Thread1 or Thread2?


Whichever thread context it is called it.

mark_c wrote:And the Label1, is accessed a quantum of time by each Thread?


I don't understand what you are asking.

mark_c wrote:notes: I tried to use GetCurrentThreadId () to understand which of the two threads is using the MyCycle () method every time but I think it only displays the ID of the UI thread


That is because you are calling GetCurrentThreadId() in the context of the UI thread only, never in the context of the worker threads, because you are synchronizing MyCycle(). The whole purpose of Synchronize() is to execute a method in the context of the UI thread, not in the context of the worker thread that is calling Synchronize(). GetCurrentThreadId() returns the ID of the thread that is calling it, which in your case is the UI thread.

Use the TThread::ThreadID property instead:

Code: Select all
Form1->Label2->Caption = this->ThreadID;//GetCurrentThreadId();
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1516
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Competition between threads

Postby mark_c » Thu Aug 02, 2018 12:06 am

Yes, Remy,
I have not yet very clear the operation of the threads but thanks to you I begin to understand.

If in this last example #5, I wanted Thread1 to run only MySincrSock1 () and Thread2 just run MySincrSock2 () separately, do I have to implement two Execute () methods?


note: in this last version I understood that the two threads are running n times and each time is decided by the windows scheduler, but the two threads do the same thing. Ultimately, this realization, as it is, serves no purpose, apart from the purely didactic purpose.
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 111
Joined: Thu Jun 21, 2012 1:13 am

Re: Competition between threads

Postby rlebeau » Thu Aug 02, 2018 1:36 pm

mark_c wrote:If in this last example #5, I wanted Thread1 to run only MySincrSock1 () and Thread2 just run MySincrSock2 () separately, do I have to implement two Execute () methods?


A TThread class can have only 1 Execute method. So, you would have to either:

- make Execute() decide whether to call MySincrSock1() or MySincrSock2() based on some criteria. If you look back at the example I gave you on July 24, I used a single TThread class and passed it a TServerSocket and a String as input to its constructor, then Execute() simply sent the given String to whichever TServerSocket it was given.

- write separate TThread classes, one for each TServerSocket, with their individual Execute() methods doing different things with their assigned TServerSocket as needed.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1516
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Competition between threads

Postby mark_c » Thu Aug 02, 2018 10:47 pm

Thanks Remy,
so, if I need 10 threads that perform completely different tasks, do I have to declare 10 classes, each with its own Execute() method?
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 111
Joined: Thu Jun 21, 2012 1:13 am

Re: Competition between threads

Postby rlebeau » Tue Aug 07, 2018 12:12 pm

mark_c wrote:so, if I need 10 threads that perform completely different tasks, do I have to declare 10 classes, each with its own Execute() method?


If those 10 tasks are using completely different coding, then using 10 separate classes makes sense, yes.

If those 10 task are using same/similar coding, then using 10 separate classes does not make sense, no. A single class with branching criteria makes more sense.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1516
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Previous

Return to Technical

Who is online

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

cron