Does cloning and writing create data race conditions in Rust? [closed] - rust

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If a global variable is being cloned (as implemented within the standard library) while being written to will it create a data race?

Cloning data involves reading it. Writing to data involves, well, writing it.
We can safely access data in the following ways
Any number of threads can read data at a given moment, or
One thread can write to data at a given moment, provided no one else is reading or writing
Neither of these conditions applies (we're reading for the clone and we're writing at the same time). Therefore, yes, it's a data race.
As pointed out in the comments, Rust forbids data races. You can't so much as look at a global variable in Rust without an unsafe block, since it's never safe to do so, by Rust's rules. But if you wrap your code in unsafe and don't provide additional protection then yes, this is a data race.

Related

What is the best way to generate and run concurent threads from a for loop over an Arc<Mutex<Vec>>? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Im running both a uni and multi threaded version of an application. There is no speed advantage. That said, what is the best way to access an Arc<Mutex<Vec>> and process each entry concurrently?
You cannot process an Arc<Mutex<Vec<T>>> concurrently - the mutex wraps the entire vector, so no other thread other than the one that locked it will be able to access it.
If you know the number of elements up front, you can use an Arc<Vec<Mutex<T>>>. This has a mutex per-element, so threads will lock only the elements. However you won't be able to grow or shrink the Vec since its shared.
There are also more specialized structures in the Concurrency section of http://lib.rs, with varying semantics, that may fit your needs.

How does Rust enforce/implement RAII [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm working on a (maybe) serious programming language and want to learn about implementing memory management. I want this language to enforce RAII, similar to Rust, but, unlike rust, this language is Object-Oriented and I hope I can implement objects that manage their own memory (like Boxes in Rust). Can anyone go into detail about how Rust handles references to Heap memory?
I think the most obvious way to implement classes is:
Your class variables are implemented as pointers, like in C# and Java.
There is a single owner of the object and all class variables have move semantics in order to enforce this, like in Rust.
Memory is a resource that needs to be cleaned up, so all class variables, after calling the destructor (if any) of the referent object, also call the deallocation routine of your memory allocator, like in C++.
You introduce lifetimes in your type system in order to ensure that lending/borrowing the object doesn't allow any non-owning references to outlive it, like in Rust.

What is the simplest way of sending a struct from one process to another? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two Rust threads; to send a struct from one thread to another I would use a channel. Now I want to split these threads into two OS processes.
Is there a simpler way than using a TCP socket and serializing to JSON back and forth?
I'm fairly new to all of this so I don't even know what terms to Google.
In terms of what to google for the keyword you are looking for is inter-process communication (IPC). There are several ways of doing that and as already mentioned in a comment, Rust doesn't offer much in the standard library.
The thing is that there are many ways to do inter-process communication, each with its own benefits and draw-backs. Maybe start reading here, it will give you some hints on what to google for.
Specifically for your question, if you are concerned about performance when serializing your structs to json you can stick to binary formats as well. Bson might be an option you can have a look at.

what is the best practice for multiple threads writing to one file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a multi-threaded program and all these threads should write their data to a single file.
these threads only writing different strings for some kind of append-only logging
whats the best practice for sharing a file between threads for out put?
For logging (for future questions, make sure you put that information into the question rather than just a comment) there's a strong preference to not have the threads do file access they don't have to; as it means that logging negatively impacts performance for the rest of that thread.
For that reason, NathanOliver's suggestion of having the threads write to a shared container and then one dedicated to dumping that container to file would probably be the best option for you.

Propose how to name a "get/create & get" function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am working on a function that returns an object from a container (e.g an Array) by value (e.g by name). If the object doesn't already exist, then a default copy of the object (default constructor) is created and returned.
That way, this function ALWAYS returns an object, regardless if the object existed prior to calling the function.
Now my question is this: how to name this function? GetOrCreateThenGet sounds stupid. Any ideas?
Edit : I feel something like this would be closer to the function's essence: GetForSure...
The best suitable name for the function would be "Provide" like if you provide goods you can ship it from a warehouse or make/buy a new one and then deliver.
Why do you need anything other than getXXX(). Any method that has and/or means that it is doing to much and is mixing concerns and seriously breaking the Single Responsibility Principle.
Should the caller actually care if the object is being created on demand if all they need from the contract is to get the object?
getOrCreate implies you will get the object or it will be created but not returned.
Again why would I care about the create part?
If I really am supposed to care it would be getOrCreateAndGet to be semantically correct, but highly unorthodox and prone to discussions with peers about methods doing to many things and mixing concerns and breaking the Single Responsiblity Principle? Just because other people name things whacky and non-sensical isn't a good excuse to do it wrong also.
GetGuaranteed seems to cover all the caller needs to know...

Resources