How Do You Know if Threads Are Executing Simultaneously?

Photograph past Michael Dziedzic on Unsplash

Understanding Concurrency and Multi-threaded Programs

I recently had to larn to program in Become because I was developing a multiplayer game for the browser.

I asked on reddit's r/gamedev community and was recommended to take a await at Get for the backend server.

Get is a relatively new language created by Google engineers that has concurrency features baked correct into the language.

We volition learn well-nigh go and multiplayer video games in an upcoming article, but showtime, nosotros must understand how concurrency works.

What the heck is Parallel Execution?

Instead of going direct to concurrency, it might be helpful to understand what parallel execution

If y'all're into reckoner science, yous might have heard about parallel programming, just don't worry if yous haven't.

Executing a program in parallel basically means this.

Running two different pieces of code simultaneously

This means both programs are running at any given point in time. One might finish before the other, but they are not executed sequentially — one instruction after the other — similar a regular program would.

This is groovy because it means we can double the speed of our program in the aforementioned corporeality of fourth dimension. Doing 2 things at once is faster than ane right?

But with great ability comes great responsibility and a price. Well, ii actually.

Conditions to run programs in parallel

Parallel execution requires boosted hardware to be able to run simultaneously.

  • Ane cadre per program run in parallel is required

Both programs must be strictly unrelated in gild to run in parallel.

  • This is because we can't access the same source of data in both programs without blocking 1 of them, which would brand the programme not parallel anymore

Let's discuss restriction #1 to get a improve grasp of why nosotros need at least two cores to run something in parallel.

A little about Processors

Programs and threads are executed past our calculator's processor. A processor is fabricated upwards of cores and each cadre can process but 1 pedagogy at a time.

Luckily for u.s.a., most modern processors come with 4 cores or more. This is why we call them quad-core processors. Smaller devices similar tables might have less than 4 cores to save battery or considering they require less computational power.

For case, my 2019 macbook air has a dual processor. **Cries in Parallel**

Since each cadre can simply process one thing at a time. We need at least two cores to be able to perform whatsoever kind of parallel execution. And then if you have four cores, theoretically, you lot can execute four dissimilar programs at the same time. That's 4 times as fast! But once more, they must exist STRICTLY unrelated.

Two related programs tin't be run in parallel because they crave some sort of synchronization to work properly.

Concurrency for the Win

Computer scientists realized that a lot of programs cannot be executed in parallel because they are related to each other.

Cores also usually have 4 threads each.

A thread is just a subprocess of a program with less overhead.

A multi-threaded programme will have advantage of additional threads — and cores — to distribute the load of the program more efficiently, every bit opposed to have one poor core exercise all the work while the others simply watch.

The premise of concurrency is to run 2 or more than different programs, kind of at the same fourth dimension.

Taking advantage of wasted CPU cycles

Let'southward say we have a program which waits for a user'southward input to print "Hello {user's input} " as opposed to "Hello World".

A process (a thread is also a procedure) has 5 states that it can be at any point in its lifetime.

created past yours truly

Whenever the program is in its ready or waiting state, cpu cycles are being wasted while the program waits for some input or event to burn down.

Concurrency takes reward of this "wasted fourth dimension" to execute another plan while the first one is waiting on said events like for a user to type his username and password, for example.

Computers today are so fast that their operating systems use this strategy to run may programme "at the same fourth dimension", but in reality, it'southward switching betwixt programs really fast that we tin't tell the difference.

Although information technology won't be equally fast as parallelism, concurrency tin can speed up a programme significantly.

Concurrent Programming is HARD

At present that we know what concurrency is and what it tin can accomplish, let's await at some of the problems we may face when implementing information technology.

Race Conditions

A race condition is created when two processes fight for the same resource. This means that the overall program becomes unpredictable because one program may modify a value before the other program has read information technology.

Example Time Again!

Let's say we are implementing a game and nosotros are storing players' locations (Ten,Y) in an hash map.

At present let's say player A shoots at player B, and nosotros need to check if the bullet will hit Role player B and inflict damage.

The collision detection and player input components of our game are running meantime so we demand to make sure no race conditions are created.

So if we have a function that is checking the collision between Player A'south bullet and Histrion B's grapheme, but player B may be moving at the same time, the collision detection programme may change player B'due south position in the array, while nosotros are looking for collisions.

In the best case, this will create inconsistencies in the game logic, like a histrion non being affected by a standoff, just in the worst case, our game will crash because memory can't be read and written to simultaneously.

Either way, nosotros don't want either of these to happen because this issues can be extremely hard to debug every bit nosotros don't know when the lawmaking is being executed.

Concurrent Consequence Resolution

Nosotros will be using the following design of a multiplayer game to discuss some real world scenarios where concurrency can be used.

Multiplayer game system design

Sharing data between threads

Sometimes some data will need to exist shared between threads and this can lead to race atmospheric condition if done improperly.

One fashion to solve this trouble is to provide a way to synchronize the threads so but one of them can access or read from the selected source at whatever given time.

In go, these solution is called a channel. Don't get intimidated by the proper noun, because a channel is basically only a queue with some added functionality.

When a thread is reading from a channel it will cake all access to whatsoever other thread until the current one finishes using information technology. In instance that two or more threads want to access the channel, they volition have to wait until it'due south available. This way, we tin ensure that no race conditions will happen.

Creating a copy of the data

We might encounter a situation where nosotros need to update information many times per second, while at the same fourth dimension a user might be modifying the data at the same time. Sometimes we can't utilize the same channel for everything unless we don't mind for all of our components to depend on each other. Decoupling is a good way to ensure a clean projection that is easily maintainable.

For example, let'due south get back to the game example. When a player shoots a component is in charge of creating a projectile and as well updating the player if it moves.

Using the first method, we can simply send every input request and projectile creating through a channel and take information technology update everything from a single source to prevent a race condition.

Simply it might exist a better solution to update the histrion'due south position directly and send only the projectile. This mode, we merely need to worry almost updating projectiles and checking collisions in our physics component, rather than take to procedure player movement too.

To do this, nosotros create a copy of the player'southward positions and check collisions with that copy since the original data might change while we are checking it.

Because we are updating projectile and checking for collision 60 times per second, this prevents reading from data that is beingness modified at the same fourth dimension and also keeps our physics logic dissever of our actor's input logic.

The other way as mentioned above, would be to procedure everything in the physics component which could make our code less readable and dependent on components that are not related.

What's next?

Concurrency can speed upwards our programs significantly by optimizing the use of resources and wait time that would be otherwise wasted. In many cases using concurrency might amend the plan, but information technology is often required for high operation systems like real time apps and video games.

We volition discuss various strategies used to solve these issues in an upcoming article besides as implementing concurrent programs in Go.

Meanwhile you tin start learning about Go because it is different from traditional languages and may take some time to go used to it.

Have a cracking day!

References

[1] MIT, Concurrency (2014), https://web.mit.edu/6.005/www/fa14/classes/17-concurrency/

[2] Ian Harris, Concurrency in Become (2019), https://world wide web.coursera.org/learn/golang-concurrency

ideabitte.blogspot.com

Source: https://towardsdatascience.com/understanding-concurrency-and-multi-threaded-programs-261047c8231f

0 Response to "How Do You Know if Threads Are Executing Simultaneously?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel