Why must the thread functions declare static and thread functions declare static?

Source: Internet
Author: User

Why must the thread functions declare static and thread functions declare static?

In fact, static functions of the class look like global functions, but the next class modifier must be added during the call.

As for why not non-static member functions, because non-static member functions always add a "this" pointer to the parameter list, so that the thread function you write does not comply with the call rules.
For example, dword winapi ThreadFun (LPVOID); is non-static and will become
Dword winapi ThreadFun (LPVOID, CMyClass * this );
This function obviously cannot be used as a thread function, because there are more parameters, so compilation will fail.

 

Reference: http://www.cnblogs.com/diegodu/p/4655036.html

 

It has the advantage of setting it as a global function: You can access the private members of an object without declaring it as a friend member.
The member variables do not need to be changed to static. When you create a thread, you can pass the "this" pointer of the object as a parameter to access it.

# Include <pthread. h> # include <unistd. h> # include <stdio. h> # include <stdlib. h> class Thread {private: pthread_t pid; private: static void * start_thread (void * arg); // static member function public: int start (); virtual void run () = 0; // The virtual function in the base class is either implemented or pure virtual function (it cannot be declared or not implemented)}; int Thread:: start () {if (pthread_create (& pid, NULL, start_thread, (void *) this )! = 0) // 'create a Thread (must be a global function) {return-1;} return 0;} void * Thread: start_thread (void * arg) // static member functions can only access static variables or static functions. By passing the this pointer, call {Thread * ptr = (Thread *) arg; ptr-> run (); // The Thread entity is run} class MyThread: public Thread {public: void run () ;}; void MyThread: run () {printf ("hello world \ n");} int main (int argc, char * argv []) {MyThread myThread; myThread. start (); // test. run (); sleep (1); return 0 ;}

Compile and run:

diego@ubuntu:~/myProg/pthreadCpp$ g++ main.cpp -lpthreaddiego@ubuntu:~/myProg/pthreadCpp$ ./a.out hello worlddiego@ubuntu:~/myProg/pthreadCpp$ 

 

Reference: http://bbs.csdn.net/topics/280040692

There is a better method, that is, using boost: thread, combined with boost: function, boost: bind, It is very convenient to implement the class member function thread.

1. How to create a simple thread

# Include <boost/thread. hpp> # include <iostream> using namespace std; void helloworld () {cout <"hello world" <endl;} int _ tmain (int argc, _ TCHAR * argv []) {boost: thread thd (& helloworld); thd. join (); // wait for the thread to end}

2. How to attach parameters to a thread

# Include <boost/thread. hpp> # include <boost/bind. hpp >#include <string >#include <iostream> using namespace std; void helloworld (string par1, int par2, double par3) {cout <"hello world" <endl; cout <par1 <"," <par2 <"," <par3 <endl;} int _ tmain (int argc, _ TCHAR * argv []) {boost: thread thd (boost: bind (& helloworld, string ("haha"), 1, 1.0); thd. join (); // wait for the thread to end}

3. Thread mutex

#include <boost/thread/thread.hpp>#include <boost/thread/mutex.hpp>#include <iostream>using namespace std; boost::mutex mutex_;int io_;void thread_in(){    boost::mutex::scoped_lock lock(mutex_);    cout << "in " << io_++ << endl;}void thread_out(){    boost::mutex::scoped_lock lock(mutex_);    cout << "out " << io_-- << endl;}int _tmain(int argc, _TCHAR* argv[]){    boost::thread thd1(&thread_in);    boost::thread thd2(&thread_out);    thd1.join();    thd2.join();    io_ = 0; }

4. How to thread the class member functions

 

#include <boost/thread/thread.hpp>#include <string>#include <iostream>using namespace std; class myclass{public:    myclass():      classname_("I am Hero")    {     }protected:    string classname_;protected:    void handle_thread()    {        cout << classname_ << endl;    }}; int _tmain(int argc, _TCHAR* argv[]){    myclass c;    boost::thread thd(boost::bind(&myclass::handle_thread,&c));    thd.join();}

 

 

 

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.