COLOR I'd like 15 popups! (On Firefox) THIS ONE (Rick rolling the kids, turn up the volume!)

COLOR I'd like 15 popups! (On Firefox) THIS ONE (Rick rolling the kids, turn up the volume!)

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 333 – Section 9 Threads.

Similar presentations


Presentation on theme: "CSE 333 – Section 9 Threads."— Presentation transcript:

1 CSE 333 – Section 9 Threads

2 HW4 How’s HW4 going? Any Questions?
So how is hw4 going for everyone? Do you guys have any questions about anything? Is everything starting to click now that we’ve talked about HTTP and client/server side programming? Let’s take a look briefly over the final product of the server you’re implementing. (self steps) Create the FLAWED server on attu, connect to it, Make a query, YAY, Then “hmm, what do you guys think will happen if I type something like… (sample queries) I like to search with <em>emphasis</em> I like to search in <span style="font-size:48pt;"><span style="color:red">C</span><span style="color:orange">O</span><span style="color:yellow">L</span><span style="color:green">O</span><span style="color:blue">R</span></span> I'd like 15 popups! <script>for (var i = 0; i < 15; i++) { alert("Popup #" + (i+1)); }</script> (On Firefox) <link rel="stylesheet" href=" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> THIS ONE (Rick rolling the kids, turn up the volume!) <iframe src=" width="500" height="375" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe><p><a href=" got RickRoll'D</a> by Your CSE 333 TA!!!</p> type something like /static/bikeapalooza_2011/Bikeapalooza.html in the URL cool! What happens when I type /static/../hw4/http333d.h? Most browsers are smart enough to catch that However, try telnet on the server’s computer: telnet GET /static/../hw4/HttpServer.h HTTP/1.1 Some of these issues some of the browsers will detect and won’t allow you to do that. (For example, GET /static/.., Safari and Chrome don’t allow the script query to be executed, Firefox does though). This is the third part of the homework, make sure to fix these issues! Now, I’m going to run the clean version, try connecting to it. Make a search. You’ll notice that we all get responses back pretty quickly without having to wait for each other. It’s almost as if we’re doing things in parallel. Which leads us to our next topic

3 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. Soooo… Threads! What are threads? (Draw on board) - They’re essentially an execution state within a process that can independently execute instructions sequentially. All programs consist of at least one thread, namely the main thread. But threads can be spinned off to do work and they’ll essentially share the same address space but different stacks. Why do we want threads?? For concurrency! I.e. For executing simultaneous operations (logically speaking) For example, one thread could be reading from the disk while another does computation Concurrency can also take advantage of paralellism if we have multiple processors

4 POSIX threads (Pthreads)
The POSIX standard provides APIs for creating and manipulating threads. Part of the standard C/C++ libraries, declared in pthread.h In POSIX we use what are called pthreads, which are part of the standard libraries.

5 Core pthread functions
pthread_create(thread, attr, start_routine, arg) pthread_exit(status) pthread_join(thread, value_ptr) pthread_cancel (thread) The main functions for dealing with threads are these 4 operations: Creating threads Exiting threads Joining threads (i.e. waiting for it to finish) Cancelling threads Do we have any questions on these operations and what they’re used for? Let’s look at each one in detail.

6 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: Pointer to a unique identifier for the new thread. (output parameter) 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. Returns 0 on success, non-0 otherwise.

7 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. Notes: - pthread cancel ourselves? Exits successfully Exit success code depends on the parameter given Cancels the thread Entire process terminated Main thread finishes without calling pthread_exit explicitly

8 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(). Notes: effect of retval gives that exit status to any other threads joining with the terminated thread

9 pthread_join Demo: pthreads.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: pthreads.cc Note: About joinable threads, that’s one of the default attributes when passing NULL for attribute parameters in pthread_create After demo of pthreads.cc also show total.cc and ask them if there’s anything wrong with it. (Explain the too much milk problem)

10 mutex pthread_mutex_init(mutex,attr) pthread_mutex_lock(mutex)
pthread_mutex_unlock(mutex) pthread_mutex_destroy(mutex) Demo: total_locking.cc Mutual exclusion: needed to lock access to a variable until a thread is finished with it. Some documentation: pthread_mutex_init(mutex,attr) – pthread_mutex_t * lock and lock attribute (which can be null) pthread_mutex_lock(mutex) – grabs the lock pthread_mutex_unlock(mutex) – releases the lock pthread_mutex_destroy(mutex) – destroys the lock

11 Boost library Used in the homework to help facilitate dealing with strings. Some uses include: Trimming Regex (Pattern matching) Splitting Replacing API: Sample Code: Demo: boostexample.cc Essentially, does the tedious task of dealing with C++ strings for us.

12 C++ threads Not used for the exercise, but is a simpler thread library for C++: #include <thread> Still compile with –pthread Demo: threads.cc #include <thread> in C++ or define a macro for C11 Most C code you’ll find with threads will use pthreads though so it’s good to get familiar with.

13 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?


Download ppt "CSE 333 – Section 9 Threads."

Similar presentations


Ads by Google