C++ vs Rust – Season 1 (NodeGraph Example)

Listen to this Post

You Should Know:

In this article, Sonia K. explores the implementation of Static and Dynamic NodeGraph in both C++ and Rust. The article delves into the challenges of object lifetimes in Rust, parallel execution using C++ Standard Library and Rust’s Rayon, and the use of C++ Co-Routines for deferring Node execution. The article also discusses thread-safety models and strategies to avoid deadlocks.

Practice Verified Codes and Commands:

1. C++ Co-Routines Example:

#include <iostream>
#include <coroutine>

struct promise {
auto get_return_object() { return std::coroutine_handle<promise>::from_promise(*this); }
auto initial_suspend() { return std::suspend_always{}; }
auto final_suspend() noexcept { return std::suspend_always{}; }
void return_void() {}
void unhandled_exception() { std::terminate(); }
};

struct task {
using promise_type = struct promise;
std::coroutine_handle<promise_type> coro;
task(std::coroutine_handle<promise_type> h) : coro(h) {}
~task() { if (coro) coro.destroy(); }
};

task my_coroutine() {
std::cout << "Hello from coroutine!\n";
co_return;
}

int main() {
auto t = my_coroutine();
t.coro.resume();
}

2. Rust Rayon Parallel Execution Example:

use rayon::prelude::*;

fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
vec.par_iter_mut().for_each(|x| {
*x *= 2;
});
println!("{:?}", vec);
}

3. Avoiding Deadlocks in C++:

#include <mutex>
#include <thread>

std::mutex mtx1, mtx2;

void thread_function1() {
std::lock(mtx1, mtx2);
std::lock_guard<std::mutex> lock1(mtx1, std::adopt_lock);
std::lock_guard<std::mutex> lock2(mtx2, std::adopt_lock);
// Critical section
}

void thread_function2() {
std::lock(mtx1, mtx2);
std::lock_guard<std::mutex> lock1(mtx1, std::adopt_lock);
std::lock_guard<std::mutex> lock2(mtx2, std::adopt_lock);
// Critical section
}

int main() {
std::thread t1(thread_function1);
std::thread t2(thread_function2);
t1.join();
t2.join();
return 0;
}

4. Rust Thread Safety Model:

use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];

for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}

for handle in handles {
handle.join().unwrap();
}

println!("Result: {}", *counter.lock().unwrap());
}

What Undercode Say:

The article provides a comprehensive comparison between C++ and Rust, focusing on their capabilities in handling NodeGraph implementations, parallel execution, and thread safety. The examples provided demonstrate practical applications of C++ Co-Routines, Rust’s Rayon for parallel execution, and strategies to avoid deadlocks in C++. These examples are essential for developers looking to leverage the strengths of both languages in their projects.

For further reading, you can watch the full series on Sonia K.’s YouTube channel: C++ vs Rust – Season 1 (NodeGraph Example).

References:

Reported By: Sonia K01451n5k4 – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

Whatsapp
TelegramFeatured Image