tutorial3 - CSCC69 Tutorial 3 Dan Zingaro May 17, 2010...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSCC69 Tutorial 3 Dan Zingaro May 17, 2010 Passing Data to Threads: Race Condition We must be careful not to cause a race condition when we pass data to threads. Whats wrong here? int rc, t; pthread_t tids[NUM_THREADS]; for(t=0; t<NUM_THREADS; t++) { rc=pthread_create(&tids[t], NULL, func, (void *) &t); ... } Passing Data to Threads: Caller Stack I Lets say we want to pass three parameters to each created thread I We could create an array on our stack, and pass the start of each three-element block to each thread int main(void) { int rc, t; pthread_t tids[NUM_THREADS]; int args[3 * NUM_THREADS], *arg; for (t = 0; t < NUM_THREADS; t++) { arg = &args[t * 3]; //Put values in arg[0], arg[1], arg[2] rc=pthread_create(&tids[t], NULL, func, (void *)arg); ... } Passing Data to Threads: Caller Stack... I Remember: the array that the threads are using is allocated on our stack I If our stack gets destroyed, the threads data will be corrupted I This wont happen if our function remains active for the entire execution of the threads I...
View Full Document

Page1 / 13

tutorial3 - CSCC69 Tutorial 3 Dan Zingaro May 17, 2010...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online