im尝试实现具有多个线程的多线程(用户在启动程序时可以输入worker的数量=线程) 每个线程在其中调用functionA,然后调用functionB。但是在functionB之前应该只在所有线程之后执行 称为functionA。那就是我的伪代码:
complete_data['Sex'].value_counts()
complete_data['Survived'].value_counts()
complete_data['Parch'].value_counts()
complete_data['Pclass'].value_counts()
complete_data['SibSp'].value_counts()
complete_data['Embarked'].value_counts()
我用谷歌搜索发现了“条件变量”的可能性。但是我不确定他们是否必须为此条件实施
void* worker_do(void* worker_id)
{
functionA((size_t) worker_id);
// First thread should only start functionB after ALL threads
// are finished with functionA
functionB((size_t) worker_id);
return NULL;
}
// I am not allowed to change pthread_create and pthread_join here
int main()
{
// should be a flexible value
ssize_t num_workers = 20;
pthread_t* workers = malloc(num_workers*sizeof(pthread_t));
for(ssize_t i = 0; i < num_workers; i++)
pthread_create(&workers[i], NULL, worker_do, (void*) i);
for(ssize_t i = 0; i < num_workers; i++)
pthread_join(workers[i], NULL);
free(workers);
return 0;
}
还是条件变量不是解决此问题的正确工具?
我将非常感谢小费我如何实现这一目标...
bw罗伯特
答案 0 :(得分:1)
我假设functionA()
和functionB()
可以由线程并行执行,因为当前代码中没有互斥保护。
为了解决您的问题,您可以使用简单的轮询机制。执行functionA()之后,每个线程都会增加一个计数器。所有线程将等待,直到计数器变为等于创建的线程数。
对于这种方法,您需要在所有线程之间都有一个互斥体和一个计数器。为了简化代码,我使用了全局变量。
unsigned int num_threads = 0;
unsigned int num_threads_completed_functionA = 0;
pthread_mutex_t lock;
void* worker_do(void* worker_id)
{
functionA((size_t) worker_id);
// First thread should only start functionB after ALL threads are finished with functionA
//Lock the mutex and update the counter
pthread_mutex_lock(&lock);
num_threads_completed_functionA++;
pthread_mutex_unlock(&lock);
while(1)
{
//Lock mutex and check how many threads completed execution of functionA()
pthread_mutex_lock(&lock);
if(num_threads_completed_functionA == num_threads)
{
//If all threads completed, then break the loop and proceed executing functionB()
pthread_mutex_unlock(&lock);
break;
}
pthread_mutex_unlock(&lock);
usleep(1); //Sleep for some time
}
//ALL threads are finished with functionA
functionB((size_t) worker_id);
return NULL;
}
答案 1 :(得分:0)
自从您询问要使用条件变量和互斥量执行此操作后,您就可以这样:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <inttypes.h>
#define N_THREADS 10
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
unsigned int count = 0;
void functionA(intptr_t id)
{
printf("functionA: %" PRIdPTR "\n", id);
}
void functionB(intptr_t id)
{
printf("functionB: %" PRIdPTR "\n", id);
}
void* thread_proc(void* pv)
{
intptr_t id = (intptr_t)pv;
functionA(id);
// lock the mutex to protect the predicate data (count)
pthread_mutex_lock(&mtx);
++count;
pthread_cond_broadcast(&cv);
// wait for all threads to finish A
while (count < N_THREADS)
pthread_cond_wait(&cv, &mtx);
// this is still owned by us. release it.
pthread_mutex_unlock(&mtx);
// now B
functionB(id);
return NULL;
}
int main()
{
pthread_t thrds[N_THREADS];
for (int i=0; i<N_THREADS; ++i)
pthread_create(thrds+i, NULL, thread_proc, (void*)(intptr_t)(i+1));
for (int i=0; i<N_THREADS; ++i)
pthread_join(thrds[i], NULL);
return EXIT_SUCCESS;
}
样本输出(可变)
functionA: 1
functionA: 4
functionA: 6
functionA: 3
functionA: 2
functionA: 8
functionA: 9
functionA: 7
functionA: 10
functionA: 5
functionB: 10
functionB: 9
functionB: 5
functionB: 7
functionB: 4
functionB: 6
functionB: 1
functionB: 2
functionB: 8
functionB: 3
正如乔纳森(Jonathan)在一般性评论中指出的那样, barrier 是解决此问题的一种更为优雅的解决方案。我会举一个例子,但是可惜我的环境不支持它们(悲伤,mac os x)。它们在大多数Unix pthread实现中都可用,因此,如果您的目标平台提供了它们,我建议您对它们进行适当的研究。