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.
时间: 2024-07-31 23:47:44

thread-01的相关文章

C++使用thread类多线程编程

转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <iostream> #include <thread> #include <Windows.h> using namespace std; void thread01() { for (int i = 0; i < 5; i++) { cout << "T

C++使用thread类进行多线程编程

C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <Windows.h> using namespace std; void thread01() { for (int i = 0; i < 5; i++) { cout << "Thread 01 is working !" << endl; Sleep(

PatentTips - Sleep state mechanism for virtual multithreading

BACKGROUND The present disclosure relates generally to information processing systems and, more specifically, to a mechanism that maintains the architectural state values for both active and inactive software threads that may be executed on a process

多线程(六)线程间的通信和协作

系统要实现某个全局功能必定要需要各个子模块之间的协调和配合,就像一个团队要完成某项任务的时候需要团队各个成员之间密切配合一样.而对于系统中的各个子线程来说,如果要完成一个系统功能,同样需要各个线程的配合,这样就少不了线程之间的通信与协作.常见的线程之间通信方式有如下几种: 1.wait和notify/notifyAll  2.await和signal/signalAll   3.sleep/yield/join  4.CyclicBarrier 栅栏  5.CountDownLatch 闭锁  

Java学习(十):Java线程池实例

线程池可以解决两个不同问题:由于减少了每个任务调用的开销,它们通常可以在执行大量异步任务时提供增强的性能,并且还可以提供绑定和管理资源(包括执行任务集时使用的线程)的方法.每个 ThreadPoolExecutor 还维护着一些基本的统计数据,如完成的任务数. Java常用的线程池有四种.Executors.newCachedThreadPool()(无界线程池,可以进行自动线程回收).Executors.newFixedThreadPool(int)(固定大小线程池).Executors.ne

我喜欢减肥我们来减肥吧

http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313278016/2015.01.28.html http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313282016/2015.01.28.html http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313289016/2015.01.28.html http://www.ebay.com/cln/usli

百度回家看沙发沙发是减肥了卡斯加积分卡拉是减肥

http://www.ebay.com/cln/hpryu-caw8ke/cars/158056866019/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/158445650015/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/158445674015/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/1584456790

巢哑偕倥乇椭煞谙暗逞帕俸

IEEE Spectrum 杂志发布了一年一度的编程语言排行榜,这也是他们发布的第四届编程语言 Top 榜. 据介绍,IEEE Spectrum 的排序是来自 10 个重要线上数据源的综合,例如 Stack Overflow.Twitter.Reddit.IEEE Xplore.GitHub.CareerBuilder 等,对 48 种语言进行排行. 与其他排行榜不同的是,IEEE Spectrum 可以让读者自己选择参数组合时的权重,得到不同的排序结果.考虑到典型的 Spectrum 读者需求

我国第三代移动通信研究开发进展-尤肖虎200106

众所周知,数据科学是这几年才火起来的概念,而应运而生的数据科学家(data scientist)明显缺乏清晰的录取标准和工作内容.此次课程以<星际争霸II>回放文件分析为例,集中在IBM Cloud相关数据分析服务的应用.面对星际游戏爱好者希望提升技能的要求,我们使用IBM Data Science Experience中的jJupyter Notebooks来实现数据的可视化以及对数据进行深度分析,并最终存储到IBM Cloudant中.这是个介绍+动手实践的教程,参会者不仅将和讲师一起在线

pl/sql学习1——标量变量psahnh6S

为类型.不能用于表列的数据类型.范围为的子类型.自然数.为的子类型.具有约束为单精度浮点数.为变量赋值时.后面要加为双精度浮点数.为变量赋值时.后面要加.为数字总位数.为小数位数是的子类型.最大精度位是的子类型.最大精度位单精度浮点型是的子类型.最大精度位双精度浮点型定义精度为位的实数..定义为位的整数.变长字符串.最长测试变量数据!.定长字符串.最长测试变长二进制字符串物理存储的为类型...固定长度.个字节使用定义数据类型那个最小值:最大值:最小值:最大值:最小值:最大值:最小值:最大值:最小