C++11 Concurrent programming: Multithreaded std::thread

  • 2020-06-12 10:06:37
  • OfStack

1: an overview

C++11 introduced thread class, greatly reducing the complexity of the use of multithreading. The original use of multithreading can only use the API system, unable to solve the cross-platform problem, a set of code platform transplant, corresponding multithreaded code must be modified. Now in C++11 you can solve this problem using only the language level of thread.

Required header file < thread >

2: Constructor

1. Default constructor

thread() noexcept 1 empty std::thread execution object

2. Initialize the constructor

template < class Fn, class... Args >

explicit thread(Fn & & fn, Args & & ... args);

The std::thread execution object is created. The thread calls the threadFun function, which takes an argument of args.


void threadFun(int a)
{
  cout << "this is thread fun !" << endl;
}
thread t1(threadFun, 2);

3. Copy the constructor

thread(const thread & ) = delete;

The copy constructor is disabled and the std::thread object cannot be copied


void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
}  
int value = 2;
thread t1(threadFun, std::ref(value));

4.Move constructor

thread(thread & & x)noexcept

x is no longer std::thread object


void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
} 
int value = 2;
thread t1(threadFun, std::ref(value));
thread t2(std::move(t1));
t2.join();

3: Member functions

1.get_id()

Gets the thread ID and returns the object of type std::thread::id.


thread t1(threadFun);
thread::id threadId = t1.get_id();
cout << " thread ID : " << threadId << endl;
//threadId To convert to a shaper value, a header file is required <sstream>
ostringstream  oss;
oss << t1.get_id();
string strId = oss.str();
unsigned long long tid = stoull(strId);
cout << " thread ID : " << tid << endl;

2.join()

Create a thread to execute a thread function that blocks the current thread until it has finished executing join.


thread t1(threadFun);
t1.join() // Block waiting for 

3.detach()

After the call to detach, the target thread becomes the daemon thread and runs in the background. The std::thread object associated with it loses its association with the target thread and cannot gain control of the thread through the std::thread object.

4.swap()

Swap two thread objects


thread t1(threadFun1);
thread t2(threadFun2);
cout << " thread 1 the ID : " << t1.get_id() << endl;
cout << " thread 2 the ID : " << t2.get_id() << endl;
t1.swap(t2);
cout << " thread 1 the ID : " << t1.get_id() << endl;
cout << " thread 2 the ID : " << t2.get_id() << endl;

5.hardware_concurrency()

Obtain logical processor storage with return value of int


int coreNum = thread::hardware_concurrency();

4: use

1. Create threads


void threadFun1()
{
  cout << "this is thread fun1 !" << endl;
}
int main()
{
  thread t1(threadFun1);
  t1.join();
  getchar();
  return 1;
}

2. Create thread and pass reference


void threadFun1(int v)
{
  cout << "this is thread fun1 !" << endl;
  cout << v << endl;
}
int main()
{
  int value = 6;
  thread t1(threadFun1, value);
  t1.join();
  getchar();
  return 1;
}

It should be noted that the variables int value and int v do not refer to each other but copy the variables, so before passing to int v, int value cannot be out of scope (freeing memory), join() ensures that the int value variable frees memory, which may be the case if detach() is used.

3. Create thread and pass reference


void threadFun1(int& v)
{
  cout << "this is thread fun1 !" << endl;
  cout << v << endl;
}
int main()
{
  int value = 6;
  thread t1(threadFun1, std::ref(value));
  t1.join();
  getchar();
  return 1;
}

4. Create a thread. The thread function is a class member function


void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
}  
int value = 2;
thread t1(threadFun, std::ref(value));
0

conclusion


Related articles: