Why a thread function in a class must declare a static

Source: Internet
Author: User
Tags mutex thread class

In fact, the static function of the class is the same as the global function, but only when the call to add the next class modifier.

As for why it cannot be a non-static member function, because a nonstatic member function adds a this pointer to the parameter list as a parameter, so that the thread function you write does not conform to the calling rule.
such as DWORD WINAPI Threadfun (LPVOID); is non-static and is actually compiled, it becomes
DWORD WINAPI Threadfun (LPVOID, CMyClass *this);
This function is obviously not a function of the thread, because there are a number of parameters. So the compilation will not go through.

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

It has the advantage of setting up a global function: You can access a private member of an object without declaring it as a friend member.
Member variables are not changed to static, and when you create a thread, the "This" pointer of the object is passed as a parameter and is accessible.

#include <pthread.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>classthread{Private: pthread_t pid; Private:        Static void* Start_thread (void*ARG);// //Static member functions     Public:         intstart (); Virtual voidRun () =0;//A virtual function in a base class is either implemented, or is a pure virtual function (it is not allowed to declare without implementation, nor pure virtual)};intThread::start () {if(Pthread_create (&pid,null,start_thread, (void*) This) !=0)//the creation of 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, which are called by passing the this pointer{Thread*ptr = (Thread *) Arg; PTR->run ();//the entity of the thread is run}classMyThread: Publicthread{ Public:         voidrun ();};voidMythread::run () {printf ("Hello world\n");}intMainintargcChar*argv[])    {MyThread MyThread;    Mythread.start (); //Test.run ();Sleep1); return 0;}

Compile run:

[Email protected]:~/myprog/pthreadcpp$ g++ main.cpp-lpthread[email protected]:~/myprog/pthreadcpp$./a . Out   

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

A better approach is to use Boost::thread, which combines boost::function,boost::bind to implement class member function lines conveniently threaded

1. How to create a simple thread

#include <boost/thread/thread.hpp><iostream>usingnamespace   void  HelloWorld () {    "helloWorld" <<int _ Tmain (int argc, _tchar* argv[]) {    boost::thread THD (&HelloWorld);    Thd.join (); // wait for thread to end }

2. How to carry parameters to a thread

#include <boost/thread/thread.hpp>#include<boost/bind.hpp>#include<string>#include<iostream>using namespacestd;voidHelloWorldstringPAR1,intPAR2,Doublepar3) {cout<<"Hello World"<<Endl; cout<< par1 <<","<< par2 <<","<< Par3 <<Endl;} int_tmain (intARGC, _tchar*argv[]) {Boost::thread THD (Boost::bind (&helloworld,string("haha"),1,1.0)); Thd.join ();//wait for thread to end}

3. How to Thread Mutex

#include <boost/thread/thread.hpp>#include<boost/thread/mutex.hpp>#include<iostream>using namespacestd; boost::mutex mutex_;intIo_;voidthread_in () {Boost::mutex::scoped_lockLock(MUTEX_); cout<<"inch"<< io_++ <<Endl;}voidthread_out () {Boost::mutex::scoped_lockLock(MUTEX_); cout<<" out"<< io_--<<Endl;}int_tmain (intARGC, _tchar*argv[]) {Boost::thread THD1 (&thread_in); Boost::thread THD2 (&thread_out);    Thd1.join ();    Thd2.join (); Io_=0; }

4. How to Thread class member functions

#include <boost/thread/thread.hpp>#include<string>#include<iostream>using namespacestd;classmyclass{ Public: MyClass (): Classname_ ("I am Hero")    {     }protected:    stringclassname_;protected:    voidHandle_thread () {cout<< classname_ <<Endl; }}; int_tmain (intARGC, _tchar*argv[])    {MyClass C; Boost::thread THD (Boost::bind (&myclass::handle_thread,&c)); Thd.join ();}

Why a thread function in a class must declare a static

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.