staticvoid fatal(constchar* message, int code) {
fputs(message, stderr); // exit the test with a non-zero exit code to avoid accidental false positives exit(code);
}
// Adapt from the callback type the OS API expects to // our OS-independent PROCEDURE type. #ifdef _WIN32
DWORD WINAPI procedure(_In_ LPVOID ctxt) { #else void* procedure(void* ctxt) { #endif
Helper* helper = (Helper*)ctxt;
helper->proc(helper->context); return0;
}
// Run 'proc' in a newly started thread, passing 'context' to it // as an argument, and then join that thread. void run_in_new_thread_and_join(PROCEDURE proc, void* context) { struct Helper helper;
helper.proc = proc;
helper.context = context; #ifdef _WIN32
HANDLE thread = CreateThread(NULL, 0, procedure, &helper, 0, NULL); if (thread == NULL) {
fatal("failed to create thread", GetLastError());
} if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { // Should be WAIT_FAILED, since this is not a mutex, and // we set no timeout.
fatal("failed to join thread", GetLastError());
} #else
pthread_t thread; int result = pthread_create(&thread, NULL, procedure, &helper); if (result != 0) {
fatal("failed to create thread", result);
}
result = pthread_join(thread, NULL); if (result != 0) {
fatal("failed to join thread", result);
} #endif
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.