Boost String API & Threads

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
PTHREADS These notes are from LLNL Pthreads Tutorial
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
The University of Adelaide, School of Computer Science
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS333 Intro to Operating Systems Jonathan Walpole.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
C Threads and Semaphores
Lesson One – Creating a thread
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Linux Processes & Threads
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Boost String API & Threads CSE 333 – Section 9 Boost String API & Threads

Boost String API Used in the homework to help facilitate dealing with strings. Some uses include: Trimming Regex (Pattern matching) Splitting Replacing API:http://www.boost.org/doc/libs/1_57_0/doc/html/string_algo/reference.html Essentially, does the tedious task of dealing with C++ strings for us.

Boost String API Split a single string into multiple substrings string str1("hello abc-*-ABC-*-aBc goodbye"); typedef vector< string > split_vector_type; split_vector_type SplitVec; split( SplitVec, str1, is_any_of("-*"), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }

Boost String API Trim, removes any leading or trailing white spaces Directly modify the original string string str1=" hello world! "; trim(str1); // str1 == "hello world!”

Boost String API Demo: boostexample.cc

Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts with one thread of execution, can spawn more. Threads in a single process share one address space Instructions (code) Static (global) data Dynamic (heap) data Environment variables, open files, sockets, etc. Edward, I believe you meant to write parallel execution for the first bullet?

POSIX threads (Pthreads) The POSIX standard provides APIs for creating and manipulating threads. Part of the standard C/C++ libraries, declared in pthread.h

Core pthread functions pthread_create(thread, attr, start_routine, arg) pthread_exit(status) pthread_join(thread, status) pthread_cancel (thread)

pthread_create #include <pthread.h> int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg ); pthread_create creates a new thread and calls start_routine with arg as its parameter. pthread_create arguments: thread: A unique identifier for the new thread. attr: An attribute object that may be used to set thread attributes. Use NULL for the default values. start_routine: The C routine that the thread will execute once it is created. arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed. Compile and link with –pthread.

Terminating Threads There are several ways in which a thread may be terminated: The thread returns normally from its starting routine; Its work is done. The thread makes a call to the pthread_exit subroutine - whether its work is done or not. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to making a call to either the exec() or exit(). If main()finishes first, without calling pthread_exit explicitly itself.

pthread_exit void pthread_exit(void *retval); Allows the user to terminate a thread and to specify an optional termination status parameter, retval. In subroutines that execute to completion normally, you can often dispense with calling pthread_exit(). Calling pthread_exit() from main(): If main() finishes before the threads it spawned, and does not call pthread_exit() explicitly, all the threads it created will terminate. To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit().

pthread_join Demo: pthread.cc int pthread_join(pthread_t thread, void **retval); Synchronization between threads. pthread_join blocks the calling thread until the specified thread terminates and then the calling thread joins the terminated thread. Only threads that are created as joinable can be joined; a thread created as detached can never be joined. (Refer pthread_create) The target thread's termination return status can be obtained if it was specified in the target thread's call to pthread_exit(). Demo: pthread.cc

Demo: total_locking.cc mutex pthread_mutex_init(mutex,attr) pthread_mutex_lock(mutex) pthread_mutex_unlock(mutex) Pthread_mutex_destroy(mutex) Demo: total_locking.cc

Section exercise (not to be turned in) Create a program that spawns two or three different threads, each of which prints a numeric sequence. Examples: First n odd numbers First n factorials First n primes Use pthread.cc for ideas, but the structure might not be the same. Can you do something in the threads ( maybe sleep() ) so that different runs of the program don’t always produce the same output?