hey guys today i am learning on what i think it's hardest part while i learn from rust docs and the even the worse i complete the first part on the topic and i still don't get and the part is rust asynchronous anyone can explain async, parallelism, concurrency, cpu, process and thread
Asking such broad questions won't help you, and you'll only waste people's time. Try to further elaborate your posts explaining what's your current mental model and where are you strugging, narrow down your questions asking one question at a time.
ok so im learning first about the concept about concurrency vs parallelism in the beginning i though concurrent vs parallel is 2 different things but when i deep dive about concurrent it's make me struggling because in the parallel it say 2 task that working at the same time ok this is very clear from perspective process that the computer core's more than one thinking from perspective thread it's same right? the main point is everything running at same time
but when i deep dive about concurrent and read in docs there is a part say "(doing, or appearing to do, many things at the same time)" so basically it's mean sometime concurrent can do in parallel execution or because the process is so fast, the documentation states it as such?
All parallelism is concurrency. Some concurrency is parallelism.
Two concurrent tasks occur at overlapping times; that is, task A starts, task B starts, task A ends, task B ends. (Also, I'm just using the word "task" in its colloquial "a piece of work to be done" definition, not any jargon.)
However, tasks need not be executed all at once; the exact sequence of events could be
- First half of task A is executed
- First half of task B is executed
- Task A is completed
- Task B is completed
The exact order in which they're executed might be entirely opaque/unknown to the person who wants tasks A and B to be done, since the executor could take care of those details behind the curtain. The executor might simply appear to do many things at the same time by virtue of frequently swapping between tasks, and that would still be an example of concurrency.
If, at some time, both tasks are literally executing at the same time (e.g. on a multicore processor), then the tasks are said to execute in parallel (making their execution an example of parallelism, in addition to concurrency). Since threads are usually executed in parallel, multithreaded programs are generally taken as examples of parallelism AFAIK.
If you tell your friend a story while you are eating, you eat and speak concurrently, you switch back and forth between chewing and talking, but never truly do both at the same instant.
But your breathing and heartbeat run in parallel, your lungs and heart work at the same time, without taking turns.
i highlight what you say "some concurrency is parallelism" my next question is so the concurrently can say parallelism at what time and what conditions? can you give me example?
Parallelism is a form of concurrency, but there are other forms of concurrency that don't involve parallelism (as in Mroth's example).
get it my bro after that i just creating and my next question is ```fn main() {
let start_total = Instant::now();
let a = std::thread::spawn(|| {
std::thread::sleep(std::time::Duration::from_secs(2));
});
let b = std::thread::spawn(|| {
std::thread::sleep(std::time::Duration::from_secs(2));
});
a.join().unwrap();
b.join().unwrap();
println!("Total time: {} ms", start_total.elapsed().as_millis());
}``
output : Total time: 2004 ms (not exactly 2s because there is a switching context and OS sceduling)
so parallelism is support when we have a computer that have multi-core it's mean parallel is about resource on our computer ? but when we pick a concurrency it's no matter to see from perspective computer as long 2 tasks run it will be concurrency
That would be a form of parallelism running in a local CPU, yes. But you should also think that two computers doing some work, even if they have single-core CPUs, is a form of parallelism.
Yes, you're getting the gist of it
.
get it thank you sir