cs552-s10-pthreads

Cs552-s10-pthreads - Click to edit Master subtitle style POSIX Threads(Pthreads Operating Systems CS 552 Spring 2010 Kenneth Chiu Compiling •

Info iconThis preview shows pages 1–12. 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

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

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: Click to edit Master subtitle style 1/20/11 POSIX Threads (Pthreads) Operating Systems CS 552 Spring 2010 Kenneth Chiu 1/20/11 Compiling • Include pthread.h . • Solaris cc –mt … -lpthread • Linux gcc –pthread … • Do not manually define _REENTRANT , or link against any libraries! 1/20/11 ALWAYS CHECK YOUR RETURN CODES WITH AT • It will save you much, much time! – ec = pthread_some_function(...); assert(ec == 0); – ec = pthread_some_function(...); assert(ec == 0); • I have learned this from painful experience! 1/20/11 Thread-Safety • What does thread-safety mean? • What about signals? Can I call any thread-safe function from a signal handler? • Is this function thread safe? – void foo() { lock(&mutex); // Do something. unlock(&mutex); } Can it be called from a signal 1/20/11 • A function that can always be re- entered, even when currently executing it, is reentrant. – Is every reentrant function thread-safe? – Is every thread-safe function re-entrant? – Sometimes a different definition of thread-safe is used: A function is thread- safe if protected access to it will work. • Sources of problems: – Returning static data: • const char *ctime(const time_t *clock); 1/20/11 Creation 1/20/11 Creation • int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); • Example: – extern "C" void *run(void *data) { … } // Use default attributes, pass 1234 as void *. int ec = pthread_create(&tid, NULL, run, (void *) 1234); assert(ec == 0); 1/20/11 Thread Attributes • Stack characteristics: – Size: • int pthread_attr_setstacksize(&attr, size_t); – Can provide the stack itself: • int pthread_attr_setstackaddr(&attr, void *stack); • User level or kernel threads: – pthread_attr_setscope(&attr, 1/20/11 Passing Data • Passing data: – pthread_create(&tid, NULL, run, (void *) 123); void *run(void *vp) { int i = (int) vp; // int i = (int) (long) vp; ... } How do we pass more than one variable? Suppose we want to pass a string and an integer? 1/20/11 – We can pass a pointer to a struct. struct Data { char name[100]; int count; }; void *thread_func(void *vp) { fprintf(stderr, "%s\n", ((Data *) vp)->name); ... } int main(int argc, char *argv { ... Data data = {“”, 0}; for (int i = 0; i < n_threads; i++) { sprintf(data.name, “Thread:%04d”, i); pthread_create(&tid[i], 0, thread_func, &data); data.count++; } ... } [Show passing_data_thread.cpp .] • Cannot pass on stack, unless we make sure that the variable persists on the 1/20/11 – Could be done using a semaphore. struct Data { char name[100]; int count; }; void *thread_func(void *vp) { char name[100]; strcpy(name, ((Data *) vp)->name); up(sync_sem); fprintf(stderr, "%d\n", name); ......
View Full Document

This document was uploaded on 01/20/2011.

Page1 / 80

Cs552-s10-pthreads - Click to edit Master subtitle style POSIX Threads(Pthreads Operating Systems CS 552 Spring 2010 Kenneth Chiu Compiling •

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

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