TThread.Synchronize同步方法及弊端

In effect it borrows time from the main VCL thread to do this. This doesn't work arbitrarily between threads. If you have two non-VCL threads, X and Y, you can't call synchronize in X alone and then modify data stored in Y. It is necessary to call synchronize from both threads when reading or writing the shared data. In effect, this means that the data is modified by the main VCL thread, and all the other threads synchronize to the main VCL thread every time they need to access this data. This is workable, but inefficient, especially if the main thread is busy: every time the two threads need to communicate, they have to wait for a third thread to become idle. Later on, we shall see how to control concurrency between the threads and have them communicate directly.


TThread.Synchronize可以避免线程对共享数据访问时发生冲突,它的实现方法是,当子线程利用Synchronize方法执行访问共享资源的代码时,该子线程立刻挂起,等待主线程空闲后在主线程中执行该访问共享资源的代码,代码在主线程执行完后,主线程回复原始状态,而子线程继续运行。 概括来说,子线程中的代码实际是在主线程中执行的,实质为给主线程发了个消息,通知它应该调用哪个函数。
存在的问题是,当多个子线程之间要同步时, 按此方法,必须等待主线程空闲下来,才有机会获得在主线程中的运行权,这就显得没有必要,效率也低。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值