#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_THREADS 4
int arraySum = 0;
int array[] = {20, 18, 16, 14, 12, 10, 8, 6, 4, 2, -10, -20, -30, -40, 15, 23, 25, 75, 45, 33};
int low = 0;
int high = 4;
// Function that calculates sum of 5 elements of an array
void *ArraySum(void *threadid)
{
// Iterating from index low to high
for(int i = low; i < high; i++)
{
// Accumulating array sum
arraySum = arraySum + array[i];
}
// Updating lowest and highest index
low = low + 5;
high = high + 5;
// Exiting current thread
pthread_exit(NULL);
}
// Main function
int main(int argc, char *argv[])
{
int rc, t;
// Creating thread array
pthread_t threads[NUM_THREADS];
// Iterating over each thread
for(t = 0; t < NUM_THREADS; t++)
{
// Creating a thread
rc = pthread_create(&threads[t], NULL, ArraySum, &t);
// Checking for status
if(rc)
{
printf(" Error; return code from pthread_create() is %d \n", rc);
exit(-1);
}
}
/* Waiting till all threads finish their execution */
for (t = 0; t < NUM_THREADS; t++)
{
pthread_join(threads[t], NULL);
}
// Printing Array sum
printf("\nArray Sum: %d \n", arraySum);
pthread_exit(NULL);
}
目标是创建4个不同的线程并将数组中的所有数字加在一起。例如,第一个线程将添加前5个数字(20 + 18 + 16 + 14 + 12)。第二个线程将添加下一个5,依此类推。
当我运行它时,我总共获得164但我期望211.我相信我创建了4个不同的线程并正确加入它们但输出错误。
答案 0 :(得分:0)
在这里,我编写了将不同线程相加的解决方案。我基本上使用线程id来确定每个线程的开始/结束索引。因此,每个线程计算一个部分和,因此不需要同步。当一个线程退出时,他的部分金额将被返回并累积在最终金额中。
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_THREADS 4
int array[] = {20, 18, 16, 14, 12, 10, 8, 6, 4, 2, -10, -20, -30, -40, 15, 23, 25, 75, 45, 33};
// Function that calculates sum of 5 elements of an array
void *ArraySum(void *arg)
{
int id = (int)arg;
int partial_sum = 0;
int begin = id * (NUM_THREADS + 1);
int end = id * (NUM_THREADS + 1) + NUM_THREADS;
// Iterating from index low to high
for(int i = begin; i <= end; i++)
{
// Accumulating array partial sum
partial_sum += array[i];
}
// Exiting current thread
pthread_exit((void *)partial_sum);
}
// Main function
int main(int argc, char *argv[])
{
int rc, t;
int arraySum = 0;
void *retvalue;
// Creating thread array
pthread_t threads[NUM_THREADS];
// Iterating over each thread
for(t = 0; t < NUM_THREADS; t++)
{
// Creating a thread
rc = pthread_create(&threads[t], NULL, ArraySum, (void *)t);
// Checking for status
if(rc)
{
printf(" Error; return code from pthread_create() is %d \n", rc);
exit(-1);
}
}
/* Waiting till all threads finish their execution */
for (t = 0; t < NUM_THREADS; t++)
{
pthread_join(threads[t], &retvalue);
arraySum += (int)retvalue;
}
// Printing Array sum
printf("\nArray Sum: %d \n", arraySum);
pthread_exit(NULL);
}
答案 1 :(得分:0)
如果您在共享变量(在您的情况下为 arraySum )上添加互斥锁,您的代码将会很好地工作 这是我尝试使用互斥锁的示例代码:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define NUM_THREADS 5
pthread_t threads[NUM_THREADS];
pthread_mutex_t mutexsum;
int a[2500];
int sum = 0;
void *do_work(void* parms) {
long tid = (long)parms;
printf("I am thread # %ld\n ", tid);
int start, end, mysum;
start = (int)tid * 500;
end = start + 500;
int i = 0;
printf("Thread # %ld with start = %d and end = %d \n",tid,start,end);
for (int i = start; i < end; i++)
{
mysum += a[i];
}
pthread_mutex_lock(&mutexsum);
printf("Thread # %ld lock and sum = %d\n",tid,sum);
sum += mysum;
pthread_mutex_unlock(&mutexsum);
pthread_exit(NULL);
}
void main(int argv, char* argc) {
int i = 0; int rc;
pthread_attr_t attr;
pthread_mutex_init(&mutexsum, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_mutex_init(&mutexsum, NULL);
printf("Initializing array : \n");
for(i=0;i<2500;i++){
a[i]=1;
}
for (i = 0; i < NUM_THREADS; i++) {
printf("Creating thread # %d.\n", i);
rc = pthread_create(&threads[i], &attr, &do_work, (void *)i);
if (rc) {
printf("Error in thread %d with rc = %d. \n", i, rc);
exit(-1);
}
}
pthread_attr_destroy(&attr);
printf("Creating threads complete. start ruun " );
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
printf("\n\tSum : %d", sum);
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}