Search code examples
arraysmultithreadingwinapidword

query about few threading terms


I am understanding and implementing the concept of threading in my application. Since now things are going good. But I have few questions still unanswered and they are making me slow now. I would appreciate if anyone replies to even any of them

  1. In Createthread(), can we only take 1 argument? as I have seen in MSDN website and all other examples that I have seen I saw only 1 argument, LPVOID.

  2. The other thing is , what does the return value DWORD WINAPI means as a return value? Can we have only DWORD , int or any other return type. I suppose it has something to do with HANDLE (may be)

  3. I want to use the array of the thread, hence I learn the array to functions, and (as I have understood) threads are itself just a function called by CreateThread() routine, hence I tried to implement that concept there but could not because of the return type DWORD WINAPI was not allowing me to do so?

  4. I have one single thread for saving files, now I want its array so that I can save multiple files at the same time (not exaclty the same starting time, but sort of parallel file saving). How can I do that?

Thanks Shan


Solution

    1. Indeed, you can only take one argument, of type void * (LPVOID). However, since it can point to anything, it can point to a struct or object (usually allocated on the heap for lifetime reasons).
    2. WINAPI is not part of the return value, it's the function's calling convention. The function must return a DWORD or anything that fit in it. It must NOT return a pointer, because a pointer can't fit a DWORD in Win64.
    3. I don't understand, please elaborate what you're trying to do.
    4. Usually for this you need a single thread function, passed several times to CreateThread() with a different argument each time. Don't forget to keep the thread handles (which you'll likely save in an array) until you stop needing them and close them with CloseHandle().