FreePascal中简易的多线程程序

要创建一个新的进程,要使用BeginThread函数.它有一个强制的参数:
这个函数将执行新的线程.返回的结果是线程退出的结果.
这个现成能够被一个用来访问初始化数据的指针所传递,
程序员必须确定线程可访问的数据在访问之前没有超出范围.

函数定义:

THandle=Word;
TThreadFunc = function(parameter : pointer) : longint;

function BeginThread(ThreadFunction: tthreadfunc) : DWord;
function BeginThread(ThreadFunction: tthreadfunc; p: pointer): DWord;
function BeginThread(ThreadFunction:tthreadfunc;p: pointer;var ThreadId:THandle) :DWord;

如果给了,指针p将被传递给线程当线程开始时(否则,将传递Nil).
如果给了ThreadID, 那么这个线程的ID将被存储起来.

重新开始的线程直到线程函数退出或者被EndThread结束:
procedure EndThread(ExitCode : DWord);
procedure EndThread;
exitcode能和线程开始的代码进行检查.

下面的小例子将告诉你怎么使用:
{$Mode ObjFPc}
{$Threading On}

Uses
 sysutils {$ifdef unix},cthreads{$endif} ;

Const
 threadcount = 100;
 stringlen = 10000;

Var
  Finished:Longint;

Threadvar
  thri:Longint;

Function f(p : pointer):Longint;
Var
 s:Ansistring;
Begin
 Writeln(‘thread ‘,longint(p),’ started’);
 thri:=0;
 While (thri   Begin
   s:=s+’1′;
   inc(thri);
  End;
 Writeln(‘thread ‘,longint(p),’ finished’);
 InterLockedIncrement(finished);
 f:=0;
End;

Var
  i:Longint;
Begin
  finished:=0;
  For i:=1 To threadcount Do BeginThread(@f,pointer(i));
  While finished
  Writeln(finished);
End.

InterLockedIncrement是一个在线程中安全的使用的Inc函数版本.
InterLockedDecrement正好相反.

为了防止系统不约束的支持多线程程序,一些有效的函数用来操作线程.使用这些函数必须得到线程启动时的ID,因为大多数的函数需要ID来确定它们该对哪个线程起作用:

function SuspendThread(threadHandle: dword): dword;
function ResumeThread(threadHandle: dword): dword;
function KillThread(threadHandle: dword): dword;
function WaitForThreadTerminate(threadHandle: dword;TimeoutMs : longint): dword;
function ThreadSetPriority(threadHandle: dword;Prio: longint): boolean;
function ThreadGetPriority(threadHandle: dword): Integer;
function GetCurrentThreadId: dword;
procedure ThreadSwitch;

prio=-15..15
这些函数的作用必须清楚:

SuspendThread
吊起一个线程,暂停线程的执行.
ResumeThread
重新执行一个暂停的线程
KillThread
停止一个线程,将该线程从内存中清除.
WaitForThreadTerminate
等待一个线程结束.这个函数返回当线程中止执行执或者超过了期限.
ThreadSetPriority
设置当前线程的优先级,这个调用不是一定有效.
ThreadGetPriority
返回当前线程的优先级.
GetCurrentThreadId
返回当前线程的ID.
ThreadSwitch
允许其他线程在这个点(at this point)执行,能够引起现成转换,但是不能保证,它依靠操作系统和处理器的数量.

写多线程程序必须注意主线程和分线程之间结束先后关系,若分线程没有结束就退出程序,那么会怎样自己想

在使用线程上,我们还有更好的一个线程的类可以使用,可以管理各个线程,这里我就不介绍太多咯。我自己知道有限^_*!

这是简单的程序
巫山霏云精心整理,希望大家喜欢!
谢谢…

2005年7月31日

发布者

巫山霏云

巫山霏云,87年生巨蟹,文科生,IT男,喜读书,不求甚解,

《FreePascal中简易的多线程程序》有30个想法

  1. Hi blogger, i must say you have high quality content here.
    Your page should go viral. You need initial traffic only.
    How to get it? Search for: Mertiso’s tips go viral

  2. However, severe and recurrent herpes simplex eye infections may lead to serious scarring, impaired vision and even severe sight impairment in some cases. order brand name viagra cheap viagra pills for sale viagra cheap [url=http://viagraforsalebrc.com/]viagra for sale[/url] If you have anemia, or suffer from its symptoms, the good news is that your diet can help to turn the condition around. OK’

  3. Crapacardia – a bad EKG that needs to be repeated. viagra dose viagra online order brand name viagra [url=http://viagra-onlinenas.com/]online doctor prescription for viagra[/url] Causes of flu The flu is caused by a highly contagious virus that is spread by fluids produced during coughing and sneezing, or by direct contact with those fluids on surfaces. OK’

发表评论

电子邮件地址不会被公开。 必填项已用*标注