Good day, I began to learn the flow in the pros. Elementary has learned to do, but the question is how to make the function type a line to set the number of times by the example of code?

DWORD WINAPI Func1 (void * param,int size) // сайз { for (int i=0; i < size; i++) cout << "SWAG" <<endl; return 0; } // main HANDLE thread1=CreateThread(0,0,Func1,0,0,0); // как сюда кинуть параметр? 

Tell me please.

  • 2
    Did you read the documentation? The function called from CreateThread be. one parameter . So, make a structure (it is better to allocate memory for it with malloc), fill it with the necessary parameters and pass its address. - avp
  • @petruska, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

1 answer 1

See it.

First, it is best to switch to the latest version of the compiler and use the universal std::thread and progressive lambda instead of platform-specific WinAPI:

 #include <thread> std::thread t([size]() { for (int i = 0; i < size; i++) std::cout << "SWAG" << std::endl; }); 

(By the way, using console output from different threads is not a good idea .)

If you still want to use WinAPI, you will have to pass the parameter in an untyped way - via the void* parameter. To do this, you have to select the necessary data (parameter) in the heap-memory, transfer the pointer to them in ThreadProc , then apply it to the desired type and apply. Do not forget to destroy the selected data.