Multiple parameters:
|
/* -------------------------------------------------------- Worker thread will receive an INTEGER input parameter -------------------------------------------------------- */ void *worker(void *arg) { int *p; // Pointer (reference, address) to an integer variable int x; // An integer variable p = (int *) arg; // Casting "(void *)" type to "(int *)" x = *p cout << "Hi, my input parameter is " << x << endl; return(NULL); /* Thread exits (dies) */ } |
/* -------------------------------------------------------- Main creates thread and passes param to new thread -------------------------------------------------------- */ int main(int argc, char *argv[]) { pthread_t tid; int param; // Integer param = 12345; pthread_create(&tid[i], &attr, worker, & param); pthread_join(tid, NULL); // Wait for the thread to end... } |
|