gpt4 book ai didi

c - C语言中的逻辑问题。在pthread上定义交互次数

转载 作者:行者123 更新时间:2023-12-03 12:54:06 26 4
gpt4 key购买 nike

所以我有这些代码,我想手动定义交互次数,因此基本上每个线程我都说10个ios,因此每个线程将计算一个10的块。如果我这样做,则该线程不会在第一个10之后出现。

基本上我想要的是evrytime,一个线程完成10个交互的计算并执行另一个10个块,可以说10个交互中的100个有10个块,例如,我想要4个线程工作,每个线程在完成运行时计算一个块并获取另一个块

有人可以帮忙吗?

#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);
}

我已经尝试了无数次的尝试,但是却无法做到这一点。也许在虚空里面循环一会儿?有想法吗?

最佳答案

您可以使用任何线程池库来执行此操作。我已经修改了代码,并添加了一个额外的变量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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com