0

I am using pthreads.h on Windows, for a simple raytracer. It seems like the main function is not waiting for the threads to finish. When I just run the program like this(I now simplified it, to just test the threads, but it still gives the error):

typedef struct {
    unsigned int id; 
} Thread_Data;

void* render_band(void* arg) {
    Thread_Data* data = (Thread_Data*)arg;
    printf("This is thread number %d", data->id);
    pthread_exit(0);
}

int main() {
    pthread_t threads[NUM_THREADS];
    Thread_Data data[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        data[i].id = id;

        int rc = pthread_create(&threads[i], NULL, render_band, &data[i]);
        if (rc) {
            printf("[ERROR] From pthread_create: %d\n", rc);
        }
    } 

    for (int i = 0; i < NUM_THREADS; i++) {
        int rc = pthread_join(threads[i], NULL);
        if (rc) {
            printf("[ERROR] From pthread_join: %d\n", rc);
        }
    }
}

The image won't be completed and only renders a couple of pixels. When I add a sleep however, the image does finish. Leading me to believe that pthread_join doesn't wait, even though the documentation says so. What am I missing here?

Edit: Added error checking, it returns error code 3 for the pthread_join.

Kay Verbruggen
  • 111
  • 1
  • 1
  • 9
  • 1
    How large is `NUM_THREADS`? And you should add error checking to the `pthread_create` and `pthread_join` calls. – Florian Weimer Jun 16 '18 at 21:45
  • 1
    You're missing error checks on your calls to `pthread_create()` and `pthread_join()`, so you don't know what's succeeding and what's failing. Could it be that `height / NUM_THREADS` evaluates to 0 (small picture, lots of threads), so the threads have nothing to do? Print out the details of what each thread is supposed to do — the `Thread_Data` entry for each thread — to make sure it does what you want. I thinks there's a good chance you're missing a lot of pixels because of the `min_y` and `max_y` calculations. – Jonathan Leffler Jun 16 '18 at 21:46
  • NUM_THREADS has been tested with 1 and 8, both failing and min_y and max_y, give the expected outcome. I will add error checking now. – Kay Verbruggen Jun 16 '18 at 21:53
  • 1
    See https://stackoverflow.com/questions/44050496/pthread-join-error-code-3 . The thread you try to join seems to have ceased to exist at the point of the join? How does your `render_band` look like? – Michael Beer Jun 16 '18 at 23:42
  • 1
    You should probably print the value of `i` when you get the failure. Is it the first thread (index 0) that fails? If so, there's at least some chance that your thread function, `render_band()`, accesses the `&data[i]` pointer in such as way as to write over the `pthread_t threads[NUM_THREADS];` array, thereby scotching your chances of getting anything useful back from `pthread_join()`. Do you get just one error or many? – Jonathan Leffler Jun 16 '18 at 23:48
  • according to the [`pthread_join` man page](http://man7.org/linux/man-pages/man3/pthread_join.3.html) a return value of 3 (`ESRCH`) means "No thread with the _ID thread_ could be found", which suggests your thread data structure is getting corrupted somehow. Please provide an MCVE, the problem isn't in the code shown. – yano Jun 16 '18 at 23:58
  • The code you show looks fine. So the problem has to be in the code you do *not* show. – alk Jun 17 '18 at 06:10
  • Every thread returns error code three. – Kay Verbruggen Jun 17 '18 at 09:14
  • I simplified the code, but it still gives the same error. It does all the printf() tho, with the expected data->id. – Kay Verbruggen Jun 17 '18 at 09:24
  • 1
    `data[i].id = id;` in the posted code doesn't look like it should compile as `id` is not defined anywhere. Did you mean `data[i].id = i;`? – Andrew Henle Jun 17 '18 at 11:37
  • In order to avoid errors like the missing `id`, it is required that you extract a [mcve] before posting here. As a new user, also read [ask] and take the [tour]. – Ulrich Eckhardt Nov 01 '18 at 21:10

1 Answers1

0

I am sorry for wasting everbody's time. The issue ended up being the library itself. When I was looking for pthreads.h on Windows, I found some library on sourceforge. But apparently MinGW already had pthread support, and it messed everything up. So for people with the same issue, just use the one that comes with MinGW.

Kay Verbruggen
  • 111
  • 1
  • 1
  • 9