multithreading - how can I use multi-threading in a c++ application on a mac? -
i need add threading code below, whats quick way so?
int main() { objecttype obj1; objecttype obj2; printmaze(obj1, obj2); getuserinput(obj1, obj2); return 0; } void printmaze(objecttype&obj1, objecttype&obj2){ /// /// /// } void getuserinput(objecttype&obj1, objecttype&obj2){ /// /// /// } i want able call printmaze , getuserinput in separate threads , have them use same variables [in essence share variable].
just off top of head, quick way implement threads on mac
#include <stdio.h> #include <time.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> //contains information needed struct allinfo{ objecttype obj1; objecttype obj2; }; int main() { srand(time(null)); pthread_t printingthread; pthread_t inputthread; allinfo info; pthread_create(&printingthread, null, &printmaze, &info); pthread_create(&inputthread, null, &getuserinput, &info); pthread_exit(null); return 0; } void *printmaze(void * infoarg) { allinfo *info; info= ( allinfo *)infoarg; /// /// pthread_exit(null); } void *getuserinput(void *infoarg) { allinfo *info; info= (struct allinfo *)infoarg;; //// //// pthread_exit(null); } you'll notice couple of things when passing arguments function called in thread,
[1] can pass 1 argument function if need more 1 argument in function call, have bundle separate arguments using struct. and
[2]when passing arguments function being called in thread, need first accept function object of type "void *" before typecasting original "allinfo" objecttype.
Comments
Post a Comment