Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_beginthread and WaitForSingleObject

Tags:

c

windows

I am implementing a program that launches 4 threads.

The functions are:

void* SOURCE_in(struct SOURCE_1*);
void* SOURCE_out(struct SOURCE*);
void* SINK_out(struct SINK_1*);
void* SINK_in(struct SINK*);

The functions allow communication between 4 blocks. The Block 1 communicates with block 2. Block 2 share information with third block. Block 3 communicates information with the Block 4.

My problem is in main.c

int main(int argc, char** argv){

    extern int p, e_source, e_sink_1, l_sink, l_source_1, flag, ctr_source;
    extern int ctr_source_1, ctr_sink, ctr_sink1;

        SOURCE source;      /*C struct*/
        SINK sink;          /*C struct*/
        SOURCE_1 source_1;  /*C struct*/
        SINK_1 sink_1;      /*C struct*/

        HANDLE CHT[4];

        p = 0;
        e_source =  flag = 0;
        l_sink = e_sink_1 = l_source_1 = 1;
        max = 5;
        ctr_source = ctr_source_1 = ctr_sink = ctr_sink_1 = 0;

        /*Initialize FIFO*/
        F *f1 = fifo_init(10);
        F *f2 = fifo_init(10);

        /*Connect FIFO and modules*/
        source.output_source = f1;
        sink.input_sink = f1;

        sink_1.output_sink = f2;
        source_1.input_source = f2;

        /*Create Threads*/
        CHT[0] = (HANDLE)_beginthread((void (*)(void *))&SOURCE_out, 0, &f1);
        CHT[1] = (HANDLE)_beginthread((void (*)(void *))&SINK_in, 0, &f1);
        CHT[2] = (HANDLE)_beginthread((void (*)(void *))&SINK_out, 0, &f2);
        CHT[3] = (HANDLE)_beginthread((void (*)(void *))&SOURCE_in, 0, &f2);

        /* Wait until all threads have terminated */

        WaitForSingleObject(CHT[0], INFINITE);
        WaitForSingleObject(CHT[1], INFINITE);
        WaitForSingleObject(CHT[2], INFINITE);
        WaitForSingleObject(CHT[3], INFINITE);

        getchar();

        return 0;}

I read that WaitForSingleObject function does not work with _beginthread.... But my functions are not the type nunsigned __stdcall...

I build the programm withou errors, and I use breakpoints to test it and it was ok. when I compile I have this problem:

The thread 'Win32 Thread' (0x11ec) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x918) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x8a4) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x2a8) has exited with code -1073741510 (0xc000013a). The thread 'Win32 Thread' (0x12f8) has exited with code -1073741510 (0xc000013a). The program '[3984] SW=SW.exe: Native' has exited with code -1073741510 (0xc000013a).

The program never gets to the function getchar()

Before doing this program, I made a program that made communication between two blocks, one read and the other one write. In that case, I had no problems.

If I don not use the function WaitForSingleObject the problem disappears, but my program, almost all times, stops before finishing.

The function that each thread use, stops with a break. But i want the other ones to continue until its break apear..

like image 733
slayra Avatar asked May 30 '26 02:05

slayra


1 Answers

The documentation of _beginthread explains that you cannot use one of the wait functions:

The following sample code demonstrates how you can use the thread handle returned by _beginthreadex with the synchronization API WaitForSingleObject. The main thread waits for the second thread to terminate before it continues. When the second thread calls _endthreadex, it causes its thread object to go to the signaled state. This allows the primary thread to continue running. This cannot be done with _beginthread and _endthread, because _endthread calls CloseHandle, destroying the thread object before it can be set to the signaled state.

And this text also gives you the solution, namely to use _beginthreadex instead.

I think you are alluding to this in the question when you say that

but my functions are not __stdcall

You simply have to change your functions to use the __stdcall calling convention.

like image 128
David Heffernan Avatar answered May 31 '26 19:05

David Heffernan