Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

C++ using a member function of a class to pass parameters to a thread

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

    for (int i = 0; i != length; ++i)
    {
      cout << arr1[i] << " ";
    }

    cout << "Make negative all elements/integers of array." << endl;

    for (int i = 0; i != length; ++i)
    {
      arr1[i] *= -1;

      cout << arr1[i] << " ";
    }
  }
};

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

int arr2[7] = { -1, -2, -3, -4, -5, -6, -7 };

thread thread1(&myFunc::negativeSign, &myFunc1, arr2, 7);

if (thread1.joinable())
{
  thread1.join();
}
 
PREVIOUS NEXT
Tagged: #member #function #class #pass #parameters #thread
ADD COMMENT
Topic
Name
5+4 =