How to deal with multiple parameters of different types in C++98?

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:39:19

问题


In order to implement a thread class(In C++98 and Windows.h). I have something like this:

Thread::Thread(_beginthreadex_proc_type fn)
{
    m_raw = fn;
    m_args = 0;
    m_handle = 0;
    m_id = 0;
}

The code above works fine it takes a function that not receive parameters, and with the next code it function is called by a new thread:

void Thread::Join()
{
    m_handle = (HANDLE)_beginthreadex(0, 0, m_raw, (m_args ? m_args : 0), 0, 0);
    if (m_handle) WaitForSingleObject(m_handle, INFINITE);
}

This code also works fine with functions that don't take any parameter. Now my question is about how can i in C++98 receive variadic parameters in my constructor and save them. And NO i can't use modern c++ if that was the case I din't need help. So plz don't give me solutions implemented with c++11 or higher.

Update

Now I'm trying a Java style solution in that every Thread is a IRunnable that have a pure virtual function named Run. And thread is almost the that this implementetation with the diff that is an abstract class. In this way can i avoid parameters because I don't pass the function instead of that I write another class that inherits from Thread and implements Run. The code look like:

The interface

struct IRunnable
{
    virtual void Run() = 0;
};

Thread class

class Thread : public IRunnable
{
    HANDLE m_handle;
    DWORD  m_id;
    typedef unsigned (__stdcall *Function)(void*);
    _beginthreadex_proc_type m_raw;
    void* m_args;
public:
    Thread();
    ~Thread();
    Thread(_beginthreadex_proc_type, void*);
    Thread(_beginthreadex_proc_type);
    unsigned GetId();
    virtual void Run() = 0;
    void Join();
    unsigned int __stdcall call(void*);
};

Call only is a wrapper to call Run function member

unsigned int __stdcall Thread::call(void* data)
{
    Run();
    return 0;
}

My problem is here:

void Thread::Join()
{
    m_handle = (HANDLE)_beginthreadex(0, 0, &this->call, 0, 0, 0);
    if (m_handle) WaitForSingleObject(m_handle, INFINITE);
}

When i compiling in vs2019 the code above produce the next error:

error C2276: '&': illegal operation on bound member function expression

error C2660: '_beginthreadex': function does not take 5 arguments


回答1:


If you look at pretty much any thread library, they very rarely support sending multiple arguments; you usually send a pointer to something, and if you want many things, you make a struct containing many things and send a pointer to it.

However, if you really want this, you could use the C varargs functions to iterate over all variadic arguments, and allocate a linked list with them, or allocate an array of them, or whatever other data structure you want. Then, send a pointer to that to your thread entry function. Your function would still be taking just one pointer, though.

In C, there is no easy way to construct a va_list, which is how variadic arguments are sent around. You can't just send the va_list you have on your main thread, because that memory won't be alive by the time it reaches your new thread. There is also no good way to expand a va_list to fill function arguments.

Btw, I realize you're using C++, but as far as C++98 goes, its varargs support is basically the same as in C, which is why I'm mentioning C in my answer.




回答2:


For your edited question, the reason you're getting a compile error is because you're trying to send the address to a member function of your Thread object. You can't take pointers to member functions and use them without also keeping the object pointer around. Instead, you should make a global function that takes a Thread* as its argument, send a pointer to that function, and let it call your runnable.

unsigned thread_entry(void* thread_ptr)
{
    Thread* thread = (Thread*) thread_ptr;
    return thread->call();
}

void Thread::Join()
{
    m_handle = (HANDLE)_beginthreadex(0, 0, thread_entry, this, 0, 0);
    if (m_handle) WaitForSingleObject(m_handle, INFINITE);
}

P.S. It's usually best to ask new questions instead of editing old ones if the question is significantly different, which yours is. Also, remember to upvote and accept helpful answers.



来源:https://stackoverflow.com/questions/58018730/how-to-deal-with-multiple-parameters-of-different-types-in-c98

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!