Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Concurrent processes running via TThreads causing application to freeze.


This question is not answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Dec 19, 2016 9:01 AM Last Post By: Alex Belo Threads: [ Previous | Next ]
Toby Dobbs

Posts: 77
Registered: 9/24/16
Concurrent processes running via TThreads causing application to freeze.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 16, 2016 4:08 AM
Hi,

I have an application which makes use of two additional threads to carry out the following

1) Populate a buffer of information and plot it along the chart
2) Compute the average of the buffer

In order for these to work alongside each other I feel I need to pause the first thread (so that the buffer remains static whilst the calclation is made) and then restart it once the calculation has finished. I have read that I shouldn't use Suspend and Resume so how should I go about this?
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Concurrent processes running via TThreads causing application to freeze.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 16, 2016 7:56 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

Hi,

I have an application which makes use of two additional threads to
carry out the following

1) Populate a buffer of information and plot it along the chart
2) Compute the average of the buffer

In order for these to work alongside each other I feel I need to
pause the first thread (so that the buffer remains static whilst the
calclation is made) and then restart it once the calculation has
finished. I have read that I shouldn't use Suspend and Resume so how
should I go about this?

You should use a critical section. A quick google search returned this
article: http://edn.embarcadero.com/article/22411.
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Concurrent processes running via TThreads causing application to freeze.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 16, 2016 8:39 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

I have an application which makes use of two additional threads to
carry out the following

1) Populate a buffer of information and plot it along the chart

Be sure that you don't work with VCL visual components (like TChart)
from secondary threads directly. (I think you know about it)

2) Compute the average of the buffer

In order for these to work alongside each other I feel I need to
pause the first thread (so that the buffer remains static whilst the
calclation is made) and then restart it once the calculation has
finished. I have read that I shouldn't use Suspend and Resume so how
should I go about this?

You can use a critical section (CS) for this. Any thread which share
the same data with other threads should lock the CS before using the
data and unlock it when data are not in use in this thread. If the CS
is already locked by one thread then another thread will be temporary
suspended by Windows on attempt to lock the same CS and resumed again
on release of the CS.

type recordTypeName = record
TCriticalSection CS;
field1: type1;
field2: type2;
...
end;

(I'm not "Delphist" so I don't know if Delphi records can have a
constructor. If this possible it is better to initialize CS in
constructor of the record and free it in destructor)

var
Rec: recordTypeName;

// initialization
Rec.CS=TCriticalSection.Create;

In thread #1
Rec.CS.Enter;
// if Rec.CS is already locked by thread #2
// thread #1 will be temporary suspended inside
// Rec.CS.Enter until thread #2 call Rec.CS.Leave
try
field1:=...
if field2=...
...
finally
Rec.CS.Leave;
end

In thread #2
Rec.CS.Enter;
// the same here
try
if field2=...
field1=...
...
finally
Rec.CS.Leave;
end

// finalization
Rec.CS.Free;

--
Alex
Toby Dobbs

Posts: 77
Registered: 9/24/16
Re: Concurrent processes running via TThreads causing application to freeze.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 19, 2016 2:55 AM   in response to: Alex Belo in response to: Alex Belo
Okay, but what if I wanted to have two threads running alongisde each other which were not dependent - e.g. computing averages on different data sets. When I try and do this I seem to run into similar problems. Is it to do with priority levels or available resources?

Edited by: Toby Dobbs on Dec 19, 2016 2:55 AM
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Concurrent processes running via TThreads causing application to freeze. [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 19, 2016 9:01 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

Okay, but what if I wanted to have two threads running alongisde each
other which were not dependent - e.g. computing averages on different
data sets.

Plan of the game is the same:
- data writers (threads-data providers) put data into some structure in
memory;
- data readers (like thread-visualizer) get data form this structure;
- all threads use the same critical section and they must lock it
before operation (reading or writing) with shared memory and unlock it
when operation is complete.

In this case all threads will always see consistent (i.e. not partially
modified) data. (Of course, in trivial case of the constant data many
threads can read these data simultaneously without any synchronization.)

The only problem is: if one thread is executing a long operation over
data then all other threads will be suspended on entering in this
critical section.

There is no universal recipe to solve this problem. For example, you
can put data chunks (or "tasks" which define portions of data for
processing) into queue and get them in many threads (no doubt, access
to such queue must be synchronized with critical section). If access to
the queue's put/get operations is fast then threads will not block one
another on appreciable periods of time: writers will create new data
blocks and put pointer on this new block into queue from time to time,
readers will take pointers from the queue when they ready to cranch new
data.

Also data writers can/must notify readers about new data. This can be
done by sending messages to threads-readers

How to send messages to threads
http://delphi-kb.blogspot.com/2008/04/how-to-send-messages-to-threads.html

or readers can wait for events (see TEvent class and "WaitFor..."
series of WinAPI functions).

For more information:

Synchronization Functions
https://msdn.microsoft.com/library/windows/desktop/ms686360(v=vs.85).aspx

and in particular

Critical section functions
Event functions
Wait functions
WaitForSingleObject
WaitForMultipleObjects

--
Alex
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02