Pass a few arguments to CreateThread

Question:

  • How to pass a special two argument the CreateThread , when

    • Argument one, type SOCKET
    • Argument Two, Interface Pointer :

    _COM_SMARTPTR_TYPEDEF(Range, __uuidof(Range));

    RangePtr pRange; //pass pRange

Suggestions:

+3
source share
4 answers

create a structure of these two types and pass a pointer to it. This is the standard way to transfer data to streams using a single pointer.

+13
source

, struct_thread_xyz_params, boost:: thread, . , -, CreateThread :

template <class Func>
class Thread
{
    Func m_Func;
    static DWORD WINAPI ThreadFunc(void* param)
    {
        Thread& pFunc = *(Thread*)param;
        pFunc();
        return S_OK;
    }
public:
    Thread(Func& func): m_Func(func){
        CreateThread(NULL,NULL,Thread::ThreadFunc,this,NULL,NULL);
    };
    void operator()()
    {
        m_Func();
    }
};

, :

void printTwoStrings(string a, string b)
{
    cout << a << " " << b << endl;
};

:

class StringFunc
{
    string m_a;
    string m_b;
public:
    StringFunc(string a, string b):m_a(a),m_b(b)
    {

    };
    void operator()(){
        printTwoStrings(m_a,m_b);
    }
};

:

int main()
{

    Thread<StringFunc> myThread(StringFunc("hello","world"));
    Sleep(500);
    return 0;
}

, , ... struct_xyz_params , struct_wxyz_params, ...

boost:: thread ( ).

-Rick

+2

std::pair - .

+1

you can also send WM_COPYDATA by filling out COPYDATASTRUCT at a later point after starting the stream (ref: http://msdn.microsoft.com/en-us/library/ms649011(VS.85).aspx )

0
source

Source: https://habr.com/ru/post/1703656/


All Articles