所以現在在coding的時候發現可以加速的地方都會加速..
so 底下提供一些sample, 把你要parallel的指令放入一個list, 然後call c_parallen()就可以達到parallel.
如果用MinGW 記得加上 -mwindows
windows.h記得include
- #define UNICODE
- #include <windows.h>
- #include <iostream>
- #include <vector>
- using namespace std;
- PROCESS_INFORMATION* c_fork(const char * cmd){
- STARTUPINFO si;
- PROCESS_INFORMATION *pi = (PROCESS_INFORMATION*)malloc(sizeof(PROCESS_INFORMATION) );
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( pi, sizeof(PROCESS_INFORMATION) );
- TCHAR Tcmd [4096];
- mbstowcs(Tcmd, cmd, (sizeof(TCHAR) * strlen(cmd) ));
- // Start the child process.
- if( !CreateProcess( NULL, // No module name (use command line)
- Tcmd, // Command line
- NULL, // Process handle not inheritable
- NULL, // Thread handle not inheritable
- FALSE, // Set handle inheritance to FALSE
- 0, // No creation flags
- NULL, // Use parent's environment block
- NULL, // Use parent's starting directory
- &si, // Pointer to STARTUPINFO structure
- pi ) // Pointer to PROCESS_INFORMATION structure
- )
- {
- printf( "CreateProcess failed (%d)\n", GetLastError() );
- return NULL;
- }
- return pi;
- }
- void c_parallel(const char* cmdlist){
- ifstream in(cmdlist, ios::binary);
- string line;
- vector<PROCESS_INFORMATION*> process;
- while( ! in.eof()){
- getline(in, line);
- if( !line.empty() ){
- cout << "do:" << line << endl;
- process.push_back( c_fork( line.c_str() )) ;
- }
- }
- //joint
- for(int i=0; i < process.size(); i++){
- WaitForSingleObject( process[i]->hProcess, INFINITE );
- }
- //close
- for(int i=0; i < process.size(); i++){
- CloseHandle( process[i]->hProcess );
- CloseHandle( process[i]->hThread );
- free(process[i]);
- }
- }
沒有留言:
張貼留言