代码之家  ›  专栏  ›  技术社区  ›  pandoragami

类内CreateThread错误:类型为“DWORD(windows_thread::)(void*)”的参数与“DWORD(*)(void*')”不匹配?[副本]

  •  0
  • pandoragami  · 技术社区  · 13 年前

    我试图在类内部创建一个线程,但出现了一个错误。我只是想知道是否有办法解决这个问题,或者我必须在 int main() 只有

    C:\windows_thread_2\windows_thread_2.cpp||In member function 'void windows_thread::thread()':|
    C:\windows_thread_2\windows_thread_2.cpp|24|error: argument of type 'DWORD (windows_thread::)(void*)' does not match 'DWORD (*)(void*)'|
    C:\windows_thread_2\windows_thread_2.cpp||In member function 'DWORD windows_thread::Thread_no_1(void*)':|
    C:\windows_thread_2\windows_thread_2.cpp|70|warning: deprecated conversion from string constant to 'char*'|
    ||=== Build finished: 1 errors, 1 warnings ===|
    

    密码在这里。

    #include <windows.h>
    //#include <strsafe.h>
    #include <stdio.h>
    
    #define BUF_SIZE 255
    
    class windows_thread
    {
    //-------------------------------------------------------------------
    // A function to Display the message indicating in which tread we are
    //-------------------------------------------------------------------
        public:
    
            windows_thread(){}
            ~windows_thread(){}
            void thread()
            {
                int Data_Of_Thread_1 = 1;            // Data of Thread 1
                HANDLE Handle_Of_Thread_1 = 0;       // variable to hold handle of Thread 1
    
                HANDLE Array_Of_Thread_Handles[1];   // Aray to store thread handles
    
        // Create thread 1.
                Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
                if ( Handle_Of_Thread_1 == NULL)  ExitProcess(Data_Of_Thread_1);
    
        // Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
                Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
    
    
        // Wait until all threads have terminated.
                WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
    
                printf("Since All threads executed lets close their handles \n");
    
        // Close all thread handles upon completion.
                CloseHandle(Handle_Of_Thread_1);
            }
            void DisplayMessage (HANDLE hScreen, char *ThreadName, int Data, int Count)
            {
                TCHAR msgBuf[BUF_SIZE];
                size_t cchStringSize;
                DWORD dwChars;
    
        // Print message using thread-safe functions.
        //StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Executing iteration %02d of %s having data = %02d \n"), Count, ThreadName, Data);
        //StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
                WriteConsole(hScreen, msgBuf, cchStringSize, &dwChars, NULL);
                Sleep(1000);
            }
    
    //-------------------------------------------
    // A function that represents Thread number 1
    //-------------------------------------------
            DWORD WINAPI Thread_no_1( LPVOID lpParam )
            {
                int     Data = 0;
                int     count = 0;
                HANDLE  hStdout = NULL;
    
        // Get Handle To screen. Else how will we print?
                if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
                    return 1;
    
        // Cast the parameter to the correct data type passed by callee i.e main() in our case.
                Data = *((int*)lpParam);
    
                for (count = 0; count <= 4; count++ )
                {
                    DisplayMessage (hStdout, "Thread_no_1", Data, count);
                }
    
                return 0;
            }
    };
    int main()
    {
        windows_thread threading;
        threading.thread();
    
        return 0;
    }
    

    我根据答案编辑了代码,如果你能解决这个错误,请编辑它。 line 78 error: no matching function for call to 'windows_thread::Thread_no_1()'|

    #include <windows.h>
    //#include <strsafe.h>
    #include <stdio.h>
    
    #define BUF_SIZE 255
    
    class windows_thread
    {
    //-------------------------------------------------------------------
    // A function to Display the message indicating in which tread we are
    //-------------------------------------------------------------------
        public:
    
            windows_thread(){}
            ~windows_thread(){}
            void thread()
            {
                int Data_Of_Thread_1 = 1;            // Data of Thread 1
                HANDLE Handle_Of_Thread_1 = 0;       // variable to hold handle of Thread 1
    
                HANDLE Array_Of_Thread_Handles[1];   // Aray to store thread handles
    
        // Create thread 1.
                Handle_Of_Thread_1 = CreateThread( NULL, 0,  Wrap_Thread_no_1, &Data_Of_Thread_1, 0, NULL);
                if ( Handle_Of_Thread_1 == NULL)  ExitProcess(Data_Of_Thread_1);
    
        // Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
                Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
    
    
        // Wait until all threads have terminated.
                WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
    
                printf("Since All threads executed lets close their handles \n");
    
        // Close all thread handles upon completion.
                CloseHandle(Handle_Of_Thread_1);
            }
            void DisplayMessage (HANDLE hScreen, char *ThreadName, int Data, int Count)
            {
                TCHAR msgBuf[BUF_SIZE];
                size_t cchStringSize;
                DWORD dwChars;
    
        // Print message using thread-safe functions.
        //StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Executing iteration %02d of %s having data = %02d \n"), Count, ThreadName, Data);
        //StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
                WriteConsole(hScreen, msgBuf, cchStringSize, &dwChars, NULL);
                Sleep(1000);
            }
    
    //-------------------------------------------
    // A function that represents Thread number 1
    //-------------------------------------------
            DWORD WINAPI Thread_no_1( LPVOID lpParam )
            {
                int     Data = 0;
                int     count = 0;
                HANDLE  hStdout = NULL;
    
        // Get Handle To screen. Else how will we print?
                if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
                    return 1;
    
        // Cast the parameter to the correct data type passed by callee i.e main() in our case.
                Data = *((int*)lpParam);
    
                for (count = 0; count <= 1; count++ )
                {
                    //DisplayMessage (hStdout, "Thread_no_1", Data, count);
                }
    
                return 0;
            }
            static DWORD WINAPI Wrap_Thread_no_1( LPVOID lpParam )
            {
                windows_thread *self = reinterpret_cast<windows_thread*>(lpParam);
                self->Thread_no_1();
            }
    };
    int main()
    {
        windows_thread threading;
        threading.thread();
    
        return 0;
    }
    
    2 回复  |  直到 13 年前
        1
  •  4
  •   Mats Petersson    13 年前

    线程必须是“普通函数”,而不是对类的非静态成员函数的调用——这需要为“this pointer”添加一个额外的“隐藏”参数。

    由于到目前为止您的代码没有使用任何成员变量[据我所见],因此简单的解决方案是生成 static .

    如果要使用类的成员变量或成员函数,则需要有一个静态函数并传递对象( this )作为线程的参数(的一部分),然后使用 reinterpret_cast<windows_thread*>(arg); 以将其转换回。

    大致如下:

    ... 
        Handle_Of_Thread_1 = CreateThread( NULL, 0, Wrap_Thread_no_1, this, 0, NULL);
    
    ... 
    
        static DWORD WINAPI Wrap_Thread_no_1( LPVOID lpParam )
        {
              windows_thread *self = reinterpret_cast<windows_thread*>(lpParam);
              self->Thread_no_1();
        }
    

    我已经删除了你原来的论点。假设线程每个对象只运行一个线程,那么可以将其作为成员变量。如果需要具有单个参数的多个线程,则需要使传入的参数 struct class reinterpret_cast 并存储 指针作为其中的一部分 结构 [然后可以 windows_thread* 所以不需要打石膏]。

        2
  •  3
  •   Drew Dormann    13 年前

    你正试图通过 pointer-to-member 到线程,这与这里的原始指针不同。它需要一个对象附加到它上。

    Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
    

    制作函数 static 通过更改:

        DWORD WINAPI Thread_no_1( LPVOID lpParam )
    

    对此:

        static DWORD WINAPI Thread_no_1( LPVOID lpParam )
    

    …(正确地)声明该函数不需要 this .

    推荐文章