Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@peace098beat

今年の言語はRust その48

More than 1 year has passed since last update.

今年の言語はRust その48

Rustを学びます

Rustの日本語ドキュメント 2nd Edition
https://doc.rust-jp.rs/book/second-edition/

オリジナル(英語)
https://doc.rust-lang.org/book/

実行環境

$ cargo -V
cargo 1.33.0 (f099fe94b 2019-02-12)

$ rustup -V
rustup 1.17.0 (069c88ed6 2019-03-05)

$ rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28)

$  cat /proc/version
Linux version 4.14.97-74.72.amzn1.x86_64 (mockbuild@gobi-build-64002) 
(gcc version 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC)) 
#1 SMP Tue Feb 5 20:59:30 UTC 2019

$ uname -a
Linux ip-10-100-0-8 4.14.97-74.72.amzn1.x86_64 
#1 SMP Tue Feb 5 20:59:30 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/\*release
NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
Amazon Linux AMI release 2018.03

16 恐れるな!並行性

並行性

  • 並行プログラミング => プログラムの異なる部分が'独立'して実行
  • 並列プログラミング => プログラムの異なる部分が'同時'に実行

16.2 メッセージ受け渡しを使ってスレッド間でデータを転送する

use std::thread;
use std::sync::mpsc;

fn main() {

  let (tx, rx) = mpsc::channel();

  thread::spawn(move || {
    let val = String::from("Hi");
    tx.send(val).unwrap();

    println!("val is {:?}", val); // valの借用兼を奪われているのでエラー
  });

  let received = rx.recv().unwrap();

  println!("Got: {:?}", received);
}

複数の値を送信し、受信側が待機するのを確かめる

use std::thread;
use std::time::Duration;
use std::sync::mpsc;

fn main() {

  let (tx, rx) = mpsc::channel();

  thread::spawn(move || {

    let vals = vec![
      String::from("hi"),
      String::from("from"),
      String::from("the"),
      String::from("thread"),
    ];

    for val in vals{
      tx.send(val).unwrap();
      thread::sleep(Duration::from_secs(1));

    }

  });

  for received in rx {
    println!("Got: {:?}", received);
  }

}

転送気をクローンして複数の生成器を作成する

use std::thread;
use std::time::Duration;
use std::sync::mpsc;

fn main() {

  let (tx, rx) = mpsc::channel();
  let tx1 = mpsc::Sender::clone(&tx);

  thread::spawn(move || {

    let vals = vec![
      String::from("hi"),
      String::from("from"),
      String::from("the"),
      String::from("thread"),
    ];

    for val in vals{
      tx1.send(val).unwrap();
      thread::sleep(Duration::from_secs(1));

    }

  });


  thread::spawn(move || {

    let vals = vec![
      String::from("more"),
      String::from("messages"),
      String::from("for"),
      String::from("you"),
    ];

    for val in vals {
      tx.send(val).unwrap();
      thread::sleep(Duration::from_secs(1));
    }

  });



  for received in rx {
    println!("Got: {:?}", received);
  }

}

複数のスレッドからデータを送ってもらえた!

イエス!

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
abeja
「ディープラーニング」を活用し、多様な業界、シーンにおけるビジネスの効率化・自動化を促進するベンチャー企業です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?