Synchronize in Delphi

Source: Internet
Author: User

In Delphi, what is the use of synchronize? I know it is used by thread synchronization.

Whether it is a function or variable. When a thread is in use, if this function uses synchronize to repair the handler, other threads are not allowed to call this function.

 

Level 1: other threads can continue after the operation is completed
Floor 2: This is basically what you mean. It aims to prevent multiple subthreads from simultaneously accessing the resources of the main thread.
Example:
Procedure serverthread. listitemend; // a method of sub-Thread
Begin
If (listitem <> nil) then
Listitem. subitems. Strings [3]: = 'transfer completed '; // listitem is a row of a Treeview in the main thread.
End;

Procedure serverthread. clientexecute; // subthread execution
Begin
Synchronize (listitemend );
// If you write listitemend directly, a conflict may occur.
End;

---------------------------------
Maximstr;
The reputation is ruined by people who hope the cloud is on the cloud, and the wind will be dispersed. ';
If not assigned (I) then
I: = Ti. Create (NiL );
I. Maxim: = maximstr;
I. Explain: = 'delete them if "OA" is displayed in the above Code ';
I. desire: = 'add + + ';
---------------------------------
     

Third floor: when multiple threads are created and multiple threads need to access the same resource, confusion may occur, so synchronize is used for control, so that only one thread can use that part of resources at a time. The code in the synchronize parameter is the Common Code required by multithreading!
4 floor: 1. The generation of the synchronize method is related to the limitations of VCL, because the VCL control can only be accessed by a single thread at the same time. If multiple threads access VCL at the same time, VCL may have problems. The tthread class provides a method called synchronize, which allows the methods in the thread to be executed in the main thread, therefore, we can put all the code for accessing VCL in this method and then run it in the main thread to ensure that VCL can be securely accessed and called. its type is tthreadmethod = procedure of object; Procedure Parameter: sychronize (method: tthreadmethod );
2. When a thread is created, VCL creates an implicit thread window in the main thread, which queues the methods used to access VCL called by synchronize. synchronize stores the method passed by the method parameter in the fmethod field of the thread, sends a cm_execproc message to the thread window, and sets the lparam parameter of the message to self, when the thread window receives the message, it calls the method specified by fmethod.
5 th floor: 1. If you do not access the VCL control (whether the VCL control is a window control), can you use the synchronize method to control it?
2. How to obtain the thread ID
Thx
6 th floor: the installation is complete.
Floor 7: 1. If you do not access the VCL control (whether the VCL control is a window control), can you use the synchronize method to control it?
2. How to obtain the thread ID
======================================
1. Yes
2. tthread. threadid
---------------------------------
Maximstr;
The reputation is ruined by people who hope the cloud is on the cloud, and the wind will be dispersed. ';
If not assigned (I) then
I: = Ti. Create (NiL );
I. Maxim: = maximstr;
I. Explain: = 'delete them if "OA" is displayed in the above Code ';
I. desire: = 'add + + ';
---------------------------------

Floor 8: Why blizzard ..
VCL does not support multi-threaded access ..
To make up for this defect, Borland uses the synchronize Method to Control concurrent access to VCL.

 

 

Http://www.websnap123.com/show-TnpReE5USXk=.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.