【问题标题】:thread pool with is not returning variable没有返回变量的线程池
【发布时间】:2020-11-17 06:37:49
【问题描述】:

我正在学习队列以及如何使用线程池来实现它们(这两个对我来说都是相当新的)。我以为我修改了我在网上找到的正确示例,但是我的线程函数中的 pclient 变量始终为 NULL。有没有经验丰富的人知道是怎么回事?

main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include "myqueue2.h"

#define THREAD_POOL_SIZE 3
#define MAX_INT 10

pthread_t thread_pool[THREAD_POOL_SIZE];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *thread_function(void *arg);

int main(int argc, char **argv)
{
    queue q1;
    // Create a bunch of threads to handle future data
    for(int i=0; i < THREAD_POOL_SIZE; i++){
        pthread_create(&thread_pool[i], NULL, thread_function, NULL);
        printf("Thread %d created\n",i);
    }
    printf("Threads created\n");

    //Create linked list
    for(int i=0; i < MAX_INT; i++){
    pthread_mutex_lock(&mutex);
    int *client = malloc(sizeof(int));
    *client = i;
    enqueue(&i);
    pthread_mutex_unlock(&mutex);
    }

    return 0;
}

void *thread_function(void *arg){
    while (true){
        int *pclient = dequeue();
        if (pclient != NULL){
            //We have stuff to do
            int t;
            pthread_mutex_lock(&mutex);
            t = dequeue();
            pthread_mutex_unlock(&mutex);
            printf("t = %d\n",t);
        } else {
            printf("No work to do\n");
        }
    }
}

myqueue2.c

#include "myqueue2.h"
#include <stdlib.h>

node_t *head = NULL;
node_t *tail = NULL;


void enqueue(int *value){
    node_t  *newnode = malloc(sizeof(node_t));
    newnode->value = value;
    newnode->next = NULL;
    //if there is a tail, connect that tail to this new node.
    if (tail == NULL){
        head = newnode; //make sure the head still makes sense
    } else {
    tail->next = newnode;
    }
    tail = newnode;
}

int* dequeue(){
    //make sure queue is empty
    if (head == NULL){
        return NULL;
    } else{

    //save result we want to return
    int *result = head->value;

    //save head of queue
    node_t *tmp = head;

    // take it off the queue
    head = head->next;

    if (head == NULL){tail = NULL;}
    free(tmp);
    return result;
    }
}

myqueue2.h

#ifndef MYQUEUE_H_
#define MYQUEUE_H_

typedef struct node {
    int value;
    struct node *next;
} node;

typedef struct node node_t;

typedef struct{
    node *head;
    node *tail;
} queue;

void enqueue(int *value);
int* dequeue();

#endif

【问题讨论】:

  • 警告太多了!你没有启用警告吗? (我使用-Wall -Wextra -pedanticgcc
  • 你不能在没有锁的情况下调用dequeue。您可以在 thread_function 中的 if 中执行此操作。

标签: c queue threadpool


【解决方案1】:

创建完线程后立即退出。您需要等待线程完成。您可以使用pthread_join 等待线程退出。

下一个问题是你的线程永远不会退出。一旦队列为空且不会添加任何其他内容,线程需要退出。

其他问题:

  • 始终启用编译器的警告。错误太多了。
  • 当线程从队列中获得某些东西时,它会直接丢弃它并使用队列中的下一个东西(如果有的话)。
  • 无法始终确保对队列进行互斥访问。 (请参阅第一次致电dequeue。)
  • 内存泄漏。传递给enqueue 的块都没有被释放。
  • 您正忙于等待,占用所有 CPU,而不是等待有事可做。

Solution(使用循环缓冲区而不是链表,但这无关紧要。)

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多