简体   繁体   中英

call a function in background in c++ similar to unix shell scripting

I want to call function in parallel in c++ the function will take input and perform some formatting, validations , enhancements etc etc .

In unix I can call it inside a loop and pass the values as arguments with function running in BG &.

Example in shell script is :

echo $value | while read arg1 arg2
do
     parser arg1 arg2 &
done
wait

How to do it in c++ with/without multi threading ?

Thanks...

In order to run things 'in the background' or 'in parallel', you must use multithreading (either use multiple threads in the same process, or use multiple processes, depending on the specific case). Whenever you want to run something in the background, you should create a new thread/process, and tell it to run the code you want running in the background, and keep on doing the rest of your code in the original thread/process.

As others have noted, you'll have to either create a sub-process or a thread. Both of these techniques require system dependent code. If you want to be system-independent and have access to a compiler supporting it (I'm not aware of any compiler that doesn't), you can use OpenMP to do the multi-threading. However, this technique is mostly suited to problems where all the threads perform the same action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM