網站首頁 編程語言 正文
C++ std::thread 線程的傳參方式
flyfish
標準 C++11
傳遞一個函數的方式
#include
#include
void thread_function()
{
std::cout << "thread function\n";
}
int main()
{
std::thread t(&thread_function); // t starts running
std::cout << "main thread\n";
t.join(); // 主線程等待線程t完成
std::cout << "end\n";
return 0;
}
柏拉圖理念世界的樣子
柏拉圖可感世界的樣子
結果
main thread
thread function
end
傳值的方式
#include
#include
#include
void thread_function(std::string s)
{
std::cout << "t thread is = " << s << std::endl;
}
int main()
{
std::string s = "abc";
std::thread t(&thread_function, s);
std::cout << "main thread message = " << s << std::endl;
t.join();
return 0;
}
main thread message = abc
t thread is = abc
傳址的方式
#include
#include
#include
void thread_function(std::string &s)
{
std::cout << "t thread is = " << s << std::endl;
s = "cde";
}
int main()
{
std::string s = "abc";
std::thread t(&thread_function, std::ref(s));
t.join();
std::cout << "main thread message = " << s << std::endl;
return 0;
}
結果
t thread is = abc
main thread message = cde
在線程之間不共享內存(不復制)的方式
#include
#include
#include
void thread_function(std::string s)
{
std::cout << "t thread is = " << s << std::endl;
s = "cde";
}
int main()
{
std::string s = "abc";
std::thread t(&thread_function, std::move(s));
t.join();
std::cout << "main thread message = " << s << std::endl;
return 0;
}
結果
t thread is = abc
main thread message =
函數對象的方式
#include
#include
class thread_obj {
public:
void operator()(int x)
{
for (int i = 0; i < x; i++)
std::cout << "Thread using function object as callable\n";
}
};
int main()
{
std::thread th(thread_obj(), 3);
th.join();
return 0;
}
Lambda 表達式的方式
#include
#include
int main()
{
// Define a Lambda Expression
auto f = [](int x) {
for (int i = 0; i < x; i++)
std::cout << "Thread using lambda expression as callable\n";
};
std::thread th(f, 3);
th.join();
return 0;
}
傳遞類的成員函數的方式
#include
#include
class SayHello
{
public:
void greeting(std::string const& message) const
{
std::cout<
堆分配對象的方式
#include
#include
class SayHello
{
public:
void greeting(std::string const& message) const
{
std::cout<<message<<std::endl;
}
};
int main()
{
std::shared_ptr<SayHello> p(new SayHello);
std::thread t(&SayHello::greeting,p,"hello");
t.join();
}
線程可移動構造 (MoveConstructible) 或移動賦值 (MoveAssignable) ;
不可復制構造 (CopyConstructible) 或復制賦值 (CopyAssignable)
#include
#include
void thread_function()
{
std::cout << "t thread function\n";
}
int main()
{
std::thread t(&thread_function);
std::cout << "main thread\n";
std::thread t2 = std::move(t);
t2.join();
return 0;
}
結果
main thread
t thread function
原文鏈接:https://blog.csdn.net/flyfish1986/article/details/123535966
相關推薦
- 2022-12-14 C#中委托和事件的區別詳解_C#教程
- 2022-09-22 k8s 配置存儲之 Configmap & secret
- 2022-09-15 Python實現圖形用戶界面計算器_python
- 2022-09-19 Tomca啟動閃退問題解決(八大類)_Tomcat
- 2022-08-02 goFrame的gqueue與channe的區別_Golang
- 2022-10-02 React在組件中如何監聽redux中state狀態的改變_React
- 2023-10-17 淺記前端遇到的問題 input上傳文件屬性以及第三方組件庫上傳文件屬性
- 2022-03-08 C#中BackgroundWorker類用法總結_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支