Home > Backend Development > C++ > How to create and use multi-threading in C++ library?

How to create and use multi-threading in C++ library?

WBOY
Release: 2024-04-18 14:39:02
Original
1023 people have browsed it

Answer: In C, you can use the std::thread function library to create and use multiple threads to implement concurrent programming. Detailed description: Use std::thread to create a new thread and execute the specified code in the child thread. Use synchronization mechanisms such as mutexes and condition variables to ensure thread-safe access to shared data. A practical case shows parallel array sorting, in which multiple threads sort a subset of the array at the same time, improving efficiency.

C++ 函数库如何创建和使用多线程?

C Function Library: Creating and Using Multithreads

Introduction

Multiple Threads are a concurrent programming technology that allows multiple tasks to be performed at the same time. In C, multithreading can be easily created and used by using libraries such as std::thread.

Create thread

To create a thread, you can use std::thread Class:

#include <thread>

using namespace std;

void thread_function() {
  // 要在子线程中执行的代码
}

int main() {
  thread th(thread_function); // 创建新线程
  th.join(); // 等待子线程完成
  return 0;
}
Copy after login

Synchronize thread

In order to ensure that multiple threads can safely access shared data, synchronization mechanisms can be used, such as mutex locks and condition variables:

#include <mutex>
#include <condition_variable>

using namespace std;

mutex mtx; // 互斥锁
condition_variable cv; // 条件变量

int shared_data = 0; // 共享数据

void thread_function() {
  while (true) {
    mtx.lock();
    // 对共享数据进行操作
    mtx.unlock();

    // 通知等待条件变量的线程
    cv.notify_all();
  }
}

int main() {
  thread th(thread_function); // 创建线程

  // 等待条件变量被通知
  unique_lock<mutex> lock(mtx);
  cv.wait(lock);

  // 对共享数据进行操作

  th.join(); // 等待子线程完成
  return 0;
}
Copy after login

Practical case: Parallel array sorting

We can use multi-threading to sort the array in parallel:

#include <thread>
#include <vector>
#include <algorithm>

using namespace std;

void merge(vector<int>& arr, int l, int m, int r) {
  // 对两个子数组进行归并排序
}

void merge_sort(vector<int>& arr, int l, int r) {
  if (l < r) {
    int m = l + (r - l) / 2;
    thread th1(merge_sort, ref(arr), l, m);
    thread th2(merge_sort, ref(arr), m + 1, r);
    th1.join();
    th2.join();
    merge(arr, l, m, r);
  }
}

int main() {
  vector<int> arr = {3, 1, 4, 2, 5};
  merge_sort(arr, 0, arr.size() - 1);
  return 0;
}
Copy after login

The above is the detailed content of How to create and use multi-threading in C++ library?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template