How to make Thread function self contained

Ajay

I am reading "C++ Concurrency in Action". I have following doubt with the below code snippet.

struct func
{
   int& i;
   func(int& i_):i(i_){}
   void operator()()
   {
       for(unsigned j=0;j<1000000;++j)
       {
           do_something(i); //Can have issue of dangling references
       }
   }
};

void oops()
{
    int some_local_state=0;
    func my_func(some_local_state);
    std::thread my_thread(my_func); 
    my_thread.detach();
}

The author says in order to avoid such scenario one way is to make the thread function self-contained and copy the data into the thread rather than sharing the data

I do understand the problem is because of the fact the function object created is local to the oop function and when the oops function finishes the object goes out of scope, but I cannot understand how to avoid in way author has mentioned it.

Sebastian Hoffmann

The issue is not the func object. std::thread will copy your functor.

First the constructor copies/moves all arguments (both the function object f and all args...) to thread-accessible storage

The issue is the reference int& i; your functor keeps to some_local_state which indeed will be invalid once some_local_state runs out of scope.

To solve this copy the value of some_local_state instead of keeping a reference to it. If you need shared access, consider using a std::shared_ptr.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Make RStudio Presentation Self-contained?

From Dev

How can I make a Self Containing Function?

From Dev

How to make Class function repeat it self in python?

From Dev

Pass self reference to contained object's function

From Dev

How to make this function thread safe and fast?

From Dev

How to make 1 part of a function "thread wait"?

From Dev

How to make a extern thread of a class function

From Dev

Why should I reference jQuery in a self contained function?

From Dev

How to write an xml annotation in Java for a self-contained tag with attributes

From Dev

How can I structure a self-contained module in Lua?

From Dev

How to create a redistributable self contained binary distribution of a VM with VirtualBox?

From Dev

How do I compile a theano network into an self-contained executable?

From Dev

How to write an xml annotation in Java for a self-contained tag with attributes

From Dev

How can I structure a self-contained module in Lua?

From Dev

Self Contained WPF .net

From Dev

How to make &mut self from &self?

From Dev

How to call a async function contained in a class?

From Dev

Self-contained generic memento

From Dev

Self-contained shared library

From Dev

Self-Contained Linked List

From Dev

Snappy and self-contained programs

From Dev

How to make WPF ListView horizontal with contained image stretched to fit the height?

From Dev

How to make a stopwatch as a thread in Java?

From Dev

How to make this search in a new thread

From Dev

How to make a thread run faster?

From Dev

How to make a thread run faster?

From Dev

How to make thread safe program?

From Dev

Make a thread to run a Postgres stored function

From Dev

How would I use an enum contained in a struct as a parameter for a function?

Related Related

  1. 1

    How to Make RStudio Presentation Self-contained?

  2. 2

    How can I make a Self Containing Function?

  3. 3

    How to make Class function repeat it self in python?

  4. 4

    Pass self reference to contained object's function

  5. 5

    How to make this function thread safe and fast?

  6. 6

    How to make 1 part of a function "thread wait"?

  7. 7

    How to make a extern thread of a class function

  8. 8

    Why should I reference jQuery in a self contained function?

  9. 9

    How to write an xml annotation in Java for a self-contained tag with attributes

  10. 10

    How can I structure a self-contained module in Lua?

  11. 11

    How to create a redistributable self contained binary distribution of a VM with VirtualBox?

  12. 12

    How do I compile a theano network into an self-contained executable?

  13. 13

    How to write an xml annotation in Java for a self-contained tag with attributes

  14. 14

    How can I structure a self-contained module in Lua?

  15. 15

    Self Contained WPF .net

  16. 16

    How to make &mut self from &self?

  17. 17

    How to call a async function contained in a class?

  18. 18

    Self-contained generic memento

  19. 19

    Self-contained shared library

  20. 20

    Self-Contained Linked List

  21. 21

    Snappy and self-contained programs

  22. 22

    How to make WPF ListView horizontal with contained image stretched to fit the height?

  23. 23

    How to make a stopwatch as a thread in Java?

  24. 24

    How to make this search in a new thread

  25. 25

    How to make a thread run faster?

  26. 26

    How to make a thread run faster?

  27. 27

    How to make thread safe program?

  28. 28

    Make a thread to run a Postgres stored function

  29. 29

    How would I use an enum contained in a struct as a parameter for a function?

HotTag

Archive