User Tools

Site Tools


c:c_threads:important_considerations:thread_function_arguments_are_always_pass_by_value

This is an old revision of the document!


C - C++ Threads - Important Considerations - Thread function arguments are always pass by value

NOTE: Arguments are always copies into the internal storage for threads.

Any changes made by the thread to the arguments passed does not affect the original arguments.


Example

#include <iostream>
#include <string>
#include <thread>
#include <functional>
 
 
void ChangeTotal(std::int& targetTotal)
{
  targetTotal = 20;
  std::cout << " Changing target_total to " << targetTotal << std::endl;
}
 
int main()
{
  int total = 10;
  std::thread t1(ChangeTotal, total);
  t1.join();
  std::cout << "Current Total is " << total << std::endl;
  return 0;
}
c/c_threads/important_considerations/thread_function_arguments_are_always_pass_by_value.1614940185.txt.gz ยท Last modified: 2021/03/05 10:29 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki