0

クラス内にスレッドを作成しようとしましたが、エラーが発生しました。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;
}
4

2 に答える 2

4

スレッドは、クラスの非静的メンバー関数への呼び出しではなく、「プレーン関数」でなければなりません。これらには、「this-pointer」に追加の「隠し」引数が必要です。

これまでのコードでは [私が見る限り] メンバー変数を使用していないため、簡単な解決策は関数を作成することstaticです。

クラスのメンバー変数またはメンバー関数を使用する場合は、静的関数を用意し、オブジェクト ( this) を引数 (の一部) としてスレッドに渡し、 a を使用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();
    }

私はあなたの元の主張を削除しました。スレッドがオブジェクトごとに 1 つのスレッドのみを実行していると仮定すると、それをメンバー変数にすることができます。個々のパラメーターを持つ複数のスレッドが必要な場合は、渡されたパラメーターを astructまたはclassにし、そのポインターをそのorの一部としてreinterpret_cast格納する必要があります [これはキャストを必要としません]。thisstructclasswindows_thread*

于 2013-03-04T00:29:09.363 に答える
3

メンバーへのポインターをスレッドに渡そうとしていますが、これは生のポインターとは異なります。オブジェクトをアタッチする必要があります。

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

于 2013-03-04T00:26:58.180 に答える