1

我一直在我的(Windows)C++ 类中使用 _beginthread 来创建线程。据我所知,要使用 _beginthread 并在类中传递成员函数,必须遵循一个相当迟钝的包装协议,例如:

class MyClass
{
public:
    void fun_1()
    {  
        _beginthread(&MyClass::fun_2_wrapper, 0, static_cast<void*>(this));
    }

private:
    void fun_2()
    {
        printf("hello");  
    }  

    static void __cdecl fun_2_wrapper(void* o)
    {
        static_cast<MyClass*>(o)->fun_2();
    }
};

我一直无法将此模型转换为接受线程函数参数的模型。例如,如果我希望我的线程函数改为:

void fun_2(int a)

我将如何正确调整我的函数调用?(注意,我试图在没有 boost::thread 或类似库的情况下完成此项目以保持项目一致性)

4

1 回答 1

0

您可以包装将传递给结构的参数,然后将结构传递给函数。

例子:

#include <Windows.h>
#include "stdio.h"
#include "stdlib.h"
#include <process.h>

class TestWrapperMultThreadToClass
{
public:
    TestWrapperMultThreadToClass(int ThreadNum)
    {
        _ThreadNum = ThreadNum; // how many thread we would create
        TestTimes = ThreadNum * 2;
    }

    ~TestWrapperMultThreadToClass()
    {

    }

    bool TestMultThreadSendParam()
    {
        tb = new ThreadBlockParam[_ThreadNum];
        for (int i = 0; i < _ThreadNum; i++) // initial
            (tb + i)->IsIdle = true;

        int ThreadIndex = 0, nowThread = 0;
        unsigned int ThreadId_For_Beginthreadex_Function = 0;
        for (int i = 0; i < TestTimes; i++)
        {
            ThreadId_For_Beginthreadex_Function = static_cast<unsigned int>(i);
            nowThread = i;

            // wrap arguments
            (tb + nowThread)->ThreadIndex = ThreadId_For_Beginthreadex_Function;
            (tb + nowThread)->InIterativeTimes = i;
            (tb + nowThread)->IsIdle = false;
            (tb + nowThread)->_wrap = (void*) this;
            _beginthreadex(NULL, 0, FunctionCalledByThread, (tb + nowThread), 0, &ThreadId_For_Beginthreadex_Function);
        }

        IsAllThreadComeBack(tb, _ThreadNum); // check whether all threads come back

        return true;
    }

private:
    int _ThreadNum, TestTimes;

    typedef struct _ThreadBlockParam
    {
        int ThreadIndex;
        int InIterativeTimes;
        bool IsIdle;
        void* _wrap;
    }ThreadBlockParam;
    ThreadBlockParam *tb;

    static unsigned int __stdcall FunctionCalledByThread(void *ptr_this)
    {
        ThreadBlockParam *_tb = static_cast<ThreadBlockParam*>(ptr_this);
        TestWrapperMultThreadToClass *_this = (TestWrapperMultThreadToClass*)(_tb->_wrap);

        _this->Test(_tb);
        return 1;
    }

    void Test(ThreadBlockParam* _tb)
    {
        ThreadBlockParam *temp = _tb;
        int ThreadId = GetCurrentThreadId();
        char Msg[200];

        Sleep(1000);
        sprintf(Msg, "In %d-th thread, thread id = %d!\n", temp->ThreadIndex, ThreadId);
        printf(Msg);
        Sleep(5000); // wait few second to check whether execute with different thread.

        temp->IsIdle = true;
    }

    bool IsAllThreadComeBack(ThreadBlockParam *tb, int ThreadNum)
    {
        int count = 0;

        while (count < ThreadNum)
        {
            if ((tb + count)->IsIdle == true)
                count++;
        }
        return true;
    }

};

int main()
{
    TestWrapperMultThreadToClass *Test = new TestWrapperMultThreadToClass(8);
    Test->TestMultThreadSendParam();

    printf("Multiple thread wrapper to class testing is finishing!\n");

    system("pause");
    return 0;
}
于 2018-08-28T14:21:12.620 回答