简体   繁体   中英

C - Pthreads Using a Controller Function

I am attempting to write a toy pthread controller function outside of main() .

I am having issue with passing the argument struct into the pthread_create function. Essentially it outputs nothing (well let's call it "nothing").

  1. I assume that I am doing something wrong with the pointers to the struct wr in the pthread_create , and rather than outputting the struct I am attempting to output the struct pointer. What am I doing wrong here?

  2. Every example that I see online has pthread implementation in main() is this just a byproduct of "simple" explanations or is this how I should be doing it in the first place?

Note Yes I do realize that two threads pools are started synchronously. That's not the question here.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct wr {

    char str[100];
    int count;

};


void writeline (struct wr writer) {

    printf("out: %s\n", writer.str);
    writer.count--; //this is left over, just ignore it for now.
    pthread_exit(0);
    return NULL;
}


void controller (void (*func)(struct wr), struct wr writer, int threads){

    pthread_t *tid;
    tid = malloc(sizeof(pthread_t) * threads);
    int i;
    for (i = threads; i > 0; i--){
// Right here is where the problem starts.
        pthread_create(&tid[i], NULL, (void *) writeline, (void *) &writer); 

    }

    for (i = threads; i > 0; i--) {

        pthread_join(tid[i], NULL);
    }
}


int main () {

    struct wr writer1 = {.str = "Bop!", .count = 3};
    struct wr writer2 = {.str = "Boop!", .count = 3};

    controller(writeline, writer1, 10);
    controller(writeline, writer2, 2);

    return 0;
}

And my Makefile options:

CC=gcc
CFLAGS=-g -Wall -pthread

1) Your cast for the function is wrong:

    pthread_create(&tid[i], NULL, (void *) writeline, (void *) &writer); 

You are casting the function pointer to a data pointer, which is meaningless.

2) Your indexing is for tid is also wrong. When you allocate, for example, 2 pthread_t elements, your valid indexes are 0 and 1. But your for loop access 1 and 2.

3) You are not using the function pointer. So you don't need to pass it at all.

4) The thread function takes a void * as argument, not a struct . So you need to change it and retrieve the argument in the function by casting it back to struct wr* .

Code with above changes:

5) You need either pthread_exit or return . pthread_exit doesn't return. Remove one of them.

void* writeline (void *arg) {
    struct wr writer = *(struct wr*)arg;
    printf("out: %s\n", writer.str);
    return NULL;
}

void controller (struct wr writer, int threads){
    pthread_t *tid;
    tid = malloc(sizeof(pthread_t) * threads);
    int i;
    for (i = 0; i <threads; i++) {
        pthread_create(&tid[i], NULL, writeline, &writer);
    }

    for (i = 0; i <threads; i++) {
        pthread_join(tid[i], NULL);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM