windows - Threading in C with CreateThread() -
i novice c, , trying make program run midi sequences, , basically, have 2 functions, both running different midi pattern, , need them run in parallel. due nature of functions (one running sequence , other playing random notes), 100% sure can't have running in same function.
i've been scouring internet clue on how pthreads (which apparently don't work on windows?) , createthread(), can't seem work. trying use createthread() , trying bring in integers required random midi sequence , getting error concerning 'lpthread_start_routine' reads: 'expected 'lpthread_start_routine' argument of type 'dword (*)(int, int, int)'.
a sort of pseudocode of i'm working on here:
dword winapi solo_thread(int key, int tempo, int scale) { ///// contains random midi notes } int backing(int key, int tempo, int backing) { handle thread = createthread(null, 0, solo_thread, null, 0, null); if (thread) { ////// contains midi sequence }
hopefully have explained problem well... aware case going createthread() thing in wrong ways.
thanks!
the signature of thread entry function is, threadproc()
reference page:
dword winapi threadproc( _in_ lpvoid lpparameter );
and solo_thread()
not have signature.
if necessary supply multiple arguments function create struct
containing multiple members representing desired arguments. argument thread must outlive thread otherwise thread accessing dangling pointer. common solution dynamically allocate argument , have thread free()
when no longer requires it.
example:
struct thread_data { int key; int tempo; int scale; }; dword winapi solo_thread(void* arg) { struct thread_data* data = arg; /* use 'data'. */ free(data); return 0; } int backing(int key, int tempo, int backing) { struct thread_data* data = malloc(*data); if (data) { data->key = key; data->tempo = tempo; data->scale = backing; handle thread = createthread(null, 0, solo_thread, &data, 0, null); }
Comments
Post a Comment