#include #include #include #include using namespace std; typedef void* (*thread_func) (void* arg); struct thread { pthread_t self; thread (thread_func f, void* arg = 0) { if (0 != pthread_create (&self, 0, f, static_cast (arg))) throw std::runtime_error (strerror (errno)); } void detach () { pthread_detach (self); } void join () { pthread_join (self, 0); } }; void* identity (void* arg) { return arg; } int main () { int numthreads = 0; try { while (1) { thread t (identity); if (++numthreads % 100 == 0) cout << numthreads << ' ' << flush; } } catch (std::exception& e) { cerr << "Caught exception after " << numthreads << " threads: " << e.what () << endl; } }