首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在类函数中将参数传递给_beginthread

在类函数中将参数传递给_beginthread
EN

Stack Overflow用户
提问于 2013-04-06 09:00:20
回答 1查看 1.7K关注 0票数 1

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

代码语言:javascript
复制
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();
    }
};

我一直在将这个模型转换成一个接受线程函数参数的模型时遇到了麻烦。例如,如果我希望我的线程函数改为:

代码语言:javascript
复制
void fun_2(int a)

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

EN

回答 1

Stack Overflow用户

发布于 2018-08-28 22:21:13

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

示例:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15845865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档