Home > Backend Development > C++ > body text

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

王林
Release: 2024-04-26 14:21:01
Original
1047 people have browsed it

Multiprocess programming in C involves creating and managing processes that run in parallel using the header file. Creating a process requires using the std::thread constructor and passing it a function to run. Parameters can be passed as additional parameters through the constructor. A practical case demonstrates the use of multiple processes to calculate the decomposition of large numbers.

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

Use C functions to implement multi-process programming

Introduction

Multi-process programming Is a technique for writing computer programs that allows multiple processes to run simultaneously on a single computer. In C, you can create and manage processes using the header file. This tutorial will guide you in learning how to implement multi-process programming using C functions.

Creating a process

To create a process, you need to use the std::thread constructor. The constructor accepts as argument a pointer to a function that will be run in the new process. The following code example demonstrates how to create a new process that prints a message, sleeps for 5 seconds, and then exits:

#include <thread>
#include <iostream>
using namespace std;

void new_process() {
  cout << "Hello from new process!" << endl;
  this_thread::sleep_for(chrono::seconds(5));
}

int main() {
  thread t(new_process);  
  t.join();  
  return 0;
}
Copy after login

In the main() function, we create a process named t The std::thread object that points to the new_process function. We then use the join() function to wait for the thread to complete.

Passing parameters to the process

You can pass parameters to the process by passing them as additional parameters to the std::thread constructor . The following code example demonstrates how to pass an integer field as a parameter to a new process:

int main() {
  int x = 10;
  thread t(new_process, x); 
  t.join(); 
  return 0;
}
Copy after login

In the new_process function, you can access the passed parameter using the following syntax:

void new_process(int x) {
  cout << "The value of x: " << x << endl;
}
Copy after login

Practical Case

The following is a practical case using multi-process programming to calculate the decomposition of large numbers. In this case we will create a process to handle the decomposition and the main process will wait for the results.

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

using namespace std;

vector<int> factors;

void find_factors(long long n, int start, int end) {
  for (int i = start; i < end; ++i) {
    if (n % i == 0) {
      factors.push_back(i);
    }
  }
}

int main() {
  long long n = 12345678910;
  int num_threads = 4;
  int range = n / num_threads;

  // 创建并运行线程
  vector<thread> threads;
  for (int i = 0; i < num_threads; ++i) {
    threads.push_back(thread(find_factors, n, i * range, (i + 1) * range));
  }

  // 等待线程完成
  for (auto& t : threads) {
    t.join();
  }

  //打印结果
  for (int factor : factors) {
    cout << factor << endl;
  }

  return 0;
}
Copy after login

Conclusion

In this article, you learned how to use C functions to implement multi-process programming, including creating processes, passing parameters, and a practical case. Multi-process programming is a powerful technique that can be used to improve the performance and efficiency of your applications.

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!