add original source
This commit is contained in:
35
source/listing_2.1.cpp
Normal file
35
source/listing_2.1.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <thread>
|
||||
|
||||
void do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
int& i;
|
||||
|
||||
func(int& i_):i(i_){}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void oops()
|
||||
{
|
||||
int some_local_state=0;
|
||||
func my_func(some_local_state);
|
||||
std::thread my_thread(my_func);
|
||||
my_thread.detach();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
oops();
|
||||
}
|
||||
Reference in New Issue
Block a user