线程函数问题

If you are rusty on your C++, let me remind you of the problem. Every C++ member function has a hidden first passed parameter known as the this parameter.Via the this parameter, the function knows which instance of the class to operate upon. Because you never see these this parameters, it is easy to forget they exist.Now, let‘s again consider the _beginthread() function which allows us to specify an arbitrary entry-point-function for our new thread.This entry-point-function must accept a single void* passed param.Aye, there‘s the rub. The function signature required by _beginthread() does not allow the hidden this parameter, and hence a C++ member function cannot be directly activated by _beginthread().意思就是说,C++类中的成员函数其实默认在参数中包含有一个this指针,这样成员函数才知道应该对哪个实例作用。而线程函数必须接受一个void指针作为参数,所以导致了矛盾。为了解决矛盾,我们可以使用static函数,它独立于实例,参数中将不会有this指针,所以可以用于打开线程
时间: 2024-08-24 02:41:38

线程函数问题的相关文章

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

其实类的静态函数就跟全局函数是一个样子的, 只是调用的时候要加下个类修饰符而已. 至于为什么不能是非静态成员函数呢, 因为非静态成员函数都会在参数列表中加上一个this指针为为参数, 这样的话你写的线程函数就不符合调用规定了.比如 DWORD WINAPI ThreadFun(LPVOID); 是非静态的,实际编译后,就会变成DWORD WINAPI ThreadFun(LPVOID, CMyClass *this); 这个函数就明显不能作为线程的函数了, 因为多了个参数.所以编译就过不了了.

管理线程之向线程函数传递參数

向线程函数传递參数在构造线程对象时就可以完毕.可是要记住,默认情况下是把參数复制到线程内部,即使在函数中使用的是引用.比如 void f(int i,std::string const &s); std::thread t(f,3,"hello"); 上面代码中,函数f的第二个參数是std::string,传递的是char const *会转换为string. 当使用指针指向自己主动变量时.要特别注意: void f(int i, std::string const&

【C++】【MFC】创建新的线程函数

DWORD WINAPI MyThreadProc (LPVOID lpParam){ somestruct* pN = (somestruct*)lpParam; // 将参数转为你的类型 ... return 0;} 创建命令以及各个参数说明:HANDLE hThread = CreateThread( NULL, // 没有安全描述符 0, // 默认线程栈的大小 MyThreadProc, // 线程函数指针 (LPVOID)&n, // 传递参数 NULL, // 没有附加属性 NUL

[笔记]linux下和windows下的 创建线程函数

linux下和windows下的 创建线程函数 1 #ifdef __GNUC__ 2 //Linux 3 #include <pthread.h> 4 #define CreateThreadEx(tid,threadFun,args) pthread_create(tid, 0, threadFun, args) 5 #define CloseHandle(ph) 6 7 int pthread_create( 8 //指向线程标识符的指针. 9 pthread_t *restrict t

类的非静态成员函数作为线程函数的注意事项

代码 #include <string> #include <boost/thread/thread.hpp> #include <boost/bind.hpp> #include <boost/function/function0.hpp> class CThreadClass { public: CThreadClass() { m_stop = true; } void StartThread() { boost::function0<void&

向线程函数传递参数

向线程函数传递参数在构造线程对象时即可完成.但是要记住,默认情况下是把参数拷贝到线程内部,即使在函数中使用的是引用.例如 void f(int i,std::string const &s); std::thread t(f,3,"hello");</span> 上面代码中,函数f的第二个参数是std::string,传递的是char const *会转换为string. 当使用指针指向自动变量时,要特别注意: <span style="font-s

在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static

在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create创建线程,线程函数是一个全局函数,所以在C++中,创建线程时,也应该使用一个全局函数.static定义的类的成员函数就是一个全局函数. 更多 参考  http://blog.csdn.net/ksn13/article/details/40538083 #include <pthread.h> #

创建线程函数的方法

1.线程函数 在启动一个线程之前,必须为线程编写一个全局的线程函数,这个线程函数接受一个32位的LPVOID作为参数,返回一个UINT,线程函数的结构为: UINT ThreadFunction(LPVOID pParam) { //线程处理代码 return0; } 在线程处理代码部分通常包括一个死循环,该循环中先等待某事情的发生,再处理相关的工作: while(1) { WaitForSingleObject(-,-);//或WaitForMultipleObjects(-) //Do so

PYTHON——多线程:Thread类与线程函数

Thread类与线程函数 可以使用Thread对象的join方法等待线程执行完毕:主线程(main()函数)中调用Thread对象的join方法,并且Thread对象的线程函数没有执行完毕,主线程会处于阻塞状态.使用Thread类实现多线程的步骤:1.创建Thread类的实例:2.通过Thread类的构造方法的target关键字参数执行线程函数:通过args关键字参数指定传给线程函数的参数.3.调用Thread对象的start方法启动线程.下面例子功能:使用Thread对象启动2个线程,并在各自