我正在使用Delphi XE来创建多线程应用程序。
我想创建一个线程来处理所有写入firebird / sqlite数据库日志的操作。
我想创建几个可以完成某些工作的线程,而其他一些线程则需要在需要时使用写入日志线程。
Thread1 = writing log thread
Thread2 = do some math and from time to time use Thread1 to write log.
Thread3 = do some other stuff and from time to time use Thread1 to write log.
以此类推
为简单起见,我在线程1中创建了一个名为WriteCollectionLog的方法,所有其他线程都需要使用该方法将日志写入线程1内存(一个集合),线程1“ onexecute”将处理将实际日志写入数据库的过程。该方法旨在像“抛物线”方法一样使用。
现在如何使该线程安全?还是可以使其线程安全? (通过使用TCriticalSection吗?)
thread2.WriteCollectionLog ...
thread3.WriteCollectionLog ...
procedure Thread1.WriteCollectionLog(aIDWORKFLOW : Integer);
var workItem : TLogFIREBIRD_Item;
begin
try
readWriteCriticalSection.Acquire; <--- this will suspend the calling thread .. like thread2,thread3 and not the thread1?
do stuff;
finally
readWriteCriticalSection.Release;
end;
end;
致谢
拉兹万