0
votes

I'm writing a C program.

For thread I use the WINAPI library.

But sometimes the CreateThread function don't launch the function associate.

I used the WaitForSingleObject function with INFINITE param to let my thread start but he never start

The GetLastError functions return always 0,so I don't know where is my mistake

The merge function is call when GTK button is press.

Below you will find my code

void merge(GtkWidget *wiget, gpointer data){

    HANDLE thread;
    FtpLogin *login = (FtpLogin *) data;

    thread = CreateThread(NULL, 0, mergeThread, login, 0, NULL);

    printf("%ld", GetLastError());

    WaitForSingleObject(thread, INFINITE);

    if(thread == NULL)
     puts("error");
}

DWORD WINAPI mergeThread( LPVOID lpParam )
{
   puts("Thread start");

   return 0;
}

Thanks for your help

2
You will need to give more details on the errors or wrong behaviors you get. Have a look at minimal reproducible example. - Patrick Mevzek
@PatrickMevzek Thanks for your comment. I don't have any error, the thread just not start. The GetLastError function never return an error code. - M5623
CreateThread always launch your function, why you decide that mergeThread not executed ? - RbMm
puts("Thread start\n"); maybe. - Martin James

2 Answers

1
votes

The C run-time library needs some per-thread initialization. CreateThread(), which knows nothing about the C RTL, doesn't perform that.

Instead of CreateThread(), use _beginthreadex(). It's declared in <process.h>.

1
votes

According to this page, GTK is not thread safe. Anything that interacts with the GUI inside your mergeThread function can have unexpected results.

Please refer to the link I provided for more information about multithreaded GTK applications to see how to use GDK instead.