在线程之间划分工作? (线程)
Dividing work up between threads? (pthread)
我正在创建一个程序来对学校项目的一些数字进行一些数学运算。假设我有 10 个线程但有 42 个项目要处理,我希望它们平均处理所有项目并承担相同数量的作业。我正在使用 POSIX pthread 库,我知道它与互斥量有关,但我不完全确定。
这是我正在做的一个简化示例,但是我想平均地平衡工作负载。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int numbers = { 1, 78, 19, 49, 14, 1, 14. 19, 57, 15, 95, 19, 591, 591 };
void* method() {
for(size_t i = 0; i < 14; i++) {
printf("%d\n", (numbers[i] * 2));
}
}
int main(int argc, char const *argv[]) {
pthread_t th[10];
for (size_t i = 0; i < 10; i++) {
pthread_create(&th[i], NULL, method, NULL);
}
return 0;
}
如果您提前(即,在启动线程之前)知道需要处理多少项,则只需在线程之间对它们进行分区。例如,告诉第一个线程处理项目 0-9,下一个线程处理项目 10-19,或其他。
您希望每个线程处理 table 中的给定索引。只要您在线程之间正确划分工作,您就不必使用互斥锁来保护 table,这样它们就不会争夺相同的数据。
一个想法:
/* this structure will wrap all thread's data */
struct work
{
size_t start, end;
pthread_t tid;
};
void* method(void*);
#define IDX_N 42 /* in this example */
int main(int argc, char const *argv[])
{
struct work w[10];
size_t idx_start, idx_end, idx_n = IDX_N / 10;
idx_start = 0;
idx_end = idx_start + idx_n;
for (size_t i = 0; i < 10; i++)
{
w[i].start = idx_start; /* starting index */
w[i].end = idx_end; /* ending index */
/* pass the information about starting and ending point for each
* thread by pointing it's argument to appropriate work struct */
pthread_create(&w[i], NULL, method, (void*)&work[i]);
idx_start = idx_end;
idx_end = (idx_end + idx_n < IDX_N ? idx_end + idx_n : IDX_N);
}
return 0;
}
void*
method(void* arg)
{
struct work *w = (struct work* arg);
/* now each thread can learn where it should start and stop
* by examining indices that were passed to it in argument */
for(size_t i = w->start; i < w->end; i++)
printf("%d\n", (numbers[i] * 2));
return NULL;
}
我正在创建一个程序来对学校项目的一些数字进行一些数学运算。假设我有 10 个线程但有 42 个项目要处理,我希望它们平均处理所有项目并承担相同数量的作业。我正在使用 POSIX pthread 库,我知道它与互斥量有关,但我不完全确定。
这是我正在做的一个简化示例,但是我想平均地平衡工作负载。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int numbers = { 1, 78, 19, 49, 14, 1, 14. 19, 57, 15, 95, 19, 591, 591 };
void* method() {
for(size_t i = 0; i < 14; i++) {
printf("%d\n", (numbers[i] * 2));
}
}
int main(int argc, char const *argv[]) {
pthread_t th[10];
for (size_t i = 0; i < 10; i++) {
pthread_create(&th[i], NULL, method, NULL);
}
return 0;
}
如果您提前(即,在启动线程之前)知道需要处理多少项,则只需在线程之间对它们进行分区。例如,告诉第一个线程处理项目 0-9,下一个线程处理项目 10-19,或其他。
您希望每个线程处理 table 中的给定索引。只要您在线程之间正确划分工作,您就不必使用互斥锁来保护 table,这样它们就不会争夺相同的数据。
一个想法:
/* this structure will wrap all thread's data */
struct work
{
size_t start, end;
pthread_t tid;
};
void* method(void*);
#define IDX_N 42 /* in this example */
int main(int argc, char const *argv[])
{
struct work w[10];
size_t idx_start, idx_end, idx_n = IDX_N / 10;
idx_start = 0;
idx_end = idx_start + idx_n;
for (size_t i = 0; i < 10; i++)
{
w[i].start = idx_start; /* starting index */
w[i].end = idx_end; /* ending index */
/* pass the information about starting and ending point for each
* thread by pointing it's argument to appropriate work struct */
pthread_create(&w[i], NULL, method, (void*)&work[i]);
idx_start = idx_end;
idx_end = (idx_end + idx_n < IDX_N ? idx_end + idx_n : IDX_N);
}
return 0;
}
void*
method(void* arg)
{
struct work *w = (struct work* arg);
/* now each thread can learn where it should start and stop
* by examining indices that were passed to it in argument */
for(size_t i = w->start; i < w->end; i++)
printf("%d\n", (numbers[i] * 2));
return NULL;
}