#include "stdafx.h" #include <iostream> //#include <Windows.h> #include <mutex> //-------------------------------------------------------------------------- std::mutex g_mutex; //-------------------------------------------------------------------------- struct _Int { _Int() { std::cout << "constructor" << std::endl; } _Int(const _Int&) { std::cout << "copy constructor" << std::endl; } _Int(_Int&&) = delete; ~_Int() { std::cout << "destructor" << std::endl; } int m_i { 42 }; }; //-------------------------------------------------------------------------- void _print(_Int i) { std::cout << i.m_i << std::endl; } //-------------------------------------------------------------------------- void worker2(void* param) { std::lock_guard<std::mutex> lock(g_mutex); std::cout << "worker 2" << std::endl; std::function<void(void)>* lambda = (std::function<void(void)>*)param; (*lambda)(); } //-------------------------------------------------------------------------- void worker() { std::lock_guard<std::mutex> lock(g_mutex); std::cout << "worker 1" << std::endl; _Int y; std::function<void(void)>* lambda = new std::function<void(void)>{[=]() { _print(y); }}; std::thread thread(worker2, lambda); thread.detach(); } //-------------------------------------------------------------------------- int main() { std::thread _thread(worker); _thread.detach(); system("pause"); } Conclusion:
worker 1 constructor copy constructor copy constructor destructor destructor worker 2 copy constructor 42 destructor ΠΠ»Ρ ΠΏΡΠΎΠ΄ΠΎΠ»ΠΆΠ΅Π½ΠΈΡ Π½Π°ΠΆΠΌΠΈΡΠ΅ Π»ΡΠ±ΡΡ ΠΊΠ»Π°Π²ΠΈΡΡ . . . Question: In the output we see 4 constructors and only 3 destructors, where did the 4th go? Also, why is the copy constructor called twice?
Example output for capturing by reference:
worker 1 constructor destructor worker 2 copy constructor 42 destructor ΠΠ»Ρ ΠΏΡΠΎΠ΄ΠΎΠ»ΠΆΠ΅Π½ΠΈΡ Π½Π°ΠΆΠΌΠΈΡΠ΅ Π»ΡΠ±ΡΡ ΠΊΠ»Π°Π²ΠΈΡΡ . . . The code with the capture of the link is invalid, but the balance of designers is respected.
I would be grateful for the link to reading matter, where the life time of variables in lambdas, flows, with capture by value and reference is described in detail.
PS new without delete is made specifically so that the lambda object is alive after the worker () thread has finished
new std::functionthere is nodelete. And it is strange to see such a wild mixture of C ++ and WinAPI. Ifstd::functionandstd::mutex, then why not takestd::thread? - VTT