简体   繁体   English

C-使用控制器功能的Pthreads

[英]C - Pthreads Using a Controller Function

I am attempting to write a toy pthread controller function outside of main() . 我试图在main()之外编写一个玩具pthread控制器函数。

I am having issue with passing the argument struct into the pthread_create function. 将参数struct传递到pthread_create函数时遇到问题。 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. 我假设我在pthread_create指向struct wr的指针做错了什么,而不是输出结构,而是尝试输出结构指针。 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? 我在网上看到的每个示例在main()都有pthread实现,这仅仅是“简单”说明的副产品,还是这首先应该是怎么做的?

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: 和我的Makefile选项:

CC=gcc
CFLAGS=-g -Wall -pthread

1) Your cast for the function is wrong: 1)您对该函数的转换是错误的:

    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. 2)您为tid编制索引也是错误的。 When you allocate, for example, 2 pthread_t elements, your valid indexes are 0 and 1. But your for loop access 1 and 2. 例如,当您分配2个pthread_t元素时,有效索引为0和1。但是您的for循环访问为1和2。

3) You are not using the function pointer. 3)您没有使用函数指针。 So you don't need to pass it at all. 因此,您根本不需要通过它。

4) The thread function takes a void * as argument, not a struct . 4)线程函数将void *作为参数,而不是struct So you need to change it and retrieve the argument in the function by casting it back to struct wr* . 因此,您需要对其进行更改,并通过将其强制转换回struct wr*来检索函数中的参数。

Code with above changes: 具有以上更改的代码:

5) You need either pthread_exit or return . 5)您需要pthread_exitreturn pthread_exit doesn't return. pthread_exit不返回。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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