TechyHarry
TechyHarry

Reputation: 301

Create Process by passing a Function in Visual C++

I am a newbie in C++ working on a module which needs parallel processing.

I need to create 2 or 3 process by passing different functions (which perform some specific task) to each process created from main (similar to _beginthread) in C++ where the main should exit as soon as after creating all process and the new processes created would continue until they finish.

When I tried with threads, My main should wait until all my threads get completed if not as soon as my main exits, all my childs are getting killed. But I don't want this approach because there are many main methods which are present in multiple DLLs. I need to call each main in every DLL and those main(s) will take care of creating the required number of processes.

I tried looking at CreateProcess function which has the following syntax.

BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);

Based on that I understood what ever functions I am talking about in the beginning which I will be passing to process(es) should be created as separate console application (Eg: exe) and then use here.

Is my understanding correct? or is there any other way to really call a function in separate process which are independent of each other?

Please let me know if any further information is required

Upvotes: 1

Views: 2016

Answers (2)

suraj_fale
suraj_fale

Reputation: 960

Yes you understood it correctly. When you use CreateProcess() it actually create a process which has no relation with main. Also you can create multiple process from a single program which are completely independent of each other.

There is no need to call function in separate process which are independent of each other.

Upvotes: 1

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

Typically you would solve the problem by using threads and then waiting for the threads to finish before exiting your main-function. You can wait for threads to finish. http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025%28v=vs.85%29.aspx

Upvotes: 0

Related Questions