Object-oriented and object-based threading encapsulation methods in C + +

Source: Internet
Author: User


Original link: http://blog.chinaunix.net/uid-26611383-id-4273854.html


At the beginning of the article I would like to say a few complaints, but also as a summary of the past few months, has not been to update the blog for several months, this period happened a lot of things, change jobs, contact with new projects, these months work pressure some big, now is completed a small project the first stage, in this period or learn a lot of things. The understanding of C + + increased a lot, before very little use, although very much like, but only the emphasis on the theory, C + + is really profound, now also just understand a fur. I am very pleased with C + +, Linux is very interested in the current work content is still more satisfied. From the 500-strong foreign companies to leave without hesitation, pay a salary to a start-up company is still worthwhile, I think people should first know what they want, what they like. Here I want to say to my friends who have not yet graduated or are about to graduate, do not care about the company's aura, others feel good is not necessarily for you, do not care about the beginning than the students to take that hundreds of thousands of dollars, when you feel that all day work is just a tool to do their own annoying things, that feeling is not better than life dead. The jobs that get you to exercise and grow fast and that you love are the ones that really work for you, and in a few years you'll find that hundreds of thousands of dollars is too small. This period of time read a lot of C + + code, also wrote a small project, this article to say I have seen the comparison of two types of multithreaded packaging, to achieve a platform for Linux
The first thing to say is that a thread encapsulates a way also we usually see the most of a package, is the object-oriented inheritance, polymorphism to achieve, the following look at the specific code, these code so that I write, mainly to illustrate the idea, if you want to use the project needs to be improved.

    /************************************************************************* > File Name:Thread.cpp > Author:kevinfu > Mail:kevinfu1985@gmail.com > Created time:2014 year May 26 Monday 21:27 51 seconds * /#include <iostream> #include <pt

    Hread.h> using namespace std;
        Class Thread {public:thread (string name = "Unknown") {} virtual ~thread ()
        {} void Start () {pthread_create (&m_threadid, NULL, ThreadFunc, this);
            Static void* ThreadFunc (void* PTH) {thread* p = static_cast<thread*> (PTH);
        P->run ();

    virtual void Run () = 0;
    private:pthread_t M_threadid;


    };
     Class Test:public Thread {public:virtual void Run () {while (1)       {cout<< "in Test::run ()" <<endl;
    }
        }
    };
        int main () {thread* thread1 = new Test ();
        Thread1->start ();
    Sleep (1);

 }

Here we use this class thread to implement the threading package, the user himself to enable the thread class to inherit this thread class, in the thread class start function, called Pthread_create created a thread, and threadfunc set to the thread function , the thread's this pointer is passed to the function, which in this thread function calls the
Virtual function run, the run function will eventually use the polymorphic call to the user thread class run function, which is the basic principle of the above code, relatively simple, this is the object-oriented use of a lot of places, can be said to be his strong place, but it is more awkward to use, Our company's thread encapsulation is used in this way, personal feel very troublesome, not easy to use, users want to enable a thread, must inherit thread this class, and to overwrite run this virtual function, very troublesome.

The following thread encapsulation method is the object based encapsulation, the package is object-oriented, this way we use the Boost library artifact Boost::function, Boost::bind, this artifact principle is to help set a Function object, function objects can contain parameters , we can use this artifact to invoke any global function, even the member function of a class,
Instead of inheriting from any class, look at the specific code below
    /************************************************************************* > File Name:Thread.cpp > Author:kevinfu > Mail:kevinfu1985@gmail.com > Created time:2014 year May 26 Monday 20:41 07 seconds * /#include <iostream> #include <pt

    hread.h> #include <boost/function.hpp> #include <boost/bind.hpp> using namespace std;
    Class Thread {typedef boost::function<void (void) > Threadfunctioncallback;
        Public:thread (Threadfunctioncallback cb, String name = "Unknow"): M_CB (CB) {} ~thread () {} void Start (void) {pthread_create (&m_threadid, NULL, THR
        Eadfunction, this);
            Static void* threadfunction (void* obj) {thread* Thread = static_cast<thread*> (obj); Thread->m_CB ();
        } Private:threadfunctioncallback M_CB;
    pthread_t M_threadid;

    };
        Class Test {public:void run (void) {cout<< "in Test::run ()" <<endl;

    }
    };
        int main () {Test T;
        Thread Thread1 (Boost::bind (&test::run, &t)); Thread1.
        Start ();
    Usleep (100);

 }

We can see that the test class does not need to inherit from the thread class, and we can directly pass the Boost::bind-defined function object to the thread's constructor, where we pass the this pointer to the THREADFUNC for simplicity. We can pass any arguments to ThreadFunc, because we don't need to use polymorphism, which is object-based thinking that seems more straightforward than object-oriented, easier to use, simpler.

c++11 in the TR1 has included boost these two functions, boost is really representing the highest level of C + + library, there are many things worth learning, there are many things will be added to the standard C + + library.



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.