Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new channels article #274

Closed
wants to merge 37 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b7e0d06
Create 2021-03-12-new-Nim-channels.md
ringabout Mar 11, 2021
4a5901e
Update 2021-03-12-new-Nim-channels.md
ringabout Mar 11, 2021
f28cebd
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 11, 2021
4bb8b90
Apply suggestions from code review
ringabout Mar 11, 2021
4183f52
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
5b87806
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
8b43a3b
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
5f11cf4
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
8db9837
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
a381873
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 12, 2021
5f11660
Update 2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
4b45bcd
Update 2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
426ce67
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
f9e5f94
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
42513d9
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
c124ead
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
9d6db08
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
01c6bf0
Apply suggestions from code review
ringabout Mar 14, 2021
965e392
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 14, 2021
fe2b67e
Apply suggestions from code review
ringabout Mar 14, 2021
accb697
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 15, 2021
84c5b0e
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 15, 2021
ef79a5d
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 15, 2021
d2383bd
Apply suggestions from code review
ringabout Mar 15, 2021
df83246
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 15, 2021
40b3d70
Update 2021-03-12-new-Nim-channels.md
ringabout Mar 15, 2021
52f3a07
update benchmark
ringabout Mar 15, 2021
5c9b9ec
Apply suggestions from code review
ringabout Mar 17, 2021
44abd9a
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 18, 2021
0d6700a
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 18, 2021
bc1bbb6
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 18, 2021
3d1169d
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 18, 2021
1aa803f
Apply suggestions from code review
ringabout Mar 18, 2021
f2a595e
Update jekyll/_posts/2021-03-12-new-Nim-channels.md
ringabout Mar 23, 2021
fefc4eb
better
ringabout Mar 31, 2021
2f9ed9a
minor whitespace changes
narimiran May 3, 2021
c112607
change the date
narimiran May 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: konsumlamm <[email protected]>
ringabout and konsumlamm authored Mar 18, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1aa803fbe0934f116af5f05f8543c785ac7addc9
10 changes: 6 additions & 4 deletions jekyll/_posts/2021-03-12-new-Nim-channels.md
Original file line number Diff line number Diff line change
@@ -80,13 +80,13 @@ var chan2 = newChannel[string](elements = 1) # unbuffered channel
var chan3 = newChannel[seq[string]](elements = 30) # buffered channel
```

The `send` proc takes data that we want to send to the channel. The passed data is moved around, not copied. Because `chan.send(isolate(data))` is very common to use, `template send[T](c: var Chan[T]; src: T) = chan.send(isolate(src))` is provided for convenience. For example, you can use `chan.send("Hello World")` instead of `chan.send(isolate("Hello World!"))`.
The `send` proc takes data that we want to send to the channel. The passed data is moved around, not copied. Because `chan.send(isolate(data))` is very common to use, `template send[T](c: var Channel[T]; src: T) = c.send(isolate(src))` is provided for convenience. For example, you can use `chan.send("Hello world")` instead of `chan.send(isolate("Hello world!"))`.

There are two useful procs for a receiver: `recv` and `tryRecv`. `recv` blocks until something is sent to the channel. In contrast, `tryRecv` doesn't block - if no message exists in the channel, it just fails and returns `false`. We can write a while loop to call `tryRecv` and handle a message when available.

### It is safe and convenient

The Nim compiler rejects the program below at compile time. It says that `expression cannot be isolated: s`. This is because `s` is a `ref object` - it may be modified somewhere and is not unique, so the variable cannot be isolated.
The Nim compiler rejects the program below. It says that `expression cannot be isolated: s`. This is because `s` is a `ref object` - it may be modified somewhere and is not unique, so the variable cannot be isolated.


```nim
@@ -118,15 +118,15 @@ import std/isolation

var data = isolate("string")
doAssert data.extract == "string"
doAssert data.extract == ""
doAssert data.extract == "" # the value was moved
```

By means of `Isolated` data, the channels become safer and more convenient to use.


## Benchmark

Here is a simple benchmark. We create 40 threads that send data to the channel and 5/10/20 threads that receive it.
Here is a simple benchmark. We create 40 threads that send data to the channel and 5/10/20 threads that receive from it.

```nim
# benchmark the new channel implementation with
@@ -155,6 +155,7 @@ proc recvHandler() =

proc sendHandler() =
chan.send(@["Hello, Nim"])

template benchmark() =
for t in mitems(sender):
t.createThread(sendHandler)
@@ -164,6 +165,7 @@ template benchmark() =
let start = now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

joinThreads(receiver)
echo now() - start

benchmark()
```