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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值