IntroductionToThreads - Introduction to Threads CS240...

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

View Full Document Right Arrow Icon
Introduction to Threads CS240 Programming in C
Background image of page 1

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

View Full DocumentRight Arrow Icon
Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called "main thread" that starts the main() function. main() --- --- printf( "hello\n" ); --- }
Background image of page 2
Introduction to Threads You can create multiple paths of execution using: POSIX threads ( standard ) pthread_create( &thr_id, attr, func, arg ) Solaris threads thr_create( stack, stack_size, func, Windows CreateThread(attr, stack_size, func, arg, flags, &thr_id)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Introduction to Threads Every thread will have its own Stack PC – Program counter Set of registers Each thread will have its own function calls, and local variables. The process table entry will have a stack, set of registers, and PC for every thread in the process.
Background image of page 4
Multi-threaded Program Example #include <pthread.h> void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } int main(){ // thread 2 pthread_create( NULL, NULL, prstr, "b\n" ); // thread 3 pthread_create(NULL, NULL, prstr, "c\n" ); // thread 1 prstr( "a\n" ); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Multi-threaded Program Example void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } main(): void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } T1 T2 T3
Background image of page 6
Output: Multi-threaded Program Example
Background image of page 7

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

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

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 16

IntroductionToThreads - Introduction to Threads CS240...

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

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