SlideShare une entreprise Scribd logo
1  sur  9
Boost Threads
Launching a thread Portable  Headers #include <boost/thread.hpp> Launching a thread void find_the_question(intthe_answer);  boost::thread deep_thought_2(find_the_question,42);
Launching a thread struct callable { void operator()(); }; boost::thread make_thread()  { callable x;   return boost::thread(x); // x is destroyed, but the newly-created thread has a copy, so this is OK }
Launching a thread Passing member functions to thread object m_thread = shared_ptr<thread>(new thread(bind(&CTimerThread::Execute, this)));
Thread Completion Join or timed_join member functions of thread object Join – waits for indefinite time for thread to complete – until the method on which the thread executes returns Timed_join takes boost::posix_time as argument m_thread->timed_join( boost::posix_time::microseconds(MAXTIMEOUT_IN_MS) );
Detaching a thread If boost::thread object is destroyed, the thread is detached. Detached thread continues executing until program terminates or till function returns detach() member function of boost::thread object also detaches the thread object.
interrupt A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object. For specified interruption points like join or sleep, the interrupted thread throws  boost::thread_interrupted exception. SuspendThread or ResumeThread not supported?
Thread ID get_id member function returns a unique id for the thread object. Portable way to sleep boost::this_thread::sleep(boost::posix_time::milliseconds(launchtime));
Mutex Void foo() { boost::mutex::scoped_lock l(m_mutex); //m_mutex is a boost::mutexobject //Critical section //Unlock happens automatically at end of scope }

Contenu connexe

Tendances

Tendances (9)

Storm introduction
Storm introductionStorm introduction
Storm introduction
 
Python library
Python libraryPython library
Python library
 
Introduction to NS2 - Cont..
Introduction to NS2 - Cont..Introduction to NS2 - Cont..
Introduction to NS2 - Cont..
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
Namespace1
Namespace1Namespace1
Namespace1
 
Introduction Big Data and Hadoop
Introduction Big Data and HadoopIntroduction Big Data and Hadoop
Introduction Big Data and Hadoop
 
Psycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python ScriptPsycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python Script
 
20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secrets20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secrets
 
Moar tools for asynchrony!
Moar tools for asynchrony!Moar tools for asynchrony!
Moar tools for asynchrony!
 

Similaire à Boost threads

Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
Navaneethan Naveen
 

Similaire à Boost threads (20)

Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with Linux
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Multithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBBMultithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBB
 
Generators & Decorators.pptx
Generators & Decorators.pptxGenerators & Decorators.pptx
Generators & Decorators.pptx
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads
ThreadsThreads
Threads
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Do something in 5 with gas 7-email log
Do something in 5 with gas 7-email logDo something in 5 with gas 7-email log
Do something in 5 with gas 7-email log
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
P threads
P threadsP threads
P threads
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptx
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Dernier (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Boost threads

  • 2. Launching a thread Portable Headers #include <boost/thread.hpp> Launching a thread void find_the_question(intthe_answer); boost::thread deep_thought_2(find_the_question,42);
  • 3. Launching a thread struct callable { void operator()(); }; boost::thread make_thread() { callable x; return boost::thread(x); // x is destroyed, but the newly-created thread has a copy, so this is OK }
  • 4. Launching a thread Passing member functions to thread object m_thread = shared_ptr<thread>(new thread(bind(&CTimerThread::Execute, this)));
  • 5. Thread Completion Join or timed_join member functions of thread object Join – waits for indefinite time for thread to complete – until the method on which the thread executes returns Timed_join takes boost::posix_time as argument m_thread->timed_join( boost::posix_time::microseconds(MAXTIMEOUT_IN_MS) );
  • 6. Detaching a thread If boost::thread object is destroyed, the thread is detached. Detached thread continues executing until program terminates or till function returns detach() member function of boost::thread object also detaches the thread object.
  • 7. interrupt A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object. For specified interruption points like join or sleep, the interrupted thread throws boost::thread_interrupted exception. SuspendThread or ResumeThread not supported?
  • 8. Thread ID get_id member function returns a unique id for the thread object. Portable way to sleep boost::this_thread::sleep(boost::posix_time::milliseconds(launchtime));
  • 9. Mutex Void foo() { boost::mutex::scoped_lock l(m_mutex); //m_mutex is a boost::mutexobject //Critical section //Unlock happens automatically at end of scope }