[英]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 . 我想在c ++中并行调用函数,该函数将接受输入并执行一些格式化,验证,增强等操作。
In unix I can call it inside a loop and pass the values as arguments with function running in BG &. 在unix中,我可以在循环内调用它,并将值作为参数传递给在BG&中运行的函数。
Example in shell script is : Shell脚本中的示例是:
echo $value | while read arg1 arg2
do
parser arg1 arg2 &
done
wait
How to do it in c++ with/without multi threading ? 如何在c ++中使用/不使用多线程?
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.
如果您想独立于系统并且可以访问支持它的编译器(我不知道没有编译器),则可以使用OpenMP进行多线程处理。 However, this technique is mostly suited to problems where all the threads perform the same action.
但是,此技术最适合所有线程执行相同操作的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.