blob: 000c3af61a65b6b5db59e63dbef670611ff8205c (
plain)
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
|
## Passing complex objects to threads
In the previous examples, we have been using threads with very simple functions and
we have been passing very simple values (integers) between threads.
This example shows how we can pass more complex data between threads.
It is important to understand that objects cannot be shared across threads.
This does not prevent us from passing complex objects but we have to serialize them
and pass them as strings.
If the objects are really simple, we can use JSON serialization but if they contain
information that JSON discards, like methods, we should use the JASON serializer
published on https://github.com/xk/JASON
In this example, we are going to use a thread to do computation with complex numbers.
We use the Complex and Equation classes defined in the ex06_complex.js file.
``` javascript
var Equation = require("./ex06_complex").Equation;
```
As usual, we create a thread
``` javascript
var t = require('webworker-threads').create();
```
We require the JASON serializer
``` javascript
var JASON = require("JASON");
```
We load the JASON serializer and the solve function in our thread:
``` javascript
t.eval("JASON= "+ JASON.stringify(JASON));
t.load(__dirname + "/ex06_complex.js")
```
Now we can pass a request to solve an equation to our thread.
The expression is wrapped into a `JASON.stringify` call because we want the thread
to stringify the solution object before returning it to the main thread
The main thread calls `JASON.parse` to _de-stringify_ the solution.
``` javascript
t.eval("JASON.stringify(new Equation(1, -4, 29).solve())", function(err, result) {
if (err) throw err;
var r = JASON.parse(result).join(', ');
console.log("\nsolution is:\n[" + r+ "]\n");
t.destroy();
});
```
### Typical Output
```
solution is:
[2 - 5i, 2 + 5i]
```
|