0Pricing
Learn Rust Coding · Aula

Criando threads

Execute código de forma concorrente.

Criando threads é uma aula grátis de Learn Rust Coding no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Learn Rust Coding, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Learn Rust Coding inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What Is a Thread?

A thread lets your program run code concurrently. The operating system can schedule multiple threads, so work can overlap.

Rust's standard library exposes threads through the std::thread module. These are native OS threads, sometimes called 1:1 threads.

In this lesson you will learn to start new threads and control how they run.

Spawning with thread::spawn

You create a new thread by calling thread::spawn and passing it a closure. The closure holds the code the new thread will run.

The call returns immediately with a JoinHandle, while the spawned thread runs in the background.

use std::thread;

fn main() {
    thread::spawn(|| {
        println!("hello from a thread");
    });
    println!("hello from main");
}

Main May Finish First

When main returns, the whole process ends, even if spawned threads are still running.

So the program above might print only the main message. The background thread may not get a chance to run before the process exits.

We need a way to wait for threads to finish.

Waiting with join

The JoinHandle returned by spawn has a join method. Calling it blocks the current thread until the spawned thread finishes.

This guarantees the spawned thread runs to completion before main continues.

use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("worker done");
    });
    handle.join().unwrap();
    println!("main done");
}

Interleaving Output

When two threads run at once, their output can interleave in unpredictable ways. The OS decides the schedule.

Running the same program twice may produce different orderings. Never rely on a specific order without synchronization.

use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        for i in 1..4 {
            println!("thread: {}", i);
        }
    });
    for i in 1..4 {
        println!("main: {}", i);
    }
    handle.join().unwrap();
}

Pausing with sleep

You can pause a thread with thread::sleep, which takes a Duration. This yields the CPU so other threads can make progress.

Sleeping is useful in examples to make interleaving more visible, but avoid it for real synchronization.

use std::thread;
use std::time::Duration;

fn main() {
    thread::spawn(|| {
        for i in 1..4 {
            println!("spawned: {}", i);
            thread::sleep(Duration::from_millis(1));
        }
    });
    thread::sleep(Duration::from_millis(10));
}

Spawning Many Threads

You can spawn several threads in a loop and collect their handles into a vector.

Later you iterate the vector and join each handle, ensuring every thread completes before the program ends.

use std::thread;

fn main() {
    let mut handles = vec![];
    for id in 0..3 {
        let h = thread::spawn(move || {
            println!("thread {}", id);
        });
        handles.push(h);
    }
    for h in handles {
        h.join().unwrap();
    }
}

Naming Threads with Builder

The thread::Builder type lets you configure a thread before spawning. You can set a name and a stack size.

Named threads make panic messages and debugging easier to read.

use std::thread;

fn main() {
    let h = thread::Builder::new()
        .name("worker".into())
        .spawn(|| {
            println!("running in named thread");
        })
        .unwrap();
    h.join().unwrap();
}

Panics Stay in the Thread

If a spawned thread panics, it does not crash the whole program by default. The panic is contained in that thread.

When you call join on a panicked thread, you get an Err. This lets the parent detect the failure.

use std::thread;

fn main() {
    let h = thread::spawn(|| {
        panic!("boom");
    });
    let result = h.join();
    println!("joined, is_err = {}", result.is_err());
}

Current Thread Info

You can inspect the running thread with thread::current. It returns a handle whose name method gives the optional thread name.

The main thread is also a real thread, usually named main.

use std::thread;

fn main() {
    let current = thread::current();
    println!("name: {:?}", current.name());
}

Returning Values from Threads

The closure passed to spawn can return a value. That value comes back wrapped in Ok when you call join.

This is a simple way to compute something on another thread and read the result later.

use std::thread;

fn main() {
    let h = thread::spawn(|| {
        let sum: i32 = (1..=10).sum();
        sum
    });
    let total = h.join().unwrap();
    println!("total = {}", total);
}

Quick Check

Test your understanding of spawning threads.

Recap

You learned to start threads with thread::spawn and a closure, which returns a JoinHandle.

Calling join waits for completion and surfaces the return value or a panic. Output between threads can interleave unpredictably, and the Builder lets you name threads.

Next you will see how to move data into the threads you spawn.

Perguntas Frequentes

A aula “Criando threads” é grátis?

Sim — o texto completo de “Criando threads” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Learn Rust Coding, atualize para CoddyKit PRO. O curso de Learn Rust Coding inclui 4 aulas no total.

O que vou aprender em “Criando threads”?

Execute código de forma concorrente. Você pratica Learn Rust Coding com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Learn Rust Coding?

Nenhuma experiência prévia é necessária. Learn Rust Coding no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Criando threads”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Learn Rust Coding?

Sim. Cada aula de Learn Rust Coding inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Criando threads
  2. Movendo dados para threads
  3. Compartilhando com Arc e Mutex
  4. Unindo threads e reunindo resultados
← Voltar para Learn Rust Coding