query about few threading terms

55 views Asked by At

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

2

There are 2 answers

1
Medinoc On BEST ANSWER
  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().
1
SJ_CAD On

for the point number three I guess I understood and will try differently. I was using

DWORD WINAPI save_uwpi_file0( LPVOID )
{
while(1)
{
    if(release == 1 && flag_oper1 == 1)
    {
    int w_cnt = 0;  FILE *opfile;
    char fname[30] = "txt_file0.txt";
    //opening file for write
    opfile = fopen(fname , "w");

    printf("assigning memory for file 1 \n");
    ssint *Lmem = (ssint *)malloc( sizeof(ssint)*size_of_memory);

    memcpy(Lmem, pInDMA, sizeof(ssint)*size_of_memory);
    release = 0;
    printf("relseaing for second file saving\n");
    for( int nbr = 0; nbr < size_of_memory; nbr++){
        fprintf(opfile , "%hi\n", Lmem[nbr] );
        }
    printf("aligned free 1\n");
    free(Lmem);

    fclose(opfile);

    printf("File saved 1\n\n");
    return 1;
    }   //if statement ends
}
}

and I was using following to make the pointer to (thread) function

DWORD WINAPI (* save_uwpi_file0)(LPVOID);

I guess I should try something like

DWORD (* save_uwpi_file0)(LPVOID);

I will do it and post the result here