线程的入口函数总结

本文介绍了C++中四种不同的线程入口函数实现方式:全局函数、友元函数、静态成员函数及普通成员函数,并详细解释了每种方法的特点及适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/aguiwang/article/details/6937243

线程的入口函数种类大致如下:

 

在C程序当中线程的入口函数就是全局函数,这种方式也可以在C++里面继续延用。在C++里面还有其它形式的函数可以作为线程的入口函数,相对而且比C的全局函数更符合封装的思想。下面我们一一介绍:

 

一、全局函数作为线程的入口函数:可以通过传递对象的this指针来实现访问对象的public成员

[cpp]  view plain copy
  1. unsigned ThreadFunc(void* lparam);)//全局函数  
  2.   
  3.   
  4. class CThread  
  5. {  
  6. public:  
  7.     CThread(int i,int y):m_i(i),m_y(y)  
  8.     {  
  9.     };  
  10.     void StartThread();  
  11.     m_y;  
  12. protected:  
  13.     int m_i;  
  14. };  
  15.   
  16. void CThread::StartThread()  
  17. {  
  18.    AfxBeginThread(ThreadFunc,this);  
  19. }  
  20.   
  21. unsigned ThreadFunc(void* lparam)//全局函数  
  22. {  
  23.    CThread* pthr = (CThread*)lparam;  
  24.     for(int i = 0;i<10;i++)  
  25.    {  
  26.        std::cout<<i+pthr->m_i<<std::endl;//访问公有有成员  
  27.       //std::cout<<i+pthr->m_y<<std::endl;//不能访问私有成员  
  28.        Sleep(100);  
  29.    }  
  30.    return 0;  
  31. };  
  32.   
  33. int main(int argc, char * argv[])  
  34. {  
  35.    CThread thread(1,2);  
  36.    thread.StartThread();  
  37.    return 0;  
  38. }  

 

二、友元函数作为线程的入口函数:它的调用和传递机制跟C的全局函数是一样的。

[cpp]  view plain copy
  1. class CThread  
  2. {  
  3. public:  
  4.     CThread(int i,int y):m_i(i),m_y(y)  
  5.     {  
  6.     };  
  7.     void StartThread();  
  8.     friend unsigned ThreadFunc(void* lparam);  
  9.     m_y;  
  10. protected:  
  11.     int m_i;  
  12. };  
  13.   
  14. void CThread::StartThread()  
  15. {  
  16.    AfxBeginThread(ThreadFunc,this);  
  17. }  
  18.   
  19. unsigned ThreadFunc(void* lparam)  
  20. {  
  21.    CThread* pthr = (CThread*)lparam;  
  22.     for(int i = 0;i<10;i++)  
  23.    {  
  24.        std::cout<<i+pthr->m_i<<std::endl;  
  25.        std::cout<<i+pthr->m_y<<std::endl;//访问私有成员  
  26.        Sleep(100);  
  27.    }  
  28.    return 0;  
  29. };  
  30.   
  31. int main(int argc, char * argv[])  
  32. {  
  33.    CThread thread(1,2);  
  34.    thread.StartThread();  
  35.    return 0;  
  36. }  

友元函数比全局函数更加高效而强悍(能访问类的所有成员),但是以破坏类的封装性为代价的。

 

三、静态成员函数作为线程的成员函数:它对外可以是public也可以是private,它的优点就是没有破坏类的封装性。但它只能访问类的静态成员,当然这点可以通过给它的参数传递对象的this指针来克服,但这样也不能访问对象的public以外的成员。

[cpp]  view plain copy
  1. class CThread  
  2. {  
  3. public:  
  4.     CThread(int i,int y):m_i(i),m_y(y)  
  5.     {  
  6.     };  
  7.     void StartThread();  
  8.     static unsigned ThreadFunc(void* lparam);  
  9.     m_y;  
  10. protected:  
  11.     int m_i;  
  12. };  
  13.   
  14. void CThread::StartThread()  
  15. {  
  16.    AfxBeginThread(ThreadFunc,this);  
  17. }  
  18.   
  19. unsigned CThread::ThreadFunc(void* lparam)  
  20. {  
  21.    CThread* pthr = (CThread*)lparam;  
  22.     for(int i = 0;i<10;i++)  
  23.    {  
  24.        std::cout<<i+pthr->m_i<<std::endl;  
  25.      //std::cout<<i+pthr->m_y<<std::endl;//不能访问私有成员  
  26.        Sleep(100);  
  27.    }  
  28.    return 0;  
  29. };  
  30.   
  31. int main(int argc, char * argv[])  
  32. {  
  33.    CThread thread(1,2);  
  34.    thread.StartThread();  
  35.    return 0;  
  36. }  

 

四、普通成员函数作为线程入口函数:这种方式能突破上述方法的局限,即不破坏类的封装性,能访问对象的所以成员,可以是虚函数,能实现多态

class CThread

{
public:
    DWORD WINAPI ThreadFunc(LPVOID param);
    ...
};
有如上类,在类中某函数想以函数ThreadFunc最为线程入口函数:

CreateThread(NULL, 0, WorkThread, &param, 0, &ThreadID))


编译会报错:cannot convert parameter 3 from unsigned long (void *) to unsigned long (__stdcall *)(void *)        None of the functions with this name in scope match the target type

怎么解决这个问题,详解:http://blog.youkuaiyun.com/aguiwang/article/details/6941596

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值