File: threadkill.mdc

package info (click to toggle)
freemat 4.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 141,800 kB
  • ctags: 14,082
  • sloc: ansic: 126,788; cpp: 62,046; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 100
file content (84 lines) | stat: -rw-r--r-- 1,914 bytes parent folder | download | duplicates (2)
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

 Usage

The threadkill function stops (or attempts to stop)
execution of the given thread. It works only for functions
defined in M-files (i.e., not for built in or imported
functions), and it works by setting a flag that causes the
thread to stop execution at the next available statement.
The syntax for this function is

    threadkill(handle)

where handle is the value returned by a threadnew call. Note
that the threadkill function returns immediately. It is
still your responsibility to call threadfree to free the
thread you have halted.
You cannot kill the main thread (thread id 1).


 Example

Here is an example of stopping a runaway thread using
threadkill. Note that the thread function in this case is an
M-file function. We start by setting up a free running
counter, where we can access the counter from the global
variables.

       freecount.m


  function freecount
    global count
    if (~exist('count')) count = 0; end  % Initialize the
  counter
    while (1)
      count = count + 1;                 % Update the
  counter
    end

We now launch this function in a thread, and use threadkill
to stop it:

  --> a = threadnew;
  --> global count                   % register the global
  variable count
  --> count = 0;
  --> threadstart(a,'freecount',0)   % start the thread
  --> count                          % it is counting

  ans =
   0

  --> sleep(1)                       % Wait a bit
  --> count                          % it is still counting

  ans =
   225458

  --> threadkill(a)                  % kill the counter
  --> threadwait(a,1000)             % wait for it to finish

  ans =
   1

  --> count                          % The count will no
  longer increase

  ans =
   225497

  --> sleep(1)
  --> count

  ans =
   225497

  --> threadfree(a)


* FreeMat_Documentation
* FreeMat_Threads
* Generated on Thu Jul 25 2013 17:18:29 for FreeMat by
  doxygen_ 1.8.1.1