Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

C++ initializing a thread with a class/object with parameters

class myFunc
{
public:
  void function()(int* arr, int length)
  {
    cout << "Array length: " << length << "...passed to thread1." << endl;
    
    for (int i = 0; i != length; ++i)
    {
      cout << arr[i] << " " << endl;
    }
  }
};

/************************************************************************/

int arr[7] = { 0, 1, 2, 3, 4, 5, 6 };

myFunc myFunc1;

thread thread1(myFunc1, arr, 7);

if (thread1.joinable())
{
  thread1.join();
}
 
PREVIOUS NEXT
Tagged: #initializing #thread #parameters
ADD COMMENT
Topic
Name
5+9 =