File: sld021.htm

package info (click to toggle)
aolserver4 4.5.1-15.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,772 kB
  • sloc: ansic: 45,120; tcl: 5,532; sh: 1,021; makefile: 380; pascal: 219; php: 13
file content (127 lines) | stat: -rw-r--r-- 2,396 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

<!--  Presentation generated by Internet Assistant for Microsoft PowerPoint 97 -->

<HTML>
<HEAD>

<meta name="GENERATOR" content="Microsoft Internet Assistant for Microsoft PowerPoint 97">
 <TITLE>Example: Event-Driven vs Thread I/O</TITLE> 
</HEAD>

<BODY     >

<CENTER>
<TABLE WIDTH=100%> 
     <TR> <TD WIDTH=100% ALIGN=CENTER>
         <A HREF="sld001.htm">First</A>&nbsp;&nbsp;
         <A HREF="sld020.htm">Previous</A>&nbsp;&nbsp;
         <A HREF="sld022.htm">Next</A>&nbsp;&nbsp;
         <A HREF="sld077.htm">Last</A>&nbsp;&nbsp;
&nbsp;&nbsp;         <A HREF="index.htm">Index</A>&nbsp;&nbsp;
         <A HREF="tsld021.htm">Text</A>&nbsp;&nbsp;
     </TD> </TR>
     <TR> <TD WIDTH=100% ALIGN=CENTER>
         <IMG SRC="img021.gif" usemap="#Objmap" WIDTH=600 HEIGHT=450 BORDER=0>
     </TD> </TR>
</TABLE>
<P>Slide 21 of 77</P>
</CENTER>
<!-- <UL>
<H2>Example: Event-Driven vs Thread I/O</H2>
</UL></P>
<P><UL>
<LI><H2>I/O bound apps are sometimes easier with threads:</H2>
</UL></P>
<P><UL>
global done count
</UL><UL>

</UL><UL>
proc my.read {sock} {
</UL><UL>
  global done count
</UL><UL>
  read $sock
</UL><UL>
  if [eof $sock] {
</UL><UL>
    close $sock
</UL><UL>
    if {[incr count -1] == 0} {
</UL><UL>
      set done 1
</UL><UL>
    }
</UL><UL>
  }
</UL><UL>
}
</UL><UL>

</UL><UL>
proc my.write {sock} {
</UL><UL>
  puts $sock "GET /index.html\r\n"
</UL><UL>
  flush $sock
</UL><UL>
  fileevent $sock readable "my.read $sock"
</UL><UL>
}
</UL><UL>

</UL><UL>
set count 0
</UL><UL>
foreach host { host1 host2 host3 host4} {
</UL><UL>
  incr count
</UL><UL>
  set sock [socket -async $host 80]
</UL><UL>
  fconfigure $sock -blocking off
</UL><UL>
  fileevent $sock writable "my.write $sock"
</UL><UL>
}
</UL><UL>

</UL><UL>
vwait done</UL></P>
<P><UL>
proc my.thread {host} {
</UL><UL>
  set sock [socket $host 80]
</UL><UL>
  puts $sock "GET /index.html\r\n"
</UL><UL>
  flush $sock
</UL><UL>
  read $sock
</UL><UL>
  close $sock
</UL><UL>
}
</UL><UL>

</UL><UL>
foreach host {host1 host2 host3 host4} {
</UL><UL>
  lappend tids [ns_thread begin my.thread $host"]
</UL><UL>
}
</UL><UL>

</UL><UL>
foreach tid $tids {
</UL><UL>
  ns_thread wait $tid
</UL><UL>
}</UL></P>
<P><UL>
Events</UL></P>
<P><UL>
Threads</UL></P>
<P>  -->
</Body>
</HTML>