Threads and Thread Synchronization

Threads and Thread Synchronization - • The process table...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Threads and Thread Synchronization
Image of page 1

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

View Full Document Right 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" ); --- }
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, arg, flags, &thr_id ) Windows CreateThread(attr, stack_size, func,
Image of page 3

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

View Full Document Right Arrow Icon
Introduction to Threads Every thread will have its own Stack PC – Program counter Set of registers State Each thread will have its own function calls, and local variables.
Image of page 4
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • The process table entry will have a stack, set of registers, and PC for every thread in the process. 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" ); } 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 Output: Multi-threaded Program Example...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern