Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
Does simultaneous execution of threads (in a C code in Linux) in two different terminals give different results?

What I have tried:

Hello, I wrote a code which creates 4 threads. The working of the 4  threads are similar, but the number of iterations for each of them and the time of trigger is different. I executed the codes simultaneously on 2 terminals and both yield results. I have an additional function to find out the task with the maximum execution time. For every execution, this function's result is also different. Is this they way it is supposed to be? 
Posted
Updated 11-Feb-20 23:05pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Ah, threads ... are they the same ever time?

In an ideal situation every app will always be deterministic: each time you run it, it will give the same results. But we live in the real world, and our OS is not doing the same things each time your app is run. So when you start multithreading, you have to realise that each thread is independent unless you specifically code for them to synchronise (and that gets extremely complicated if you aren't very, very careful). That independence means that the OS is at liberty to run them as it sees fit, and as cores withing your processor become free or are preemptively freed to allow other threads to run.

So depending on how you have coded them - and it doesn't sound like you have coded them as independent units, they seem to be accessing the same variables and that's very dangerous - you can get very different results on two separate runs. Without your code, we can't tell what might happen in your specific case, but do be aware that when you write a line of code, no matter how simple it is it isn't executed as a single unit, it is executed as a set of steps, so if another thread is also accessing the same variables, the results can become very unpredictable because they can literally be changing the same variable at the same time!

This is not a good field for beginners: you need to think very, very carefully and plan out threading before you start coding, or you can get into the situation where one thread is affecting another and they both cannot run until the other has.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
Is this they way it is supposed to be?
Yes.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
Does the execution of threads differ at every execution?

Yes, it always differ because Os already have hundred of threads running continuously and it is impossible to repeat exact same conditions on 2 runs.
Quote:
this function's result is also different. Is this they way it is supposed to be?

If result reflect thread scheduling, it is normal.
If result is computation, it is not normal. You have to design your code to take into account this behavior of threads.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100