POCO C + + routines collation--About threads

Source: Internet
Author: User

This article mainly collated the online see, as well as their own writing about the AI poco examples, the spirit of open source sharing, for everyone to reference, speed up the learning and use of POCO Framework library, accelerate their application in the project, accelerate product design and development.

Example one: Incoming object

in Poco, the entry function is abstracted into a class runnable, which provides a void run () interface that the user needs to inherit into the class to implement a custom entry function. Poco also abstracts threads into a class thread, providing methods such as Start, join, and so on:

Defines a thread object, invokes its Start method and passes in a Runnable object to start the thread, using a method that is relatively simple

#include "poco/thread.h" #include "poco/runnable.h" #include <iostream>class hellorunnable:public Poco::    runnable{virtual void Run () {std::cout << "Hello, world!" << Std::endl;    }};int Main (int argc, char** argv) {hellorunnable runnable;    Poco::thread Thread; Thread.Start (runnable);//Incoming object instead of object pointer Thread.Join ();}

Example two: Passing in a function in a well-defined class

If your thread's entry function is in another defined class, Poco provides an adapter to enable the thread to boot from the portal you specify, without modifying the existing classes:

#include "poco/thread.h" #include "poco/runnableadapter.h" #include <iostream>class greeter{public:void greet ()   {std::cout << "Hello, world!" << Std::endl;    }};int Main (int argc, char** argv) {Greeter Greeter;    Poco::runnableadapter<greeter> runnable (Greeter, &greeter::greet);    Poco::thread Thread;    Thread.Start (runnable); Thread.Join ();//wait for the thread technology return 0;}

Example three: Passing in functions and parameters directly

Thread::start can also pass in functions and parameters in addition to receiving runnable objects

#include <iostream> #include "poco/thread.h" #include "poco/threadlocal.h" #include "poco/runnable.h" using namespace Std; Using namespace Poco;void SayHello (void* name) {cout<< "Hello" << (char*) Name<<endl;}    int main () {static char* name = "Djwu";    Thread thr;    Thr.start (SayHello, name);    Thr.join (); return 0;}

Example four: thread-local variable storage

The Threadlocal class provides developers with a more concise way to use the TLS mechanism, which is used to hold variables such as: they have different values in different threads, and each maintains that the thread cannot access these variables in other threads.

The memory content that is modified in one thread is in effect for all threads. This is an advantage as well as a disadvantage. Say it is the advantage, the data exchange of the thread becomes very fast. Say it is a disadvantage, a thread died, other threads are also life-insured; Multiple threads access shared data, require expensive synchronization overhead, and are prone to synchronization-related bugs.

A new mechanism is required if you need a variable that can be accessed by each function call within a thread, but not accessible by other threads (known as static memory local to a thread threads local static variable). This is TLS.

#include   "Poco/thread.h" #include   "poco/runnable.h" #include   "poco/threadlocal.h" #include   <iostream>class counter: public poco::runnable{    void run ()    {        static Poco::ThreadLocal<int>  tls;        for  (*tls = 0; *tls <  10; ++ (*TLS))         {             std::cout << *tls << std::endl;         }    }};int main (int argc, char** &NBSP;ARGV) {    counter counter;    poco::thread t1;     poco::thread t2;    t1.start (counter);     T2.start (counter);   &nbsP; t1.join ();     t2.join ();     return 0;} 

example five: A comprehensive example

#include  <iostream> #include   "poco/thread.h" #include   "poco/runnable.h" #include   "poco/ ThreadTarget.h "#include   poco/runnableadapter.h" Using namespace std ;using poco:: thread;using poco::runnable;using poco::threadtarget;using poco::runnableadapter;class  Myrunnable:public runnable{public:    void run ()  { std::cout < <  "hello myrunnable." &NBSP;&LT;&LT;&NBSP;STD::ENDL;&NBSP;}};VOID&NBSP;GFUN4TD () {    std::cout <<   "HELLO&NBSP;GFUN4TD" &NBSP;&LT;&LT;&NBSP;STD::ENDL;} Class staticfun4td{public:    static void staticfun ()  { std::cout  <<  "Hello static fun."  << std::endl; }};class commfun4td{public:    void commfun ( )  { std::cout <<  "Hello common function." &NBSP;&LT;&LT;&NBSP;STD:: Endl; }};int main () {    thread t1 ("Myrun");     thread &NBSP;T2 ("global"); &NBSP;&NBSP;&NBSP;&NBSP;THREAD&NBSP;T3 ("Static");     thread t4 (" Comm ");     myrunnable rmy;    threadtarget rg (gFun4Td);     threadtarget rs (&staticfun4td::staticfun);     commfun4td &NBSP;COM;&NBSP;&NBSP;&NBSP;&NBSP;RUNNABLEADAPTER&LT;COMMFUN4TD&GT;&NBSP;RC (Com,&commFun4Td::commFun);     t1.start (Rmy);     thread::sleep (+);     T2.start (RG);     thread::sleep (+);     t3.start (RS);     thread::sleep (+);     t4.start (RC);     t1.join ();     t2.join ();     t3.join ();     t4.join ();     return 0;}





This article from "LINUXQT Jinan high-tech Zone" blog, please be sure to keep this source http://qtlinux.blog.51cto.com/3052744/1698951

POCO C + + routines collation--About threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.