用STL库创建线程

 测试了3种方式:

1:子线程不带返回值!!

2:子线程带返回值!!

3:子线程带引用类型参数!!

使用join方式,让父线程等待子线程运行结束.

// TestTemp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>

#include <iostream>
#include<thread>
#include <ctime>
#include<chrono>
#include <thread>   //线程库

using namespace std;

volatile clock_t startt, endt;


void  thread_task(void* para) 
{
	int *params = (int*)para;
	std::this_thread::sleep_for(std::chrono::seconds(params[0]));
	std::cout << "hello thread "
		<< std::this_thread::get_id()
		<< " paused " << params[0] << " seconds" <<",age:" << params[1] << std::endl;

	printf("free addr:0x%lx \n", params);
	delete[] params;
}


int sum(int &x, int &y)
{
	printf("子线程id为:%d\n", std::this_thread::get_id());
	std::cout << std::hex << std::this_thread::get_id() << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(1));
	x += 100;
	y += 100;
	return x + y;
}

int sums(int x, int y, int z)
{
	printf("子线程id为:%d\n", std::this_thread::get_id());
	std::this_thread::sleep_for(std::chrono::seconds(1));
	return x + y + z;
}


int main()
{
	
	//
	 子线程不带返回值
	//
	std::thread threads[5];
	std::cout << "创建 5 threads...\n";
	printf("子线程不带返回值!!\n");
	printf("主线程id:%d\n", std::this_thread::get_id());
	clock_t startt, endt;
	startt = clock();
	for (int i = 0; i < 5; ++i)
	{
		int* para = new int[2];
		printf("malloc addr:0x%lx for thread\n", para);
		para[0] = i + 1;
		para[1] = 20 + i;
		threads[i] = std::thread(thread_task, (void*)para);
	}
	std::cout << "创建5个 threads完毕 ! Now wait for them to join \n";
	for (auto& t : threads) 
	{
		t.join();//线程的活干完了,这个join()函数才能得到返回。父线程将会等待子线程们返回。
	}
	endt = clock();
	printf("All threads joined. cost:%u ms\n", endt - startt);
	//
	 子线程不带返回值 end
	//
	printf("\n\n\n\n===============================================\n");

	//
	 子线程带返回值
	//
	printf("子线程带返回值!!\n");
	printf("主线程id:%d\n", std::this_thread::get_id());
	int x = 3;
	int y = 4;
	int z = 5;
	int result = 0;
	std::thread t = std::thread([&] { result = sums(x, y, z); });
	t.join();
	printf("子线程返回值为:%d\n", result);
	//
	 子线程带返回值 end
	//
	printf("\n\n\n\n===============================================\n");

	//
	 子线程函数中有引用类型参数
	//
	printf("子线程带引用类型参数!!\n");
	printf("主线程id:%d\n", std::this_thread::get_id());

	//
	 子线程函数中有引用类型参数 end
	//
	x = 3;
	y = 4;
	result = 0;
	std::thread t2 = std::thread([&] { result = sum(std::ref(x), std::ref(y)); });
	t2.join();
	printf("子线程执行完毕后,x:%d,y:%d,返回值为:%d\n", x, y, result);

	getchar();
	return 0;
}

运行结果:

创建 5 threads...
子线程不带返回值!!
主线程id:21716
malloc addr:0xd803c8 for thread
malloc addr:0xd80668 for thread
malloc addr:0xd80160 for thread
malloc addr:0xd80438 for thread
malloc addr:0xd804a8 for thread
创建5个 threads完毕 ! Now wait for them to join
hello thread 8324 paused 1 seconds,age:20
free addr:0xd803c8
hello thread 17044 paused 2 seconds,age:21
free addr:0xd80668
hello thread 16956 paused 3 seconds,age:22
free addr:0xd80160
hello thread 17312 paused 4 seconds,age:23
free addr:0xd80438
hello thread 21900 paused 5 seconds,age:24
free addr:0xd804a8
All threads joined. cost:5010 ms


===============================================
子线程带返回值!!
主线程id:21716
子线程id为:22080
子线程返回值为:12


===============================================
子线程带引用类型参数!!
主线程id:21716
子线程id为:15324
3bdc
子线程执行完毕后,x:103,y:104,返回值为:207

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thequitesunshine007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值