codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
//----------------------------------------------------------------------------- // >>Code Template<< (for Visual C++) #include <iostream> #include <sstream> #include <iomanip> #include <string> #include <vector> #include <set> #include <map> #include <algorithm> #include <numeric> #include <iterator> #include <complex> #include <cmath> #define cout os using namespace std; typedef long long LL; void END_OF_INPUT_FOR_THIS_TEST_CASE(); // stub for multi-threading //----------------------------------------------------------------------------- // >>Main<< void case_main( ostream& os ) { END_OF_INPUT_FOR_THIS_TEST_CASE(); } //----------------------------------------------------------------------------- // >>Code Template<< (Multi-Thread Solver) #undef cout #include <windows.h> #include <process.h> static const int THREAD_NUM = 2; volatile int g_id; int g_nCase; CRITICAL_SECTION g_cs; vector<string> g_output; unsigned __stdcall thread_main( void* t_id ) { for(;;) { EnterCriticalSection(&g_cs); const int id = ++g_id; if(id>g_nCase) { LeaveCriticalSection(&g_cs); break; } cerr << setw(4) << id << " @ " << (int)t_id << " start" << endl; ostringstream ss; ss << "Case #" << id << ": "; case_main( ss ); EnterCriticalSection(&g_cs); if(g_output.size()<id) g_output.resize(id); g_output[id-1] = ss.str(); cerr << setw(4) << id << " @ " << (int)t_id << " end" << endl; LeaveCriticalSection(&g_cs); } return 0; } void END_OF_INPUT_FOR_THIS_TEST_CASE() { LeaveCriticalSection(&g_cs); } int main() { cin >> g_nCase; InitializeCriticalSection(&g_cs); vector<HANDLE> thread; for(int i=0; i<THREAD_NUM; ++i) thread.push_back( (HANDLE)_beginthreadex(0, 0, &thread_main, (void*)i, 0, 0) ); WaitForMultipleObjects( thread.size(), &thread[0], TRUE, INFINITE ); DeleteCriticalSection(&g_cs); copy( g_output.begin(), g_output.end(), ostream_iterator<string>(cout) ); }
Private
[
?
]
Run code