Fearless Concurrency in Rust

Reading Time: 2 minutes

In concurrency different part of the program run independently of each other and in parallel programming parts of a program execute at the same time, and these factors are becoming so important as we want to take advantage of multiple processors. In Rust you don’t have look for different circumstances under which runtime concurrency bug occurs because in rust most of the concurrent errors are compiled time. Incorrect code will refuse to compile and shows an error message explaining the problem isn’t thats a fearless concurrency.

The main building block of concurrent programming is what we called Threads. Let’s have a look on how to create to create a thread in Rust to run multiple parts of the code. An operating system runs your program as a single process and using thread you can run the same program into multiple processors. Using thread to run your program can improve your performance but adds complexity since there is no inherent, that in which order those threads will run and different problems may arise due to that these could be 
1. Race condition – Where different threads looked for data or resources in an inconsistent order. 
2. Deadlocks – Where two threads are waiting for each other to finish using a resource which they are seeking preventing both threads from furthers executing.
 3.Bugs – That’s happened only in a certain situation.

Rust tries to counter with these could effect of threads but still, you require a code structure which is different from what you have normally used for single thread program.

Lets now focus on threads many programming languages like Go provide their own implementation of threads, basically, threads are of two types one is Operating system thread other is Green thread. The operating system thread is offered by the operating system itself while the green thread is an abstraction that sits on the top of the operating system thread in M: N i.e. M green threads per N operating system where M and N might not be same. Each model have their own advantages and trade-offs like in green thread might have larger run times but have more features and this would scale upon the power of the computer. Rust, however, uses the operating system threads which have nearly no run time. By runtime we mean the code that is included in every binary. Smaller runtime might have fewer features but will have the advantage of smaller binaries, which makes it easier to combine with the other languages. Let’s have an example of creating a thread in Rust.

use std::thread;
fn main() {
   let v = vec![1,2,3];
   let handle = thread::spawn(move || {
   println("{:?}",v);
});
 handle.join();
}

In the above example we created thread using thread::spawn, we have used move keyword inside it to force the closure to take ownership of the vector from main thread, as you might have seen we join our thread with main thread to handle blocks the thread currently running until the thread represented by the handle terminates. You can create multiple threads but Rust doesn’t ensure any specific order for the thread execution and in most of the cases we don’t really care.

knoldus-advt-sticker

Written by 

Alok Jha is the QA Consultant at Knoldus Software LLP. He has good knowledge of languages Java, Java 8, Rust and JavaScript. As a QA, he always tries to explore the different type of software and tools.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!