问题是:编写全局整数大小为N的一维数组A的程序的因子M。使用线程,主线程用随机整数初始化A和M,创建N个线程,使得第i个线程找到Ai =M* Ai,最后打印数组A。
我的问题是,我有一个数组初始化的错误,我也不确定以正确的方式制作线程。我希望我已经把我的问题解释清楚了。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int M;
int [10] A;
void *run(void *t)
{
A[(int)t]=A[(int)t]*M;
pthread_exit(NULL);
}
int main()
{
pthread_t [10] th;
srand(time(0))%20;
M=rand();
int t;
for(t = 0; t < 10; t++)
{
pthread_create(&th[t], NULL, run, (void *)t);
}
int j;
for(j=0;j<10;j++)
A[j]=j;
int i;
for(i = 0; i < 10; i++)
{
pthread_join(th[i], NULL);
printf("%d: %d\n", i,A[i]);
}
printf("Thread main\n");
pthread_exit(NULL);
}以下是错误:
que1.c:5: error: expected identifier or ‘(’ before ‘[’ token
que1.c: In function ‘run’:
que1.c:9: error: ‘A’ undeclared (first use in this function)
que1.c:9: error: (Each undeclared identifier is reported only once
que1.c:9: error: for each function it appears in.)
que1.c: In function ‘main’:
que1.c:16: error: expected identifier or ‘(’ before ‘[’ token
que1.c:17: error: void value not ignored as it ought to be
que1.c:22: error: ‘th’ undeclared (first use in this function)
que1.c:27: error: ‘A’ undeclared (first use in this function)发布于 2011-03-16 20:15:55
在编写int [10] A时,看起来您有一个Java背景(或D)。你可以把它理解为A是一个10整型数组对象的变量。但在C中,这些东西有点不同。在C中,只有单独的原始变量,它们可以排列在连续的内存集合中,这就称为一个数组。这就是为什么选择语法int A[10];的原因,这意味着:A是一个指向整数的指针,指向自动分配给10个整数的存储的开始。
程序的下一个问题是通过void*类型传递整数,尽管编译器不会报告它。在C中,每个指针都可以转换为某个整数,但并不是所有的整数值都是指针。C中的指针是抽象的东西:大多数实现选择使用内存地址作为其整数表示。但通过内射LUT映射它们也是有效的。因此,一些整数值可能无法映射到指针,因此无法正确传输。你真的应该传递一个真正的指针。
此外,您对线程的使用也是次优的。在您的例子中,您正在像mad一样丢弃缓存线,并且切换开销将吞噬所有的并行化收益。根据经验,您创建的线程数不应超过‘数量’+2个线程。考虑到所有这些因素,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* some prime number, so show what happens with uneven work sizes */
#define ELEMENTS 17
int M;
int A[ELEMENTS];
struct WorkletInfo {
int *data_base;
int count;
};
void *worklet(void *t)
{
int i;
struct WorkletInfo *wi = t; /* note: no cast here */
if(!wi)
return 0;
for(i=0; i < wi->count; i++)
wi->data_base[i] *= M;
return wi;
}
#define N_CPUS 4
#define NUM_THREADS (N_CPUS+1)
int main(int argc, char *argv[])
{
int t, ec;
pthread_t th[NUM_THREADS];
int remaining_elements;
int const elements_per_worklet = ELEMENTS/NUM_THREADS;
srand(time(0));
M=rand();
/* you must initialize the data a thread will access before starting the thread */
for(t = 0; t < ELEMENTS; t++)
A[t]=t+1; /* 0 * x = 0, so give it some nonzero value */
remaining_elements = ELEMENTS - (elements_per_worklet*NUM_THREADS);
ec = 0;
for(t = 0; t < NUM_THREADS; t++)
{
struct WorkletInfo *wi = malloc(sizeof(*wi));
wi->data_base = &A[ec];
wi->count = elements_per_worklet;
if(t<remaining_elements) {
wi->count++;
}
ec += wi->count;
pthread_create(&th[t], NULL, worklet, wi);
}
ec = 0;
for(t = 0; t < NUM_THREADS; t++)
{
int i;
struct WorkletInfo *wi;
void *retval;
pthread_join(th[t], &retval);
if(!retval)
continue;
wi = retval;
for(i = 0; i < wi->count; i++, ec++)
printf("worklet %d, worklet element %d => total element %d: %d\n",
t, i, ec,
wi->data_base[i]);
free(wi);
}
printf("Thread main\n");
return 0;
}此程序的输出如下所示:
worklet 0, worklet element 0 => total element 0: 300787748
worklet 0, worklet element 1 => total element 1: 601575496
worklet 0, worklet element 2 => total element 2: 902363244
worklet 1, worklet element 0 => total element 3: 1203150992
worklet 1, worklet element 1 => total element 4: 1503938740
worklet 1, worklet element 2 => total element 5: 1804726488
worklet 1, worklet element 3 => total element 6: 2105514236
worklet 2, worklet element 0 => total element 7: -1888665312
worklet 2, worklet element 1 => total element 8: -1587877564
worklet 2, worklet element 2 => total element 9: -1287089816
worklet 3, worklet element 0 => total element 10: -986302068
worklet 3, worklet element 1 => total element 11: -685514320
worklet 3, worklet element 2 => total element 12: -384726572
worklet 3, worklet element 3 => total element 13: -83938824
worklet 4, worklet element 0 => total element 14: 216848924
worklet 4, worklet element 1 => total element 15: 517636672
worklet 4, worklet element 2 => total element 16: 818424420
Thread main exit发布于 2011-03-16 19:14:35
你的第一个问题纯粹是一个语法问题。这是无效的C:
int [10] A;它应该是:
int A[10];pthread_t [10] th;也是如此。
发布于 2011-03-16 19:21:04
除了@Oli Charlesworth的回答之外,srand不返回任何值,它被声明为void,所以您不能编写srand(time(0)) % 20。
https://stackoverflow.com/questions/5324432
复制相似问题