Rust program to set the name of a thread

Rust | Thread Example: Write a program to set the name of a thread.
Submitted by Nidhi, on November 06, 2021

Problem Solution:

In this program, we will create a thread by assigning the name of the thread using the thread::Builder::new().name() method.

Program/Source Code:

The source code to set the name of a thread is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to set the name 
// of a thread

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

fn main() {
	thread::Builder::new().name("MyThread".to_string()).spawn(move || {
		println!("Thread is running");
	});	
   
	thread::sleep(Duration::from_millis(500));
	println!("Program finished");
}

Output:

$ rustc main.rs

$ ./main
Thread is running
Program finished

Explanation:

Here, we created a thread by assigning the name MyThread using the thread::Builder::new().name() method and printed the message "Thread is running".

Rust Threads Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.