multithreading - Run a specific set of lines within a function in a different thread in C -
i have huge function (length>4000)
lines. in function, have more 100 variables declared in beginning. now, want run specific block of lines in different thread. example, want run lines 2000-3000
in different thread. how do this?
to scale down example, have:
int functiona() { .....variables declared...... .....variables declared...... printf("hello"); printf("this"); printf("is in another"); printf("thread"); }
i want run 4 printf
functions in thread.
to this, i've done:
int functiona() { .....variables declared...... .....variables declared...... void functionb() { printf("hello"); printf("this"); printf("is in another"); printf("thread"); } pthread_create(&tid, null, functionb, null); pthread_join(tid, null); }
i know terrible way this. however, there many variables pass in case want make functionb
new independent function.
please let me know how proceed.
what in case is: create struct containing needed variables. create new function pointer struct parameter. can create new thread using function , have pass struct. struct creation coded fast, have put
struct nameforstruct { //declare vars here, e.g.: int somevar; }
around , change access vars copy-pasting structname-> in front of it.
function may like:
void threadingstuff(struct nametostruct * myvars) { if (myvars->somevar == 1) { // stuff } }
that in opinion fastest way achieve want (and way lessest work). consider refactoring better approach...
Comments
Post a Comment