首页 > 编程语言 >c++多线程参数传递学习程序及随笔注释

c++多线程参数传递学习程序及随笔注释

时间:2023-01-02 23:00:49浏览次数:47  
标签:std widget copy thread type c++ 参数传递 多线程 data

`#include

include

void f(int i, const std::string &s)
{
}
void oops(int some_para)
{
char buffer[1024];
sprintf(buffer, "%i", some_para);
//! std::thread t(f,3,buffer);
// Threads begin execution immediately upon construction of the associated thread object
// oops will exit before the buffer has been converted to a std::stringe

std::thread t(f, 3, std::string(buffer)); 
// copy(move) std::string while construct thread object

//  Creates new std::thread object and associates it with a thread of execution. The new thread of execution
//* starts executing /*INVOKE*/(std::move(f_copy), std::move(args_copy)...), where
// /*INVOKE*/ performs the INVOKE operation specified in Callable, which can be performed by std::invoke (since C++17), and
// f_copy is an object of type std::decay<Function>::type and constructed from std::forward<Function>(f), and
// args_copy... are objects of types std::decay<Args>::type... and constructed from std::forward<Args>(args)....

//*copy occers during the conversation to its dacay type
t.detach();

}

struct widget_data
{
int value;
};
void update_data_widget(widget_data &data)
{
data. Value += 1;
}
void oops_again()
{
widget_data data = widget_data();
// std::thread t(update_data_widget, data);
/static_assert( __is_invocable<typename decay<_Callable>::type,
typename decay<_Args>::type...>::value,
"std::thread arguments must be invocable after conversion to rvalues"
);
/
// lvalue will be deduced to lreference by Arg&&,and ref is removed by decay,causing static_assert error
// std::ref can warp lreference to pass the assert

std::thread t(update_data_widget, std::ref(data));
t.join();
std::cout << data. Value;

}

struct X
{
void do_lengthy_work(int i) { std::cout << i << std::endl; }
};

int main()
{
oops(1);
oops_again();

X my_x;
std::thread t(&X::do_lengthy_work, &my_x, 1);
t.join();

getchar();

}`

标签:std,widget,copy,thread,type,c++,参数传递,多线程,data
From: https://www.cnblogs.com/fengsc/p/17020790.html

相关文章

  • c++文件基本操作
    c++:文件操作全解文本文件操作读文件类别作用ios::in打开一个文件用于读取ios::out打开一个文件用于写入ios::binary用二进制打开一个文件ios::app......
  • C++/python共享内存交换图片/文本信息
    共享内存保存读取图片OpenShare.cpp#include"OpenShare.h"//共享内存1,,C++发--python传递位姿与图像存储路径intkey_id=1111;intshmid;void*pBuffer;//共......
  • 2D Pose人体关键点实时检测(Python/Android /C++ Demo)
    2DPose人体关键点实时检测(Python/Android/C++Demo)目录​​2DPose人体关键点实时检测(Python/Android/C++Demo)​​​​1.人体关键点数据集​​​​(1)COCO数据集​​......
  • 多线程
    1.线程相关概念1.1程序(program)是为完成特定任务、用某种语言编写的一组指令的集合。简单的说:就是我们写的代码1.2进程进程是指运行中的程序,比如我们使用QQ,就启......
  • C++ | 3-需要函数对象的容器
    函数对象及其特化首先来讨论一下两个重要的函数对象,less和hash。们先看一下less,小于关系。在标准库里,通用的less大致是这样定义的:template<classT>structless......
  • 第十四章《多线程》第6节:线程通信
    ​之前所有的例子中,线程的执行都具有一定的随机性。如果希望线程能够有序的执行,必须使用线程通信技术。Java语言提供了一些线程通信的机制能够保证线程的有序执行,本小节将详......
  • 第十四章《多线程》第7节:线程组
    ​多个线程可以组成一个线程组,线程组可以对一批线程统一调度和管理,也就是说,控制一个线程组相当于控制这个线程组中所有的线程。Java语言以ThreadGroup这个类来表示线程组这......
  • 第十四章《多线程》第9节:ThreadLocal类
    ​如果多个线程共用一个对象,那么这个对象的属性值对于这些线程都是相同的。例如有一个a对象,它有一个x属性,如果x属性的值是1,那么对于任何一个线程而言,a对象的x属性都是1。但......
  • 第十四章《多线程》第4节:控制线程
    ​从14.3小节所列举的各个例子可以很明显的看出:线程的执行有一定的随机性,如果不加以适当控制,会导致执行结果的不确定性。实际开发过程中,很多情况下都需要让线程按照程序员期......
  • 第十四章《多线程》第5节:线程同步
    ​当两个或两个以上的线程需要共享资源,它们需要某种方法来确定资源在某一刻仅被一个线程占用,达到这个目的的过程叫做同步。如果线程在操作共享资源时没有实现同步,那么很有可......