pThread_Examples - CourseAnnouncements pthreadExamples Now...

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

View Full Document Right Arrow Icon
Now that you know the pthread API… How do you create threads? How do you pass different values to them? How do you return values from threads? What are some common mistakes? Copyright ©:  University of Il inois CS 241 Staff pthread Examples Copyright ©:  University of Il inois CS 241 Staff Course Announcements ± MP #1 is due at 11:59pm tonight. ± MP #2 released this afternoon. Will appear in your svn at NETID/mp2  and on the course assignment page. Due at 11:59pm, one week from today. Solo MP. Passing Arguments to Threads ± pthread_create() All arguments must be passed by reference and  cast to  (void *) Only one argument to the thread start routine For multiple arguments  ± Creating a structure that contains all of the arguments ± Pass a pointer to that structure in  pthread_create() Copyright ©:  University of Il inois CS 241 Staff Copyright ©:  University of Il inois CS 241 Staff Passing Arguments to Threads ± Passing an int: int i = 42; pthread_create(. .., my_func, (void *)&i); ± Passing a C-string: char *str = "UIUC"; pthread_create(. .., my_func, (void *)str); ± Passing an array: int arr[100]; pthread_create(. .., my_func, (void *)arr); Where should these  be declared? Passing Arguments to Threads ± Retrieving an int: void *myfunc(void *vptr_value) { int value = *((int *)vptr_value); ± Retrieving a C-string: void *myfunc(void *vptr_value) { char *str = (char *)vptr_value; ± Retrieving an array: void *myfunc(void *vptr_value) { int *arr = (int *)vptr_value; Copyright ©:  University of Il inois CS 241 Staff Copyright ©:  University of Il inois CS 241 Staff Passing Arguments to Threads ± Putting it all together: void *myfunc(void *vptr_value) { int value = *((int *)vptr_value); printf("Thread value: %d", value); return NULL; } int main() { pthread_t tid; int i = 6; pthread_create(&tid, NULL, myfunc, &i); pthread_join(tid, NULL); return 0; } Notifies the pthread library to  use default attributes Notifies the pthread library to  ignore return value of myfunc Will this be a  problem?
Background image of page 1

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

View Full DocumentRight Arrow Icon
Thread Argument Passing How can you safely pass data to newly created  threads, given their non-deterministic start-up and  scheduling? ± Make sure that all passed data is thread safe i.e., it cannot be changed by other threads The following code fragment: ± Demonstrates how to pass a simple integer to each thread  ± The calling thread uses a unique data structure for each  thread ± Each thread's argument remains intact throughout the  program Copyright ©:  University of Il inois CS 241 Staff Thread Argument Passing #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 8 char *messages[NUM_THREADS]; void *PrintHello(void *threadid) { int *id_ptr, taskid; sleep(1); id_ptr = (int *) threadid;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

pThread_Examples - CourseAnnouncements pthreadExamples Now...

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

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