Guest:
Register
|
Login
|
Member List
|
Search
|
Statistics
|
FAQ
Language
----------
Simplifed Chinese
Traditional Chinese
English
CnPack Forum
»
技术板块灌水区
» 在delphi中使用go语言的并发编程方法,增加demo3
‹‹ Last Thread
|
Next Thread ››
Poll
Trade
Reward
Activity
Printable Version
|
Email to Friend
|
Subscription
|
Favorites
Subject: 在delphi中使用go语言的并发编程方法,增加demo3
zzzl
(早安的空气)
版主
UID 590
Digest Posts 0
Credits 399
Posts 199
点点分 399
Reading Access 100
Registered 2004-11-29
Status Offline
#1
Post at 2010-12-1 16:13
Profile
|
Blog
|
P.M.
|
在delphi中使用go语言的并发编程方法,增加demo3
program
demo3
;
{$APPTYPE CONSOLE}
uses
SysUtils
,
Generics
.
Collections
,
coroutineUnit
;
//这个例子演示了如何实现超时控制
var
cWork
,
cTimeout
:
CChannel
<
Integer
>;
//声明两个通道,各用于工作和超时
ret
:
tpair
<
Integer
,
Integer
>;
//后面有讲到
begin
cWork
:=
CChannel
<
Integer
>.
create
;
cTimeout
:=
CChannel
<
Integer
>.
create
;
go
(
procedure
()
//起动工作线程
var
i
:
Integer
;
begin
sleep
(
1010
);
//假设这个工作费时1010毫秒
try
cWork
.
value
:=
1
;
//发通道发送一个值,表示工作完成了。
except
end
;
end
);
go
(
procedure
()
//起动一个线程用来计时
begin
sleep
(
1000
);
//假设超时等待时间是1秒
cTimeout
.
value
:=
1000
;
//...
end
);
{直到cWork,cTimeout这两个通道至少有一个可读取,获取通道的结果,结果是个pair,其中key描述了哪个通道有响应,value描述了通道的值(对这个select的模仿远没有go语言的幽雅,也许以后会有更好的办法)}
ret
:=
CCoroutine
.
select
<
Integer
>
([
cWork
,
cTimeout
])
;
case
ret
.
key
of
//判断是哪个通道可读
0
:
Writeln
(
'完成'
);
//如果是第0个,就是cWork的信号了,表示在超时等待之前工作完成了。
1
:
Writeln
(
'超时'
+
inttostr
(
ret
.
value
));
//...
end
;
cWork
.
Free
;
cTimeout
.
Free
;
Readln
;
end
.
Attachment
:
coroutineUnit.zip
(2010-12-1 16:13, 930 bytes)
Download count 87
Passion
(LiuXiao)
管理员
UID 359
Digest Posts
19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
#2
Post at 2010-12-6 20:47
Profile
|
Blog
|
P.M.
附件中只有coroutineUnit.pas,没有相应的dpr文件?
zzzl
(早安的空气)
版主
UID 590
Digest Posts 0
Credits 399
Posts 199
点点分 399
Reading Access 100
Registered 2004-11-29
Status Offline
#3
Post at 2010-12-10 10:11
Profile
|
Blog
|
P.M.
|
Attachment
:
demo_timeout.rar
(2010-12-10 10:11, 838 bytes)
Download count 81
friendlinzh
新警察
UID 67162
Digest Posts 0
Credits 20
Posts 10
点点分 20
Reading Access 10
Registered 2011-8-18
Status Offline
#4
Post at 2011-8-18 10:22
Profile
|
Blog
|
P.M.
多谢分享啊
playermall
Poll
Trade
Reward
Activity
CnPack Forum
CnPack English Forum
> CnWizards IDE Wizards
> CVSTracNT
> Announcements & Others
All times are GMT++8, the time now is 2024-11-22 06:42
Powered by
Discuz!
5.0.0
© 2001-2006
Comsenz Inc.
Processed in 0.015473 second(s), 10 queries , Gzip enabled
TOP
Clear Cookies
-
Contact Us
-
CnPack Website
-
Archiver
-
WAP
Member's CP Home
Edit Profile
Credits Transaction
Public User Groups
Buddy List
Main
Page Views
User Agents
Posts History
Top Forums
Top Threads
Post Ranking
Credit Ranking
Online Time
Team
Moderation Stats