YouTip LogoYouTip

Rust Concurrency

Title: Rust Concurrency Programming | \n\nOne of the purposes of Rust's creation is to handle concurrency safely and efficiently, mainly to solve the server's high load capacity.\n\nThe concept of concurrency (concurrent) refers to different parts of a program executing independently, which is easily confused with the concept of parallelism (parallel), which emphasizes "simultaneous execution".\n\nConcurrency often leads to parallelism.\n\nThis chapter describes programming concepts and details related to concurrency.\n\n### Thread\n\nA thread (thread) is an independent running part of a program.\n\nThe difference between threads and processes (process) is that threads are concepts within the program, and programs are usually executed within a process.\n\nIn an environment with an operating system, processes are often scheduled alternately for execution, while threads are scheduled within the process by the program.\n\nSince thread concurrency is very likely to result in parallel situations, deadlocks and delay errors that may be encountered in parallel often appear in programs containing concurrency mechanisms.\n\nTo solve these problems, many other languages (such as Java, C#) use special runtime software to coordinate resources, but this undoubtedly greatly reduces the program's execution efficiency.\n\nC/C++ language also supports multithreading at the lowest level of the operating system, and the language itself and its compiler do not have the ability to detect and avoid parallel errors, which puts great pressure on developers, who need to spend a lot of effort to avoid errors.\n\nRust does not rely on the runtime environment, which is similar to C/C++.\n\nHowever, Rust has designed mechanisms within the language itself, including the ownership mechanism, to eliminate the most common errors at the compile stage as much as possible, which other languages do not have.\n\nBut this does not mean we can be careless when programming. So far, problems caused by concurrency have not been fully resolved in the public domain, and errors may still occur. Be extra careful when doing concurrency programming!\n\nIn Rust, new threads are created through the std::thread::spawn function:\n\n## Example\n\n```rust\nuse std::thread;\n\nuse std::time::Duration;\n\nfn spawn_function(){\n\nfor i in 0..5{\n\n println!("spawned thread print {}", i);\n\n thread::sleep(Duration::from_millis(1));\n\n}\n\n}\n\nfn main(){\n\n thread::spawn(spawn_function);\n\nfor i in 0..3{\n\n println!("main thread print {}", i);\n\n thread::sleep(Duration::from_millis(1));\n\n}\n\n}\n\nRunning result:\n\nmain thread print 0\nspawned thread print 0\nmain thread print 1\nspawned thread print 1\nmain thread print 2\nspawned thread print 2\n\nThe order of this result may change in some cases, but overall it is printed like this.\n\nThis program has a child thread, which is intended to print 5 lines of text, and the main thread prints three lines of text, but obviously as the main thread ends, the spawn thread also ends, and not all printing is completed.\n\nThe parameter of std::thread::spawn function is a parameterless function, but the aboveapproach is not the recommended way. We can use closures to pass functions as parameters:\n\n## Example\n\n```rust\nuse std::thread;\n\nuse std::time::Duration;\n\nfn main(){\n\n thread::spawn(||{\n\nfor i in 0..5{\n\n println!("spawned thread print {}", i);\n\n thread::sleep(Duration::from_millis(1));\n\n}\n\n});\n\nfor i in 0..3{\n\n println!("main thread print {}", i);\n\n thread::sleep(Duration::from_millis(1));\n\n}\n\n}\n\nClosures are anonymous functions that can be saved into variables or passed as parameters to other functions. Closures are equivalent to Lambda expressions in Rust, with the following format:\n\n|param1, param2, ...| -> return_type
← Python3 Os PardirRust Collection String β†’