echo
echo

Reputation: 799

Efficiently passing parameters to function in another thread in C++

As far as I can see, a pointer to structure can be used. But I am wondering, is there any more efficient or elegant way to do that? At least when a structure is being used, it is not easy to see what are the parameters used by the function.

Thanks for any insightful answer.

Upvotes: 1

Views: 6633

Answers (2)

Ratnesh
Ratnesh

Reputation: 1700

Here is a small example if you want to pass a single parameter to a thread function in Win32 API

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

DWORD WINAPI funHello(void *x)
{
    int c = (int*)x;
    printf("\n Thread No: %d\n",c);
    // Do some work , call some function, etc.
    return 0;
}

int main()
{
    HANDLE  myhandle;
    DWORD threadId;
    int c = 1;
    myhandle = CreateThread(NULL, 0, funHello, (void *)c, 0, &threadId);
    if (myhandle == NULL)
    {
        printf("Create Thread Failed. Error no: %d\n", GetLastError);
    }
    WaitForSingleObject(myhandle, INFINITE);
    printf("\n Main Hello...\n");
    CloseHandle(myhandle);
    return 0;
}

Upvotes: 1

LihO
LihO

Reputation: 42133

Here's a small example, that uses WIN32 API:

#include <windows.h>
#include <stdio.h>

struct PARAMS
{
    int i;
    char* msg;
};

DWORD WINAPI myThread(void* parameter)
{
    PARAMS* params = (PARAMS*)parameter;
    printf("Received parameters: i = %d, msg = '%s'\n", params->i, params->msg);
    return 0;
}

int main(int argc, char* argv[])
{
    char msg[] = "Hi there.";
    PARAMS params;
    params.i = 1;
    params.msg = msg;

    HANDLE threadHandle = CreateThread(NULL, 0, myThread, &params, 0, NULL);
    WaitForSingleObject(threadHandle, INFINITE);

    return 0;
}

You say, that "it is not easy to see what are the parameters used by the function". Well it depends on situation. If you don't consider it "elegant" enough, you should leave some helpful comment there at least... if you are using good naming and trying to write code, that is self-documenting, then using of structure will be just fine.

Here's an example of wrapping CreateThread so that programmer that uses your code doesn't have to know that you are using some structure:

#include <windows.h>
#include <stdio.h>

class MyWrapper
{
private:
    struct PARAMS
    {
        int i;
        char* msg;
    };

    static DWORD WINAPI myThread(void* parameter)
    {
        PARAMS* params = (PARAMS*)parameter;
        printf("Received parameters: i = %d, msg = '%s'\n", params->i, params->msg);
        delete params;
        return 0;
    }

public:
    HANDLE createThread(int i, char* msg)
    {
        PARAMS* params = new PARAMS;
        params->i = i;
        params->msg = msg;

        return CreateThread(NULL, 0, MyWrapper::myThread, params, 0, NULL);
    }
};

int main(int argc, char* argv[])
{
    MyWrapper mw;
    char msg[] = "Hi there.";

    HANDLE threadHandle = mw.createThread(1, msg);
    WaitForSingleObject(threadHandle, INFINITE);

    return 0;
}

Upvotes: 4

Related Questions