当前位置:网站首页>C + + 11 multithreading programming (2) -- mutex usage

C + + 11 multithreading programming (2) -- mutex usage

2020-12-06 14:57:28 A little note, alittle

It's still that question , Learn a new technology point in the programming world , Be sure to understand one thing , Why do we have this technical point , Only when we understand this can we have the motivation to learn . So why do we have multithreaded locks ? In a word .

In order to ensure the accuracy of the data !

Computers were born to calculate data , If you can't guarantee the accuracy of the data , Any technology is just a castle in the air , The same is true of multithreading , So why does multithreading make the data inaccurate ? You can take a look at the following example

#include <iostream>
#include <thread>
#include <string>
using namespace std;
 
void thread_task()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "print thread: " << i << endl;
    }
}
int main()
{
    thread t(thread_task);
    for (int i = 0; i > -10; i--)
    {
        cout << "print main: " << i << endl;
    }
    t.join();
    return 0;
 
}

 

Output results

C++11 Multithreaded programming ( Two )—— The mutex mutex usage

As you can see, there is a very strange phenomenon , Logically speaking, output “print thread:” It should be followed by i Value , however i But it's worth “print main:” It's behind the , This is obviously not what we can get , So why is that ? Because of multithreading , It's the internal control of the operating system , It is usually executed in turn through time slice polling , Even in multicore CPU Next is parallel execution .

So how to solve this problem ? So that we can process the data we need in a thread , And then hand over the control ? This is the use of locks .

Assuming that thread A In execution cout << "print thread: " << i << endl; Before this code , Lock it in the front , When a thread B When you want to grab control , Found this place locked , Can't rob , Can only wait for , Waiting for it to release . After executing that code, you can release the lock , then B Threads are here to grab control , once B You get control and lock yourself , To prevent someone else from taking control of the key points . that C++ How to realize the process of locking ?

C++ One of the classes used is mutex, This Chinese means mutex , seeing the name of a thing one thinks of its function , That is, there can only be one visit at a time , Here's the code

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
using namespace std;
 
mutex mt;
void thread_task()
{
    for (int i = 0; i < 10; i++)
    {
        mt.lock();
        cout << "print thread: " << i << endl;
        mt.unlock();
    }
}
 
int main()
{
    thread t(thread_task);
    for (int i = 0; i > -10; i--)
    {
        mt.lock();
        cout << "print main: " << i << endl;
        mt.unlock();
    }
    t.join();
    return 0;
}

 

Where locks are needed , call metex Of lock() Method , Where to unlock unloc() Method , In this way, you can output the desired results in sequence .

C++11 Multithreaded programming ( Two )—— The mutex mutex usage

That's all C++ On the mechanism of mutex in , It's quite simple and easy to understand .

 

More highlights , Please pay attention to the public of the same name : A little notes alittle

版权声明
本文为[A little note, alittle]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012061457061455.html