zoukankan      html  css  js  c++  java
  • std::thread的使用

    1 添加头文件#include <thread>

    2 使用全局函数作为线程函数

    #include <iostream>
    #include <thread>
    #include <string>
    using namespace  std;
    
    void ThreadFunc1()
    {
        std::cout << "ThreadFunc1" << std::endl;
    }
    
    void ThreadFunc2(int data)
    {
        std::cout << "ThreadFunc2"<<" "<<data << std::endl;
    }
    
    void ThreadFunc3(int data1,int data2,string str)
    {
        std::cout << "ThreadFunc3" << " "<< data1 << " "<< data2 << " "<< str << std::endl;
    }
    
    int main()
    {
        thread th1(ThreadFunc1);
        th1.join();
        int data1 = 10;
        thread th2(ThreadFunc2,data1);
        th2.join();
        int data2 = 100;
        string str = "StrTest";
        thread th3(ThreadFunc3, data1,data2,str);
        th3.join();
        return 0;
    }

    3 使用类成员函数作为线程函数

    #include <iostream>
    #include <thread>
    #include <string>
    using namespace  std;
    
    
    class Foo
    {
    public:
        void ThreadFunc1() {
            std::cout << "ThreadFunc1" << std::endl;
        }
    
        void ThreadFunc2(int data1,int data2) {
            std::cout << "ThreadFunc2" << " " << data1 << " " << data2 << std::endl;
        }
    };
    
    int main()
    {
        Foo foo;
        thread th1(&Foo::ThreadFunc1,foo);
        th1.join();
        int data1 = 10,data2 = 100;
         thread th2(&Foo::ThreadFunc2, foo,data1,data2);
         th2.join();
        return 0;
    }

    4 使用lambda表达式作为线程函数

    int main()
    {
        thread th([] {while (1)
        {
            std::cout << "lambda" << endl;
        }
        });
        while (1)
        {
            std::cout << "main" << endl;
        }
        th.join();
        return 0;
    }

    结果如下:

     5 leetcode练习

    题目描述:

    public class Foo {
      public void one() { print("one"); }
      public void two() { print("two"); }
      public void three() { print("three"); }
    }
    
    三个不同的线程将会共用一个 Foo 实例。
    
    线程 A 将会调用 one() 方法
    线程 B 将会调用 two() 方法
    线程 C 将会调用 three() 方法
    请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。
    class Foo {
    public:
        Foo() {
            
        }
    
        void first(function<void()> printFirst) {
            
            // printFirst() outputs "first". Do not change or remove this line.
         std::lock_guard<std::mutex> lock(mx_);
          printFirst();
          number_ = 1;
        }
    
        void second(function<void()> printSecond) {
            
            // printSecond() outputs "second". Do not change or remove this line.
            while (1)
          {
              if (number_ == 1)
              {
                  std::lock_guard<std::mutex> lock(mx_);
                  printSecond();
                  number_ = 2;
                  break;
              }
          }
        }
    
        void third(function<void()> printThird) {
            
            // printThird() outputs "third". Do not change or remove this line.
           while (1)
          {
              if (number_ == 2)
              {
                 std::lock_guard<std::mutex> lock(mx_);
                  printThird();
                  number_ = 3;
                  break;
              }
             
          }
        }
        
        std::mutex mx_;
        int number_ = 0;
    };
    class Foo
    {
    public:
        Foo(){}
    
        void first(function<void()> printFirst)
        {
            // printFirst() outputs "first". Do not change or remove this line.
            mx_.lock();
            printFirst();
            number_ = 1;
            mx_.unlock();
        }
    
        void second(function<void()> printSecond)
        {
            // printSecond() outputs "second". Do not change or remove this line.
            while (1)
            {
                if (number_ == 1)
                {
                    mx_.lock();
                    printSecond();
                    number_ = 2;
                    mx_.unlock();
                    break;
                }
            }
        }
    
        void third(function<void()> printThird) 
        {
            // printThird() outputs "third". Do not change or remove this line.
            while (1)
            {
                if (number_ == 2)
                {
                    mx_.lock();
                    printThird();
                    number_ = 3;
                    mx_.unlock();
                    break;
    
                }
    
            }
        }
    
        std::mutex mx_;
        int number_ = 0;
    };
    class Foo
    {//三个线程分别有各自的函数,可以不用加锁
    public:
        Foo() {}
    
        void first(function<void()> printFirst)
        {
            // printFirst() outputs "first". Do not change or remove this line.
            printFirst();
            number_ = 1;
        }
    
        void second(function<void()> printSecond)
        {
            // printSecond() outputs "second". Do not change or remove this line.
            while (1)
            {
                if (number_ == 1)
                {
                    printSecond();
                    number_ = 2;
                    break;
                }
            }
        }
    
        void third(function<void()> printThird)
        {
            // printThird() outputs "third". Do not change or remove this line.
            while (1)
            {
                if (number_ == 2)
                {
                    printThird();
                    number_ = 3;
                    break;
    
                }
    
            }
        }
    
        int number_ = 0;
    };
  • 相关阅读:
    Python 五个知识点搞定作用域
    python中的三元表达式(三目运算符)
    Python中 sys.argv的用法简明解释
    pycharm多行批量缩进和反向缩进快捷键
    Python终端如何输出彩色字体
    第8周LeetCode记录
    第7周Leetcode记录
    《Java核心技术》第九章读书笔记(集合)
    《Java核心技术》第八章读书笔记(泛型)
    《Java核心技术》第七章读书笔记(异常处理)
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/11436100.html
Copyright © 2011-2022 走看看