C++11多线程03

首先纠正以前的错误:在没有调用join()之前,线程已经运行了。

#include <iostream>
#include <thread>
#include <mutex>

void shared_print(std::string name , int num)
{
    std::cout<<name<<":"<<num<<std::endl ;
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

//    t.join();

    return 0;
}

下面还是把join加上,目前这个程序输出很乱

#include <iostream>
#include <thread>
#include <mutex>

void shared_print(std::string name , int num)
{
    std::cout<<name<<":"<<num<<std::endl ;
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

使用基本的互斥锁,解决资源竞争,看到输出不在凌乱

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mu ;
void shared_print(std::string name , int num)
{
    mu.lock();
    std::cout<<name<<":"<<num<<std::endl ; //缺点:如果某个线程在这里崩溃,就永远不能unlock了
    mu.unlock();
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

改进:自动释放mu

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mu ;
void shared_print(std::string name , int num)
{
    //优点:guard析构时,会自动释放mu,不怕下面那句异常
    //缺点:cout是全局对象,没有完全的保护起来,其他地方在加锁情况下仍然可以使用
    std::lock_guard<std::mutex> guard(mu);
    std::cout<<name<<":"<<num<<std::endl ;

}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

继续优化:去掉上一个程序的缺点

#include <iostream>
#include <thread>
#include <mutex>
#include <fstream>

class LofFile
{
public:
    LofFile()
    {
        f.open("log.txt");
    }
    void shared_print(std::string name , int num)
    {
        std::lock_guard<std::mutex> locker(m_mutex) ;
        f<<name<<": "<<num<<std::endl;
    }
private:
    std::mutex m_mutex ;
    std::ofstream f ;
};

void thread_fun(LofFile& log)
{
    for(int i = 0 ; i>-100 ;i--)
    {
        log.shared_print("子线程",i);
    }
}

int main()
{
    LofFile log ;

    std::thread t(thread_fun,std::ref(log));

    for(int i=0 ;i<100;i++)
    {
        log.shared_print("主线程",i);
    }

    t.join();

    return 0;
}

时间: 2024-08-07 04:18:31

C++11多线程03的相关文章

C++11 并发指南一(C++11 多线程初探)(转)

引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧,和大家共勉. 相信 Linux 程序员都用过 Pthread, 但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++11 的多线程编程方式还是很有益处的. 如果你对 C+

C++11多线程教学(二)

转载自:http://www.cnblogs.com/lidabo/p/3908713.html C++11多线程教学II 从我最近 发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭 建相当复杂的处理图片程序,但是我们回避了线程同步的议题.在接下来的部分,我们将进入C++11多线程编程的同步领域,看看如何来同步一组并行的线程. 我们快速回顾一下如何利用c++11创建线程组.上次教学当中,我们用传统c数

C++11多线程教学(一)

转载自:http://www.cnblogs.com/lidabo/p/3908705.html 本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/) 2. raw string(http://solarianprogrammer.com/

C++11多线程编程之互斥量

+(UIImage*)createImageFromView:(UIView*)view { //obtain scale CGFloat scale = [UIScreen mainScreen].scale; 开始绘图,下面方法,第一个参数表示区域大小.第二个参数表示是否是非透明的.如果需要显示半透明效果,需要传NO,否则传YES.第三个参数就是屏幕密度了 UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.wi

C++11 多线程

C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件<thread>.<mutex>.<atomic>.<future>等用于支持多线程. 使用C++11开启一个线程是比较简单的,下面来看一个简单的例子: #include <thread> #include &

Linux下c++11多线程聊天室

刚看的c++11多线程,写个聊天室试试编译的时候加上 c++11 和 多线程库g++ -Wall -std=c++0x -pthread -o server server.cppserver 和client 都是 q 退出?1. [代码]server.cpp #include <iostream>#include <thread>#include <arpa/inet.h>#include <cstring>#include <vector>#i

c++11 多线程 -- 基本使用

c++11 多线程 – 基本使用 前言:这篇文章仅针对没有使用过c++11线程库的童鞋来快速入门,也是自己的一个简单记录,内容比较基础. -1.线程的基本使用 -2.互斥量 -3.条件变量 -4.原子变量 1.线程的基本使用 代码: #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <thread> #include <iostream> int k = 0;

51CTO学院新课发布~~带你遇见更好的自己(七)(2017.11.27-12.03)

以往新课发布的开场白,都是小编姐姐逗比的闲扯,小编姐姐准备转变一下画风,以后的新课发布开场白就谈谈每周我对于职场或者生活的一点小理解吧. 上周看到一篇文章,关于人和人的身价的差距:职场10年,为什么有人已经当上了董事总经理,而有的人还是资深销售经理?为什么有人已经当上了架构师,而有的人还是资深技术人员?为什么有人已经身价数十亿美金,而有的人还在为竞争总监头衔而周游于人情场?人和人的身价几倍甚至几十倍的差距,真的就只是智商.教育背景.能力.勤奋程度所决定的吗?当然不是.更大程度上是由个人的价值观.

DirectX* 11 多线程渲染的性能、方法和实践

对于在 CPU 上运行的 PC 游戏,渲染通常是主要的性能瓶颈:多线程渲染是一种消除瓶颈的有效方法.本文研究了 DirectX* 11 多线程渲染的性能可扩展性,讨论了多线程渲染的两种基本方法,并介绍了传统多线程延迟着色管线在大型在线游戏<战意*>中的应用.了解更多 原文地址:https://www.cnblogs.com/IDZPRC/p/10912669.html