所以我有这些代码,我想手动定义我的交互数量,所以对于每个线程,我基本上定义了10个interactios,这样每个线程将计算10个 block 。如果我这样做,线程将不会在前 10 个。
基本上我想要的是,每次一个线程完成计算10次交互后,会进行另外10次交互,假设10次交互中的100次有10个我想要的 block ,例如4个线程工作,每个线程在完成时计算一个 block 并抓取另一个区 block
有人可以帮忙吗?
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NTHREADS 4
#define ARRAYSIZE 1000000
#define ITERATIONS ARRAYSIZE / NTHREADS
double sum=0.0, a[ARRAYSIZE];
pthread_mutex_t sum_mutex;
void *do_work(void *tid)
{
int i, start, *mytid, end;
double mysum=0.0;
/* Initialize my part of the global array and keep local sum */
mytid = (int *) tid;
start = (*mytid * ITERATIONS);
end = start + ITERATIONS;
printf ("Thread %d doing iterations %d to %d\n",*mytid,start,end-1);
for (i=start; i < end ; i++) {
a[i] = i * 1.0;
mysum = mysum + a[i];
}
/* Lock the mutex and update the global sum, then exit */
pthread_mutex_lock (&sum_mutex);
sum = sum + mysum;
pthread_mutex_unlock (&sum_mutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int i, start, tids[NTHREADS];
pthread_t threads[NTHREADS];
pthread_attr_t attr;
/* Pthreads setup: initialize mutex and explicitly create threads in a
joinable state (for portability). Pass each thread its loop offset */
pthread_mutex_init(&sum_mutex, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i=0; i<NTHREADS; i++) {
tids[i] = i;
pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
}
/* Wait for all threads to complete then print global sum */
for (i=0; i<NTHREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("Done. Sum= %e \n", sum);
sum=0.0;
for (i=0;i<ARRAYSIZE;i++){
a[i] = i*1.0;
sum = sum + a[i]; }
printf("Check Sum= %e\n",sum);
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&sum_mutex);
pthread_exit (NULL);
}
我尝试了无数的方法,但无法理解这样做的逻辑。也许在虚空中有一个 while 循环?有什么想法吗?
最佳答案
您可以使用任何线程池库来完成此操作。我修改了代码并添加了一个额外的变量 index_to_start
来决定从哪里开始计数。
代码已添加注释,您可以查看一下。
对于此类问题,我建议使用线程池库,它将处理大部分工作。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NTHREADS 4
#define ARRAYSIZE 1000000
#define ITERATIONS ARRAYSIZE / NTHREADS
// every thread will process BLOCK_SIZE numbers from array
#define BLOCK_SIZE 1000
double sum = 0.0, a[ARRAYSIZE];
// a mutex for index_to_start
pthread_mutex_t sum_mutex, index_mutex;
// this index tells thread that from where to start
static int index_to_start = 0;
void *do_work(void *tid)
{
int i, start, *mytid, end;
double mysum = 0.0;
/* Initialize my part of the global array and keep local sum */
mytid = (int *)tid;
// thread will be working untill index_to_start is less that ARRAYSIZE
while (1) {
// since index_to_start is shared, lock it
pthread_mutex_lock(&index_mutex);
if (index_to_start >= ARRAYSIZE) {
pthread_mutex_unlock(&index_mutex);
break;
}
// this is from where it should start counting
start = index_to_start;
// to find end just add BLOCK_SIZE to index_to_start and if it is going beyond ARRAYSIZE
// just assign it to ARRAYSIZE
if ((start + BLOCK_SIZE) < ARRAYSIZE)
index_to_start = end = start + BLOCK_SIZE;
else
index_to_start = end = ARRAYSIZE;
// we are done with index_to_star, unlock the mutex
pthread_mutex_unlock(&index_mutex);
mysum = 0;
printf ("Thread %d doing iterations %d to %d\n", *mytid, start, end-1);
for (i = start; i < end ; i++) {
a[i] = i * 1.0;
mysum = mysum + a[i];
}
/* Lock the mutex and update the global sum, then exit */
pthread_mutex_lock (&sum_mutex);
sum = sum + mysum;
pthread_mutex_unlock (&sum_mutex);
}
pthread_exit(NULL);
return NULL;
}
int main(int argc, char *argv[])
{
int i, start, tids[NTHREADS];
pthread_t threads[NTHREADS];
pthread_attr_t attr;
/* Pthreads setup: initialize mutex and explicitly create threads in a
* joinable state (for portability). Pass each thread its loop offset */
pthread_mutex_init(&sum_mutex, NULL);
pthread_mutex_init(&index_mutex, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i=0; i<NTHREADS; i++) {
tids[i] = i;
pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
}
/* Wait for all threads to complete then print global sum */
for (i=0; i<NTHREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("Done. Sum = %e\n", sum);
sum = 0.0;
for (i = 0; i < ARRAYSIZE; i++){
sum = sum + a[i];
}
printf("Check Sum = %e\n",sum);
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&sum_mutex);
pthread_mutex_destroy(&index_mutex);
pthread_exit (NULL);
}
关于c - C 中的逻辑问题。定义 pthread 上的交互数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204310/