E
E
evgenyt20002020-05-07 19:14:23
C++ / C#
evgenyt2000, 2020-05-07 19:14:23

How to pass two parameters to a thread?

How to pass 2 parameters to a thread: a pointer to an array and its size. Just a pointer is not enough, and then its size cannot be determined there.
So now:

int *array = new int[mSize];
HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)a,&array,0,&tid);

int a(int *array)
{
    min = array[0]; max = array[0];
    for (i = 0; i < mSize; i++) { sum += array[i]; Sleep(12); }
    cout << "Sum   :" << sum << endl;
    for (i = 0; i < mSize; i++){if (min > array[i])  min = array[i];}
    cout << "Min   :" << min << endl;
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
15432, 2020-05-07
@evgenyt2000

You can create new int[mSize+1], write the size in the first cell, use the rest for your own purposes... And in the thread, the size from the same array and extract

M
mayton2019, 2020-05-08
@mayton2019

Because C++ is an object language, the discussion of how to communicate something must be in terms of objects.
It's not assembler. Look at the header of the function for creating a thread.

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES   lpThreadAttributes,
  SIZE_T                  dwStackSize,
  LPTHREAD_START_ROUTINE  lpStartAddress,
  __drv_aliasesMem LPVOID lpParameter,
  DWORD                   dwCreationFlags,
  LPDWORD                 lpThreadId
);

In it, lpParameter should be a pointer to a structure from your array and length. Or just a vector of integers in STD terminology (std::vector)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question