C++笔记--线程

Posted ljt2724960661

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++笔记--线程相关的知识,希望对你有一定的参考价值。

          C++开发过程中,经常会用到线程,创建线程是必不可少的,C++11 标准中,<thread>头文件提供了 thread 类(位于 std 命令空间中),专门用来完成线程的创建和使用。线程的常见操作如下:

线程创建与使用

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


using namespace std;

void threadFun1(int n) 
	cout << "--->>>thread1 running\\n";
	cout << "n=" << n << endl;

void threadFun2(const char* url) 
	cout << "--->>>thread2 running\\n";
	cout << "url=" << url << endl;



int main()


	//调用第 1 种构造函数
	thread thread1(threadFun1, 6);
	//调用移动构造函数
	thread thread2 = std::thread(threadFun2, "http://www.baidu.com");
	//阻塞主线程,等待 thread1 线程执行完毕
	thread1.join();
	//阻塞主线程,等待 thread2 线程执行完毕
	thread2.join();

system("pause");
return EXIT_SUCCESS;



输出:
--->>>thread2 running
url=http://www.baidu.com--->>>thread1 running
n=
6

传值:

void thread_functionvalue(std::string s)

	std::cout << "t thread is = " << s << std::endl;


int main()


    std::string s = "abc";
    std:thread  t(&thread_functionvalue, s);
	std::cout << "main thread message = " << s << std::endl;
	t.join();


输出:

main thread message = abc
t thread is = abc

传引用:

void thread_function_ref(std::string& s)

	std::cout << "t thread is = " << s << std::endl;
	s = "cde";



int main()


    std::string s = "abc";
	std::thread t(&thread_function_ref,std::ref(s));
	t.join();
	std::cout << "main thread message = " << s << std::endl;

    system("pause");
	return EXIT_SUCCESS;




输出:

t thread is = abc
main thread message = cde

线程条件变量

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <future>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <chrono>
#include <cstdlib>


std::mutex mtx;
//创建一个条件变量
std::condition_variable_any cond;



void print_id() 
	mtx.lock();
	//阻塞线程,直至条件成立
	cond.wait(mtx);
	std::cout << "---->> thread id " << std::this_thread::get_id() << " run" << std::endl;
	//等待 2 秒
	std::this_thread::sleep_for(std::chrono::seconds(2));
	mtx.unlock();

void go() 
	std::cout << "go running\\n";
	//阻塞线程 2 秒钟
	std::this_thread::sleep_for(std::chrono::seconds(5));
	//通知所有等待的线程条件成立
	cond.notify_all();



int main()


std::thread threads[4];
	for (int i = 0; i < 4; ++i)
		threads[i] = std::thread(print_id);
	//创建 1 个线程执行 go() 函数
	std::thread goThread(go);
	//等待所有线程执行结果后,主线程才能继续执行
	goThread.join();
	for (auto& th : threads) 
		th.join();
	

	system("pause");
	return EXIT_SUCCESS;




输出:

go running
---->> thread id 113448 run
---->> thread id 23956 run
---->> thread id 93412 run
---->> thread id 119704 run

线程互斥

#include <mutex>          // std::mutex
#include <chrono>         // std::chrono::seconds()
using namespace std;
int  n = 0;
std::mutex mtx;           
void threadFun() 
    while(n<10)     
        mtx.lock();
        n++;
        cout << "ID" << std::this_thread::get_id() << " n = "<< n << endl;
        mtx.unlock();
        std::this_thread::sleep_for(std::chrono::seconds(1));
    

int main()

    thread th1(threadFun);
    thread th2(threadFun);
    th1.join();
    th2.join();
    return 0;


输出:

id : 134388 n = 1
id : 1948 n = 2
id : 134388 n = 3
id : 1948 n = 4
id : 134388 n = 5
id : 1948 n = 6
id : 1948 n = 7
id : 134388 n = 8
id : 1948 n = 9
id : 134388 n = 10


以上是关于C++笔记--线程的主要内容,如果未能解决你的问题,请参考以下文章

C++笔记--线程

《C++多线程编程》学习笔记

C++笔记--Linux编程(14)-线程同步

C++笔记--线程间共享数据

C++笔记--线程间共享数据

C++并发编程----无锁实现线程安全队列(《C++ Concurrency in Action》 读书笔记)