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

Add more concurrent example/document/blog #639

Open
zw963 opened this issue Sep 17, 2022 · 1 comment
Open

Add more concurrent example/document/blog #639

zw963 opened this issue Sep 17, 2022 · 1 comment

Comments

@zw963
Copy link
Contributor

zw963 commented Sep 17, 2022

e.g. https://lbarasti.com/post/select_statement/, i thought we should add it into official blog.

AFAIK, we even never mention any example/document for select in crystal book or API or official blog.

I consider following examples probably is a good start which i discuss with @Blacksmoke16 on here

ch = Channel(Int32).new
terminate = Channel(Nil).new
done = Channel(Nil).new

[1, 2, 3].each do |i|
  spawn do
    ch.send i
  end
end

spawn do
  loop do
    select
    when value = ch.receive
      p value
    when terminate.receive?
      break
    end
  end
  done.close
end

terminate.close
done.receive?
ch = Channel(Int32).new

[1, 2, 3].each do |i|
  spawn do # => NOTICE: spawn 3 fiber here.
    ch.send i
  end
end

loop do
  break if ch.closed?

  select
  when value = ch.receive?
    p value
  when timeout 3.seconds
    puts "Timeout"
    ch.close
  end
end
@Blacksmoke16
Copy link
Member

Duplicate of #208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants