zoukankan      html  css  js  c++  java
  • thread-01

    // 8:15 AM/09/28/2017
    #pragma once
    #include <iostream>       // std::cout
    #include <thread>         // std::thread
    #include <mutex>          // std::mutex
    #include <chrono>
    using namespace std;
    volatile int counter(0);
    volatile int counter2(0);
    mutex mtx;//This mutex class is synchronization primitive that
    //can be used to protect shared data from being simultaneously accessed by multiple threads.
    // mutex类是一个同步原语,用来保护共享数据,阻止多线程同时访问
    mutex mtx2;
    
    void run()
    {
        for (int i = 0; i < 100; ++i)
        {
            mtx.lock();// lock mtx,blocks if mtx is not available
            // the word block means that when the mtx is avaiable,it will lock mtx and the following code will being executed
            ++counter;
            cout << this_thread::get_id() << "==> " << counter << endl;
    
            mtx.unlock();// this function will make mtx is available,
            //and other threads that is being blocked will detect the mtx is available
            // but the others don't mean that all of them can detect the mtx is available because if one detect it and it will lock it.
            // only the one thread will own the mtx 
            //here the function unlock is necessary
    //一般不直接使用mutex 而用 std::unique_lock, std::lock_guard等
    //mutex is usually not accessed directly

    } } void run2() { for (int i = 0; i < 100; i++) { if (mtx2.try_lock()) //It differs from the function lock.Here,it will not block and if mtx2 is available,it will be lock and return ture. { ++counter2; cout << this_thread::get_id() << "==> " << counter2 << endl; mtx2.unlock(); } } } int main(int argc, const char* argv[]) { thread ts[10]; for (int i = 0; i < 10; ++i) { ts[i] = thread(run); } for (auto& t : ts) t.join(); std::this_thread::sleep_for(std::chrono::seconds(2)); // sleep for 2s thread ts2[10]; for (int i = 0; i < 10; ++i) { ts2[i] = thread(run2); } for (auto& t : ts2)t.join(); } //We see that the results of counter and counter2 are not same,and we convincingly konw the counter is equal //to 1000 because of the function lock.The counter2,however,may not have a unique result owing to the function // try_lock without blocking.
  • 相关阅读:
    自己写的基类:Forms身份验证类、客户端事件类,序列化类下载
    毕业设计上线啦!跳蚤部落与基于Comet的WebIM系统开发
    域名解析碎片整理 《不同的子域名解析到同一服务器下不同的网站》
    Mac 命令行大全
    position 事件 zindex
    vue 微信公众号网页开发 跳转小程序 踩坑
    React 笔记
    我对架构师的理解(如何成为一个合格的架构师)
    听过我爸是李刚,你听说过我妈是上海人不?
    Lucene.NET打造站内搜索引擎
  • 原文地址:https://www.cnblogs.com/infoo/p/7605691.html
Copyright © 2011-2022 走看看