Example #1
0
DWORD WINAPI threadFunc(LPVOID data)
{
    struct THREAD_PARAMS myParams = *(struct THREAD_PARAMS*) data;
    int len;
    char bgr[3];
    DWORD mutexResult;

    int y, x;
    for(y=myParams.yMax;y>=myParams.yMin;y--)
    {
        for(x=0; x<XSIZE ;x++)
        {
            getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0,&bgr[2],&bgr[1],&bgr[0]);

            mutexResult = WaitForSingleObject(mutex, INFINITE);
            if(mutexResult == WAIT_OBJECT_0)
            {
                len=fwrite(bgr,1,3,myParams.file);
                if(-1==len || len!=3)
                {
                    perror("write");
                    exit(4);
                }
            }else if(mutexResult == WAIT_ABANDONED)
            {
                return -1;
            }
        }
    }

    return 0;
}
Example #2
0
// Thread Rountine
DWORD WINAPI run(LPVOID *data) {
    // cast void pointer to integer value
    int *bins = (int*) data;
    int from = bins[0];
    int to = bins[1];
    int x, y, len;
    char bgr[3];
    long int bytepos;

    printf("Setup thread from %d to %d\n\n", from, to);

    for (y = from; y < to; y++) {
        for (x = 0; x < XSIZE; x++) {
            getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);
            bytepos = 3*((y * XSIZE) + x) + 54;

            // wait until other threads finished writing
            // and block others immediately
            WaitForSingleObject(sem, INFINITE);

            fseek(fd, bytepos, SEEK_SET);
            len = fwrite(bgr,1,3,fd);
            if (-1 == len || len != 3) {
                printf("error while writing\n");
                exit(1);
            }

            // work done, notify semaphore
            ReleaseSemaphore(sem, 1, NULL);
        }
    }

    // data was allocated dynamically before thread creation
    free(data);
}
Example #3
0
File: aufg22.c Project: flxw/bs1
void *threadRoutine (void *dataPointer) {
    struct ThreadData *td = (struct ThreadData *) dataPointer;
    int i, x, y;
    char* c;

    printf("new thread starting at offset %d, calculating %d values.\n", td->startPos, td->pixelCount);

    for (i = 0; i < td->pixelCount; ++i) {
        x = (td->startPos + i) % XSIZE;
        y = (td->startPos + i) / XSIZE;
        c = colorArray[y][x];

        getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0,&c[2],&c[1],&c[0]);
    }

    // we are the only one with a pointer to this - free it
    free(td);
    td = NULL;
}