现在的位置: 首页 > 操作系统 > 正文

为什么类中的线程函数必须要声明静态

2020年02月10日 操作系统 ⁄ 共 2707字 ⁄ 字号 评论关闭

其实类的静态函数就跟全局函数是一个样子的, 只是调用的时候要加下个类修饰符而已。至于为什么不能是非静态成员函数呢, 因为非静态成员函数都会在参数列表中加上一个this指针为为参数, 这样的话你写的线程函数就不符合调用规定了。

比如 DWORD WINAPI ThreadFun(LPVOID); 是非静态的,实际编译后,就会变成

DWORD WINAPI ThreadFun(LPVOID, CMyClass *this);

这个函数就明显不能作为线程的函数了, 因为多了个参数.所以编译就过不了了.

它与设置成全局函数有一个好处:就是不用声明为friend成员即可访问对象的私有成员。

成员变量不用改成static的,你创建线程的时候把对象的“this”指针作为参数传递过去,就可访问了。

#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);// //静态成员函数 public: int start(); virtual void run() = 0; //基类中的虚函数要么实现,要么是纯虚函数(绝对不允许声明不实现,也不纯虚)};

int Thread::start(){ if(pthread_create(&pid,NULL,start_thread,(void *)this) != 0) //´创建一个线程(必须是全局函数) { return -1; } return 0;}

void* Thread::start_thread(void *arg) //静态成员函数只能访问静态变量或静态函数,通过传递this指针进行调用{ Thread *ptr = (Thread *)arg; ptr->run(); //线程的实体是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;}

编译运行:

linuxidc@Ubuntu:~/myProg/pthreadCpp$ g++ main.cpp -lpthreadlinuxidc@ubuntu:~/myProg/pthreadCpp$ ./a.out hello worldlinuxidc@ubuntu:~/myProg/pthreadCpp$

有一个更加好的方法,就是使用boost::thread,结合boost::function,boost::bind很方便的实现类成员函数线程化

1.如何创建简单的线程

#include <boost/thread/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();//等线程结束}

2.如何给线程携带参数

#include <boost/thread/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();//等线程结束}

3.如何线程互斥

#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.如何让类成员函数线程化

#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();}

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2017-02/141167.htm

以上就上有关为什么类中的线程函数必须要声明静态的相关介绍,要了解更多线程函数,为什么类中的线程函数必须要声明静态,编程,Linux编程,Linux Shell,Android,Android教程,JAVA,C语言,Python,HTML5内容请登录学步园。

抱歉!评论已关闭.