Home >Backend Development >C++ >How to implement multi-threaded programming using C++ functions?

How to implement multi-threaded programming using C++ functions?

WBOY
WBOYOriginal
2024-04-26 11:12:01410browse

How to use C functions to implement multi-threaded programming? Create a std::thread object with a callable object. Call the join() function and wait for the thread to complete. Use thread synchronization techniques such as mutexes, condition variables, and semaphores to ensure safe access to shared resources. In practical cases, you can create threads to process files in parallel to improve program performance.

如何使用 C++ 函数实现多线程编程?

Using C functions to implement multi-threaded programming

Introduction

Multi-threaded programming involves creating multiple simultaneous task or thread. In C, multithreading can be implemented by using thread functions. This article explains how to use C functions to create and manage threads.

Creating and starting threads

To create threads, you can use the std::thread class. This class takes as parameter a callable object that defines the task to be performed by the thread. The following is sample code to create a thread:

#include <thread>

void thread_function() {
  // 线程要执行的任务
}

int main() {
  std::thread t(thread_function);
  t.join(); // 等待线程完成
  return 0;
}

When the std::thread object is created, it calls the specified function and creates a new thread to execute the function. join() The function waits for threads to complete to ensure that all threads have completed before the main thread exits.

Thread synchronization

In order to ensure safe access to shared resources in a multi-threaded environment, thread synchronization technology needs to be used. C provides several synchronization primitives, including:

  • Mutex (mutex): is used to protect exclusive access to shared resources.
  • Condition variable (condition variable): Used to wait for conditions associated with shared resources.
  • Semaphore: Used to limit concurrent access to shared resources.

Practical case

Suppose we want to create a program that processes files in parallel. We can use multithreading to read and process different parts of the file simultaneously. The following is a program example:

#include <thread>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

vector<string> lines;

void read_file(const string& filename) {
  ifstream file(filename);
  string line;
  while (getline(file, line)) {
    lines.push_back(line);
  }
}

void process_lines() {
  for (auto& line : lines) {
    // 处理行
    cout << line << endl;
  }
}

int main() {
  // 创建读取和处理文件的线程
  thread t1(read_file, "file1.txt");
  thread t2(process_lines);

  // 等待线程完成
  t1.join();
  t2.join();

  return 0;
}

In this example, the read_file() function is used to read the file, while the process_lines() function is used to process each line. Multithreading allows the reading and processing of files to occur simultaneously, thus improving program performance.

The above is the detailed content of How to implement multi-threaded programming using C++ functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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