C++ How to gracefully exit multiprocessing

  • Context: C/C++ 
  • Thread starter Thread starter Bill Simpson
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion focuses on optimizing multiprocessing in C++ by transitioning from heavyweight processes created with fork to lightweight threads using POSIX threads. The author highlights the challenge of gracefully terminating processes without disrupting shared resources like cin and count. A proposed solution involves using a mutex to manage output from threads, ensuring that results are printed without interfering with ongoing computations. This approach enhances performance and simplifies resource management in concurrent programming.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with POSIX threads (pthreads)
  • Knowledge of inter-process communication (IPC) techniques, specifically pipes
  • Basic concepts of mutexes and thread synchronization
NEXT STEPS
  • Research POSIX threads and their implementation in C++
  • Learn about mutexes and their role in thread synchronization
  • Explore inter-process communication methods, focusing on pipes and their usage
  • Investigate performance comparisons between processes and threads in C++ applications
USEFUL FOR

C++ developers, software engineers working on concurrent applications, and anyone interested in optimizing multiprocessing and resource management in their code.

Bill Simpson
Messages
1,077
Reaction score
33
Not a homework problem.

I've written a little compute-intensive code. That only keeps one core busy. So I run four copies. But compute time is variable so I thought I'd try my hand at multiprocessing.

I can fork to create four processes. No problem.

I/O is from cin, count and the processes need to share these to get the work stream and report results. So create a pipe holding a magic cookie. When any process has completed crunching and needs to do I/O it waits on the pipe until it gets the cookie, does the I/O, puts the cookie back in the pipe and goes back to crunching. No problem.

Now the problem. When each process finally discovers the input stream of work has been exhausted I need to gracefully terminate the process. But a few Google searches tell me that if the process just exits it will tear down the shared cin, count and the other processes that are busy working won't be able to report their final results.

Is there a really simple solution to this problem?
 
Technology news on Phys.org
Bill Simpson said:
Is there a really simple solution to this problem?
Bottom line: Use the lightweight processes that you get with POSIX threads instead of the heavyweight processes that you get with fork.

One solution is to make a thread that wants to print something do so as follows: Lock a mutex, print the desired results, and finally unlock the mutex. Threads are simple and switching between threads is generally faster than is switching between heavyweight processes.
 

Similar threads

  • · Replies 29 ·
Replies
29
Views
10K
Replies
2
Views
2K
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 13 ·
Replies
13
Views
8K
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 33 ·
2
Replies
33
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K