C++ is Fun – Part 15 at Turbine/Warner Bros.! Russell Hanson.

Slides:



Advertisements
Similar presentations
C++ is Fun – Part 10 at Turbine/Warner Bros.! Russell Hanson.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Threads. Readings r Silberschatz et al : Chapter 4.
Multi-core Programming Programming with Posix Threads.
C++ is Fun – Part 12 at Turbine/Warner Bros.! Russell Hanson.
PTHREADS These notes are from LLNL Pthreads Tutorial
Welcome to C++ is Fun at Turbine/Warner Bros.! Russell Hanson.
Multithreading Multithreading is a specialized form of multitasking and a multitasking is the feature that allows your computer to run two or more programs.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition. This material does not exist in first edition)
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.
Jonathan Walpole Computer Science Portland State University
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.
C++ is Fun – Part Nine at Turbine/Warner Bros.! Russell Hanson.
C++ is Fun – Part 13 at Turbine/Warner Bros.! Russell Hanson.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Operating Systems Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled Car with a camera Car is controlled via the.
POSIX Threads Programming The following is extracted from a tutorial by Blaise Barney at Livermore Computing Blaise Barney (Lawrence Livermore National.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition chapter This material does not exist.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Programming with POSIX* Threads Intel Software College.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.
POSIX Threads HUJI Spring 2011.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
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 Dr. Yingwu Zhu. Threaded Applications Web browsers: display and data retrieval Web servers Many others.
Now that you know the pthread API…  How do you create threads?  How do you pass different values to them?  How do you return values from threads? 
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
Tutorial 4. In this tutorial session we’ll see Threads.
Realizing Concurrency using the thread model
Pthreads void *sleeping(void *); /* thread routine */
CS399 New Beginnings Jonathan Walpole.
PTHREADS These notes are from LLNL Pthreads Tutorial
Code Tuning Techniques
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Operating Systems Lecture 14.
PTHREADS AND SEMAPHORES
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Computer Architecture Multi-threaded Matrix Multiply
Presentation transcript:

C++ is Fun – Part 15 at Turbine/Warner Bros.! Russell Hanson

Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming environment Mar ) Objects, encapsulation, abstract data types, data protection and scope April 1-3 3) Basic data structures and how to use them, opening files and performing operations on files – April ) Algorithms on data structures, algorithms for specific tasks, simple AI and planning type algorithms, game AI algorithms April Project 1 Due – April 17 5) More AI: search, heuristics, optimization, decision trees, supervised/unsupervised learning – April ) Game API and/or event-oriented programming, model view controller, map reduce filter – April 29, May 1 7) Basic threads models and some simple databases SQLite May 6-8 8) Graphics programming, shaders, textures, 3D models and rotations May Project 2 Due May 15 9) Threads, Atomic, and Exceptions, more May 20 10) Gesture recognition & depth controllers like the Microsoft Kinect, Network Programming & TCP/IP, OSC May 27 11) Selected Topics June 3 12) Working on student projects - June 10 Final project presentations Project 3/Final Project Due June 10

#include // int main() { // std::cout<<"Hello World\n"; // } void hello(){ std::cout<<"Hello Concurrent World\n"; } int main() { std::thread t(hello); t.join(); } Class Exercise, join a thread Hello Concurrent World

Transferring ownership of a thread

Pthreads is another alternative

#include using namespace std; #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; cout << "Hello World! Thread ID, " << tid << endl; pthread_exit(NULL); } int main () { pthread_t threads[NUM_THREADS]; int rc; int i; for( i=0; i < NUM_THREADS; i++ ){ cout << "main() : creating thread, " << i << endl; rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i); if (rc){ cout << "Error:unable to create thread," << rc << endl; exit(-1); } pthread_exit(NULL); } main() : creating thread, 0 main() : creating thread, 1 main() : creating thread, 2 main() : creating thread, 3 main() : creating thread, 4 Hello World! Thread ID, 0 Hello World! Thread ID, 1 Hello World! Thread ID, 2 Hello World! Thread ID, 3 Hello World! Thread ID, 4 Class Exercise, using pthreads

