c++11 std:thread 多线程

参考:

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

2.C++11 并发指南二(std::thread 详解)

3.C++11 Thread多线程的学习心得与问题

4.C++11多线程(简约但不简单)

5.github(《c++并发编程》基本上述所以例子都出于这里,也不是很长,有一点基础的直接看这个就行)

 

如果第一次接触,直接看我给的参考即可,看过了之后,我把我觉得ok的重点总结在了下面

-----------------------------------------------------笔记--------------------------------------------------

博客一:(一个简单例程+makefile)

c++11 std:thread 多线程

#include <stdio.h>
#include <stdlib.h>

#include <iostream> // std::cout
#include <thread>   // std::thread

void thread_task() {
    std::cout << "hello thread" << std::endl;
}

/*
 * ===  FUNCTION  =========================================================
 *         Name:  main
 *  Description:  program entry routine.
 * ========================================================================
 */
int main(int argc, const char *argv[])
{
    std::thread t(thread_task);
    t.join();

    return EXIT_SUCCESS;
}  /* ----------  end of function main  ---------- */

View Code

c++11 std:thread 多线程

all:Thread

CC=g++
CPPFLAGS=-Wall -std=c++11 -ggdb
LDFLAGS=-pthread

Thread:Thread.o
    $(CC) $(LDFLAGS) -o $@ $^

Thread.o:Thread.cc
    $(CC) $(CPPFLAGS) -o $@ -c $^


.PHONY:
    clean

clean:
    rm Thread.o Thread

View Code

 

博客二:(算是cplusplus官网给的.join()例子的扩充,挺好的)(也有其他函数的官网链接)

c++11 std:thread 多线程

#include <stdio.h>
#include <stdlib.h>

#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for

void thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
        << std::this_thread::get_id()
        << " paused " << n << " seconds" << std::endl;
}

/*
 * ===  FUNCTION  =========================================================
 *         Name:  main
 *  Description:  program entry routine.
 * ========================================================================
 */
int main(int argc, const char *argv[])
{
    std::thread threads[5];
    std::cout << "Spawning 5 threads...n";
    for (int i = 0; i < 5; i++) {
        threads[i] = std::thread(thread_task, i + 1);
    }
    std::cout << "Done spawning threads! Now wait for them to joinn";
    for (auto& t: threads) {
        t.join();
    }
    std::cout << "All threads joined.n";

    return EXIT_SUCCESS;
}  /* ----------  end of function main  ---------- */

View Code

c++11 std:thread 多线程

结果是隔一秒打印一行

其他成员函数

 

博客三:(有一个使用互斥量的例子,介绍了volatile关键字)

如果该线程是在同一类的某一成员函数当中被构造,则直接用this关键字代替即可。

这里使用this指针代替实例化对象的地址

c++11 std:thread 多线程

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

std::mutex mut;

class A{

public:

    volatile int temp;

    A(){
        temp=0;
    }

    void fun(int num){

        int count=10;
        while(count>0){

            mut.lock();
            temp++;
            std::cout<<"thread_"<<num<<"...temp="<<temp<<std::endl;
            mut.unlock();

            count--;
        }
    }

    void thread_run(){
            std::thread t1(&A::fun,this,1);   

            std::thread t2(&A::fun,this,2);

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

int main(){

    A a;

    a.thread_run();
}

View Code

c++11 std:thread 多线程

 

参考五:(hardware_concurrency)

检测硬件并发特性,返回当前平台的线程实现所支持的线程并发数目,但返回值仅仅只作为系统提示(hint)。

#include <iostream>
  #include <thread>
   
  int main() {
      unsigned int n = std::thread::hardware_concurrency();
      std::cout << n << " concurrent threads are supported.n";
  }

c++11 std:thread 多线程

虚拟机设置为2核,支持两个线程并发

 

原文链接: https://www.cnblogs.com/exciting/p/11162855.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++11 std:thread 多线程

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/298551

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午7:52
下一篇 2023年2月15日 下午7:53

相关推荐