zoukankan      html  css  js  c++  java
  • 线程函数中访问成员变量的方法(转)

    用AfxBeginThread启动线程,线程的执行函数有两种定义的方法:

    <!--[if !supportLists]-->1.      <!--[endif]-->全局函数:UINT threadMessageBoxAdapter( LPVOID lParam );

    <!--[if !supportLists]-->2.      <!--[endif]-->静态成员函数:static UINT threadMessageBoxAdapter( LPVOID lParam );


    我们一般的经验是,在线程中使用变量都必须是全局变量,成员变量定义成static,才能在线程函数中访问成员变量。

    这里说明一种在线程函数中访问成员变量的方法:

    这里定义Thread类:

    class Thread

    {

    public:

                  static UINT threadMessageBoxAdapter( LPVOID lParam );

                  UINT threadMessageBoxProc( );

    private:

                  CString strThreadText;

    };

    线程函数的实现:

    UINT Thread::threadMessageBoxAdapter( LPVOID lParam )

    {

                  CTestVectorDlg* obj = ( CTestVectorDlg* )lParam;

                  return obj->threadMessageBoxProc();

    }

    UINT Thread::threadMessageBoxProc()

    {

                  CString strThreadText;

                  strThreadText.Format( _T( "%s" ), _T( "Thread adapter" ) );

                  AfxMessageBox( strThreadText );

                  return 0;

    }

    使用AfxBeginThread启动线程:

                  CWinThread* thread;

                  thread = AfxBeginThread( threadMessageBoxAdapter, this );

    这样,当线程启动后,弹出”Thread Adapter”的信息,说明成功访问成员变量。

  • 相关阅读:
    【一月の飞雪】(小年快乐!)
    【十二月の博雅闻道】(元旦快乐!)
    【十一月の期中考试总结】
    【十月のA Letter to 后辈菌】
    【九月の文化课生活】(国庆快乐!)
    OI回忆录(流水账)
    SDOI 2017 Round2 滚粗了
    【BZOJ 3456】城市规划
    【Vijos 1998】【SDOI 2016】平凡的骰子
    【HDU 3662】3D Convex Hull
  • 原文地址:https://www.cnblogs.com/lebronjames/p/1772406.html
Copyright © 2011-2022 走看看