简体   繁体   English

在c ++中调用背景函数,类似于unix shell脚本

[英]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.

相关问题 如何在 Z6CE809EACF900BA125B40FABD90FABD90392E 中运行在 Z6CE809EACF900BA125B40FABD92E 中的 Z6CE809EACF900BA125Bshell 中编写一个 cp function (在 linux shell 中) - how to write a cp function (in linux shell ) in c++ that runs in background? Unix上的C ++:重定向Shell输出 - C++ on Unix: Redirecting Shell Output Unix中的C ++ Shell,execv:从函数动态创建并返回可用的第二个参数 - C++ Shell in Unix, execv: Dynamically create and Return a usable second parameter from a function Unix C ++第三方API调用 - Unix C++ Third party API Call C++ - 对 execvp 的函数调用返回无法访问 C++ Shell 程序中的错误 - C++ - Function Call To execvp Returns Cannot Access Error In C++ Shell Program 如何解决这个构建错误:在UNIX下的C ++中“没有匹配的函数调用”? - How can I solve this build error: “no matching function for call to” in C++ under UNIX? c ++检查Unix中是否存在目录,如果存在,则调用void函数。 - c++ Checking if a directory exists in Unix and call void function if it exists. c ++中的read()函数类似于c read() - read() function in c++ similar to c read() 将Gravity(脚本语言)调用转换为本机C函数调用 - Translating a Gravity (a scripting language) call to a native C function call C ++函数调用 - C++ function call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM