Современные
информационные технологии/3. Программное обеспечение
Zhuaspaev T.A
A.Baitursynov Kostanai
State University, Kazakhstan
USING
MULTIPLE STREAMS WITH CLASS TTHREAD In C + +
In some cases, the application should
organize the work of 2 or more concurrent threads. This need arises when
unrelated data blocks, the more computation that does not require sequential
location in the program code. For example, one thread of execution can perform
most of the work, and the second, with a lower priority may at the same time to
prepare some files that will be needed in the future, ie perform the dirty
work. Another example - the parallel operation with multiple external sources
of information. Especially large gains in productivity through parallel
execution of multiple threads can be obtained in a multiprocessor system in
which you can organize the implementation of each strand of a single processor.
Class TThread - an abstract class that allows you to create a separate thread
in the application of the [1].
To study the TThread programming
environment used by Borland C + + Builder 6.0.
The
threads used in a variety of independent calculations, but they can be used in
more complex calculations require the use of a single thread of the other
results. The easiest way is awaiting the completion of the thread (the method
WaitFor ()), and therefore virtually ineffective. Another method is to use a
queue. When one thread (receiver) need new data, the preparation of which is
engaged in the second (spring), and it is waiting for the completion of the
third (the mediator), the latter examines the new data in the queue and returns
them if they are.
After a
third stream (mediator) is produced, and adding new information to the queue. This
is to avoid conflicts of simultaneous use of the queue. The model turn is shown
in Picture 1.
Picture 1. The scheme of turn
To create a thread of the existing class
that inherits TThread, creates a pointer to the type of this class, and the
function returns a pointer to the new object of that class.
NameThreadClass
* Potok;
……………
Potok =
new NameThreadClass(false);
False parameter indicates immediate
execution threads (the function Execute ()) immediately after the constructor
(NameThreadClass ()), and the parameter to true will only call the constructor,
then the thread is done by Resume (), which in addition to this case is used
after the suspension of the thread method Suspend ().
On the basis of the material has been
modified 2 applications:
1.
Application using a
thread.
2.
An application that uses
three streams, by starting with a queue.
This
application of the existing sequence numbers removes the longest sequence of
numbers arranged in ascending order. Down stream implements output sequence
after the removal, but the task can not be completed before the next two
streams. Therefore, from the first stream and a second stream is called waiting
for its completion. The second stream
adds a numeric sequence in turn and look for the position of the beginning of
the longest sequence of numbers arranged in ascending order. Next, the thread
returns to the first thread is called from a third stream implements the
removal of the sequence length. After the completion of the third stream the
result is displayed on the screen (Picture 2).
Picture 2. The resulting output
Literature
1. Архангельский
А.Я. Программирование в C++ Builder 6.0 – М.: Бином, 2000 –
1548с
2. Бобровский В.П.
Самоучитель программирования на языке С++ в системе Borland C++ Builder 5.0 – М.: Десс, 2000 – 272с