c++ - Proper use of threads Win32 -
i learning win32 programming , had question threading. suppose have example below, want know if printstuff() function being called inside for-loop runs on new thread created or run on main thread? thanks.
handle hthrd; dword winapi printstuff() { outputdebugstring("printing stuff"); return 0; } int winapi winmain(...) { ... hthrd = createthread(null, 0, (lpthread_start_routine)printstuff, null, create_suspended, &id) int i; (i = 0; < 5; i++) { printstuff(); } ... }
you seem think createthread
somehow "marks" function run in thread, each time called it's run in thread. it's not that. createthread
, name implies, creates new thread entry point function specified. instructions follow call still running in same thread before, while function specified called once in separate thread few moments after calling createthread
.
incidentally, if going use function standard library on newly created threads should not use directly createthread
, use instead _beginthread
.
i see specifying create_suspended
flag; thus, new thread never run unless call resumethread
on it.
Comments
Post a Comment