What is Mutex in C++ (Multithreading for Beginners)

preview_player
Показать описание
In this programming tutorial you will learn about thread safety, multithreading and concurrency. When working with multithreading in programming, one of the crucial moments is knowing how to secure certain parts of the code from being accessed by multiple threads simultaneously, which can lead to serious problems. For that purpose, we use Mutex. In this video, you will learn all about Mutex and how to use it in programming. Considering that Mutex is a very important concept when it comes to multithreading, this video will be valuable for everyone eager to learn more about multithreading. This video contains real world examples and it is easy-to-understand. I will also show you multiple ways of implementing Mutex in C++, and we will discuss the differences as well as the pros and cons of different approaches.

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

I use it to enhance the performance, features, and support for C, C#, and C++ development in Visual Studio.
It is a powerful, secure text editor designed specifically for programmers.

However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇

Related videos:

CONTENTS:
00:00 - What you will learn in this video
01:38 - More about Mutex
02:03 - Real-world example of Mutex
02:40 - Creating a problematic multithreading scenario
06:27 - How to solve a problem with Mutex
07:13 - Implementing Mutex with unique_lock
08:25 - Implementing Mutex with lock_guard
09:08 - Difference between lock_guard and unique_lock
11:18 - Where to learn more

Add me on:
Рекомендации по теме
Комментарии
Автор

📚 Learn programming with these Free E-Books ⬇
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Code:

#include <iostream>
#include<thread>
#include<mutex>
using namespace std;

mutex carMutex;

void driveCar(string driverName) {
//lock_guard<mutex> carLock(carMutex);

unique_lock<mutex> carLock(carMutex);
cout << driverName << " is driving" << endl;

cout << driverName << " is done driving" << endl;
carLock.unlock();
}

int main()
{
thread t1(driveCar, "Saldina");
thread t2(driveCar, "Elon");

t1.join();
t2.join();

cin.get();
}

CodeBeauty
Автор

I think it will be another pill of knowledge to improve our study about C++. Thanks Saldina for all!

carmelostagno
Автор

great explanation, very simplified, thanks

amelccc
Автор

you made yet another complex topic very easy to understand, thank you

lukvkru
Автор

I watch a lot of Saldina's older C++ coding videos and she used to wear a Bat-Man t-shirt quite often. I wonder whatever became of that Bat-Man t-shirt🤔🤨?

christopherrice
Автор

I've been watching your videos for a while now, and I'm pleasantly surprised that you don't include any ads on your channel. It really highlights your dedication to education. We're eagerly anticipating your upcoming programming course!

hsngraphics
Автор

Your method of instruction is really good. First explaining the concept then directly followed by an example implementing the concept. Thanks for your videos.

woodperfekt
Автор

Always the best youtube C++ performer! Thank Saldina!

lsnlst
Автор

I would really like to see a C# series, it would be really helpful with your teaching style!!

fixnemlacika
Автор

I watched your video about multithreading, and I was hoping that you will publish more videos about that topic. Thanks Saldina ❤

marym
Автор

awesome video i was scared of threads up until i came to your channel. You explain everything so well and its so easy to follow thank you !!!

tav
Автор

Amazing and simple to understand thankyou!

joshualloyd
Автор

I tried searching for a particular string in a file but all to no avail.
Please any help

OgunsolaMercy-duif
Автор

im not a beginner but i was once & honestly its so much easier to learn programming when u actually understand whats happening.

for example, u shd state that the namespace this_thread is defined in the header <thread> and so is chrono.

sleep_for can then be considered a function and not a static method of the class this_thread.

chrono::seconds can also be considered a function although im assuming its actually an explicit constructor or a basic constructor that takes in an int & returns and instance of seconds.

only thing i didnt know was the stuff about mutex, so thanks.

its better to specify what exactly is happening as apposed to simply expecting that people just copy ur code.

anon-fzbo
Автор

Thanks, You improved my life 1000 percent Saldina!

PenseBaixoNivel
Автор

Such a beauty, and also the c++!

New sub!

martinalcala
Автор

Yes we like topics like determine big o and asynchronous
Thank you so much

yazenyazen
Автор

my final was 3 days ago i wish u uploaded this sooner lol

mertsecen
Автор

You always remind me of Nadia from American Pie for some reason. I like it 🤠

san-joshuabarrett
Автор

CodeBeauty is on point, both for the code and the coder!

gustavo