#include using namespace std; #define NUM_THREADS 5 struct thread_data{ int thread_id; char *message; }; void *PrintHello(void *threadarg) { struct thread_data *my_data; my_data = (struct thread_data *) threadarg; cout thread_id ; cout message << endl; pthread_exit(NULL); } int main () { pthread_t threads[NUM_THREADS]; struct thread_data td[NUM_THREADS]; int rc; int I; for( i=0; i < NUM_THREADS; i++ ){ cout <<"main() : creating thread, " << i << endl; td[i].thread_id = i; td[i].message = "This is message"; rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]); if (rc){ cout << "Error:unable to create thread," << rc << endl; exit(-1); } pthread_exit(NULL); } main() : creating thread, 0 main() : creating thread, 1 main() : creating thread, 2 main() : creating thread, 3 main() : creating thread, 4 Thread ID : 1 Message : This is message Thread ID : 0 Message : This is message Thread ID : 2 Message : This is message Thread ID : 3 Message : This is message Thread ID : 4 Message : This is message Passing Arguments to Threads

#include using namespace std; #define NUM_THREADS 5 void *wait(void *t) { int i; long tid; tid = (long)t; sleep(1); // cout << "Sleeping in thread " << endl; cout << "Thread with id : " << tid << "...exiting " << endl; pthread_exit(NULL); } int main () { int rc; int i; pthread_t threads[NUM_THREADS]; pthread_attr_t attr; void *status; // Initialize and set thread joinable pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for( i=0; i < NUM_THREADS; i++ ){ cout << "main() : creating thread, " << i << endl; rc = pthread_create(&threads[i], NULL, wait, (void *)i ); if (rc){ cout << "Error:unable to create thread," << rc << endl; exit(-1); } // free attribute and wait for the other threads pthread_attr_destroy(&attr); for( i=0; i < NUM_THREADS; i++ ){ rc = pthread_join(threads[i], &status); if (rc){ cout << "Error:unable to join," << rc << endl; exit(-1); } cout << "Main: completed thread id :" << i ; cout << " exiting with status :" << (int)status << endl; } cout << "Main: program exiting." << endl; pthread_exit(NULL); } // free attribute and wait for the other threads pthread_attr_destroy(&attr); for( i=0; i < NUM_THREADS; i++ ){ rc = pthread_join(threads[i], &status); if (rc){ cout << "Error:unable to join," << rc << endl; exit(-1); } cout << "Main: completed thread id :" << i ; cout << " exiting with status :" << (int)status << endl; } cout << "Main: program exiting." << endl; pthread_exit(NULL); } main() : creating thread, 0 main() : creating thread, 1 main() : creating thread, 2 main() : creating thread, 3 main() : creating thread, 4 Sleeping in thread Thread with id : 0...exiting Sleeping in thread Thread with id : 1...exiting Sleeping in thread Thread with id : 2...exiting Sleeping in thread Thread with id : 3...exiting Sleeping in thread Thread with id : 4...exiting

And the bullet-point by bullet-point explanation/elaboration

Thread Identifiers

mutex – mutual exclusion

On Windows, setting threads for each processor #define WIN32_LEAN_AND_MEAN #include HANDLE *m_threads = NULL; DWORD_PTR WINAPI threadMain(void* p); DWORD_PTR GetNumCPUs() { SYSTEM_INFO m_si = {0, }; GetSystemInfo(&m_si); return (DWORD_PTR)m_si.dwNumberOfProcessors; } int wmain(int argc, wchar_t **args) { DWORD_PTR c = GetNumCPUs(); m_threads = new HANDLE[c]; for(DWORD_PTR i = 0; i < c; i++) { DWORD_PTR m_id = 0; DWORD_PTR m_mask = 1 << I; m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain, (LPVOID)i, NULL, &m_id); SetThreadAffinityMask(m_threads[i], m_mask); wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask); } return 0; } DWORD_PTR WINAPI threadMain(void* p) { return 0; }

2 concurrent threads are supported. #include int main() { unsigned int n = std::thread::hardware_concurrency(); std::cout << n << " concurrent threads are supported.\n"; }

Using atomic types – indivisible operations can’t be half-done

The simplest possible thread pool

Next time… threadsafe Cinder Kinect!