Tag: pthreads线程

C Pthreads – 线程安全队列实现的问题

我是multithreading的新手,我试图实现一个简单的线程安全任务队列,每个线程可以从中拉出工作,直到没有剩下的任务。 任何线程都不会排队任务。 出于测试目的,每个任务仅包含一个数字。 static pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER; typedef struct Task{ int number; }Task; typedef struct Cell{ Task t; struct Cell* next; }Cell; typedef struct TQueue{ struct Cell* head; struct Cell* tail; }TQueue; int empty(TQueue *Queue) return queue->head == queue->tail; void startQueue(TQueue *queue){ queue->head = malloc(sizeof(Cell)); queue->tail = queue->head; } void enqueue(TQueue *queue, Task C){ […]

C:用pthreads制作poolthread的方法是什么?

我有一个工作队列,我想建立一个4线程的池,我可以把我的工作。 我所坚持的是如何制作线程并在没有工作的情况下让它们暂停。 JOB QUEUE | job1 | job2 | job3 | job4 | .. THREAD POOL | thread1 | thread2 | thread3 | thread4 | 要创建我当前处于初始化点的线程: for (t=0; t<num_of_threads; t++){ pthread_create(&(threads[t]), NULL, doSth2, NULL); } num_of_threads = 4且doSth2是一个内部没有任何内容的函数。 所以一旦我创建了4个线程并且完成了doSth2,我怎么能给他们新的工作呢,而不会杀死他们?