Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_catch_multi_threaded.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Test the multi-threaded dynamic `do catch` mechanism
16 import pthreads
17
18 # A Thread that does a lot of `do catch`
19 class CatchThread
20 super Thread
21
22 var x = 10000
23 var caught = 0
24
25 redef fun main do
26 do
27 rec_do_catch
28 catch
29 end
30 print "caught {caught} aborts"
31 return null
32 end
33
34 fun rec_do_catch do
35 do
36 x -= 1
37 if x > 0 then
38 rec_do_catch
39 else
40 abort
41 end
42 catch
43 self.caught += 1
44 abort
45 end
46 end
47 end
48
49 var ts = new Array[CatchThread]
50 var nb_threads = 10
51 for i in [0..nb_threads[ do
52 var t = new CatchThread
53 ts.add(t)
54 t.start
55 end
56 for t in ts do t.join