stdthread

Why arguments moved twice when constructing std::thread

烈酒焚心 提交于 2020-03-10 04:18:11
问题 Consider this program that essentially creates std::thread that calls the function func() with arg as argument: #include <thread> #include <iostream> struct foo { foo() = default; foo(const foo&) { std::cout << "copy ctor" << std::endl; } foo(foo&&) noexcept { std::cout << "move ctor" << std::endl; } }; void func(foo){} int main() { foo arg; std::thread th(func, arg); th.join(); } My output is copy ctor move ctor move ctor As far as I understand arg is copied internally in the thread object

Why arguments moved twice when constructing std::thread

对着背影说爱祢 提交于 2020-03-10 04:14:26
问题 Consider this program that essentially creates std::thread that calls the function func() with arg as argument: #include <thread> #include <iostream> struct foo { foo() = default; foo(const foo&) { std::cout << "copy ctor" << std::endl; } foo(foo&&) noexcept { std::cout << "move ctor" << std::endl; } }; void func(foo){} int main() { foo arg; std::thread th(func, arg); th.join(); } My output is copy ctor move ctor move ctor As far as I understand arg is copied internally in the thread object

using clr and std::thread

旧城冷巷雨未停 提交于 2020-01-22 02:07:06
问题 I'm creating an UI abstraction layer for desktops. Now I'm implementing the functionality of the .NET framework. The annoying thing is that if I let the users create a CLR Windows Forms Application in Visual studio they can't use all the standard libraries like std::thread and if I let them create another type of application, the console shows up. Is there a way to use clr with std::thread or, even better, is there a way to prevent the console from starting (or hide it from both the screen

Difference between pointer and reference as thread parameter

倾然丶 夕夏残阳落幕 提交于 2020-01-18 04:28:17
问题 This is the example: #include<iostream> #include<thread> using namespace std; void f1(double& ret) { ret=5.; } void f2(double* ret) { *ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret << endl; thread t2(f2, &ret); t2.join(); cout << "ret=" << ret << endl; } And the output is: ret=0 ret=5 Compiled with gcc 4.5.2, with and without -O2 flag. Is this expected behavior? Is this program data race free? Thank you 回答1: The constructor of std::thread deduces

Why move constructor is called twice when passing temporaries to thread function?

元气小坏坏 提交于 2020-01-12 09:18:26
问题 In below code I could not understand why move constructor of class is called twice considering that my thread function is taking argument by rvalue reference and so I was hoping move constructor will be called only once when arguments will be moved to thread constructor.Can somebody give insights on how thread constructor works and how it passes argument to thread function. #include <iostream> #include <thread> #include <chrono> class Test { public: Test() {} Test(Test&&) { std::cout<<"Move

C++: Boost.Asio: Start SSL Server session in a new thread

一世执手 提交于 2020-01-10 03:16:04
问题 I wrote a pair of server/client programs based on this example for the server and I'm done of all communication protocols. The server is supposed to receive multiple connections from multiple connections from multiple client, so I want to separate the sessions from each other, and I'm hoping I could do that with std::thread . This looks to be easy, but I don't know how to do it at all. All examples online seem to show how to run a function in parallel, but it doesn't seem to show how to

Create variable number of std::threads

旧城冷巷雨未停 提交于 2020-01-06 03:49:25
问题 I have a threaded program that I have to run on multiple computers. Each of them have a different number of supported threads. In the computer that I developed the program there are 4 threads , and so I hard coded 4 threads to be created. I want to make this vary according to the situation . I want to use std::thread::hardware_concurrency to get the number of threads , and divide the work into the number of threads available . Is this possible ? The hard coded thread creation is : //const

Executing a member function of a class

落爺英雄遲暮 提交于 2020-01-04 04:46:24
问题 I am trying to experiment the C++11 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown in the first code snippet below on line 20 that is marked . The class definition is given in the 2nd code snippet. When this code is compiled, I am getting a bunch of errors shown in the 3rd snippet. Can anyone tell me what I am doing wrong? Thanks. SNIPPET 1: Thread initialization (main_app.cpp) #include <thread> #include "ServiceRegistrar.hpp"

Moving a unique_lock<recursive_mutex> to another thread

核能气质少年 提交于 2020-01-03 21:01:30
问题 I was wondering what happens when you move a unique_lock that holds a recursive_mutex . Specifically, I was looking at this code: recursive_mutex g_mutex; #define TRACE(msg) trace(__FUNCTION__, msg) void trace(const char* function, const char* message) { cout << std::this_thread::get_id() << "\t" << function << "\t" << message << endl; } future<void> foo() { unique_lock<recursive_mutex> lock(g_mutex); TRACE("Owns lock"); auto f = std::async(launch::async, [lock = move(lock)]{ TRACE("Entry");

Storing an std::thread object as a class member

允我心安 提交于 2020-01-02 09:42:14
问题 I'm trying to keep an std::thread object inside a class. class GenericWindow { public: void Create() { // ... MessageLoopThread = std::thread(&GenericWindow::MessageLoop, *this); } private: std::thread MessageLoopThread; void GenericWindow::Destroy() // Called from the destructor { SendMessageW(m_hWnd, WM_DESTROY, NULL, NULL); UnregisterClassW(m_ClassName.c_str(), m_WindowClass.hInstance); MessageLoopThread.join(); } void GenericWindow::MessageLoop() { MSG Msg; while (GetMessageW(&Msg, NULL,