[英]Counting a number in a list using pthreads.. C
我在使用pthreads计算列表中3的数量时遇到麻烦。 使用我的代码的串行版本可以正常工作,但是尝试使用pthread_create给我带来麻烦。 当前的问题是count3s_thread_2(int id)没有为我提供与串行版本相同的值。 我需要更改什么? PS,对不起,一团糟。 我是C语言编程的新手。
// Declares some global variables we will use throughout the
// program with all versions.
#define NUM_THREADS 4
int Length = 1000;
int array[1000];
int count;
long i;
pthread_mutex_t m;
pthread_t threads[NUM_THREADS];
void create_list(int *array)
{
srand(time(NULL));
for (i = 0; i < Length; i++)
{
int r = rand();
r = (r % 10) + 1;
array[i] = r;
}
}
void* count3s(void* threadid)
{
// This is the function that counts the number of threes for
// the first threaded version.
//int i = (intptr_t)threadid;
int i = (intptr_t)threadid;
long tid = (long)threadid;
int length_per_thread = Length / NUM_THREADS;
long start = tid * (long)length_per_thread;
for (i = start; i < start + length_per_thread; i++)
{
if (array[i] == 3)
{
count++;
}
}
pthread_exit(NULL);
}
void* count3s_v2(void* threadid)
{
// This is the function that counts the number of threes for
// the second threaded version.
//int serial = count3s_serial();
//printf("Number of threes: %d\n", serial);
int i = (intptr_t)threadid;
long tid = (long)threadid;
int length_per_thread = Length / NUM_THREADS;
long start = tid * (long)length_per_thread;
for (i = start; i < start + length_per_thread; i++)
{
if (array[i] == 3)
{
pthread_mutex_lock(&m);
count++;
pthread_mutex_unlock(&m);
}
}
pthread_exit(NULL);
}
int count3s_serial()
{
// This is the serial version of count3s. No threads are
// created and run separately from other threads.
count = 0;
for (i = 0; i < Length; i++)
{
if (array[i] == 3)
{
count++;
}
}
return count;
}
int count3s_thread(int id)
{
clock_t begin, end;
double time_spent;
begin = clock();
//pthread_attr_init();
for (i = 0; i < NUM_THREADS; i++)
{
pthread_create(&threads[i], NULL, count3s, (void *)i);
}
//pthread_attr_destroy();
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
return count;
}
int count3s_thread_2(int id)
{
clock_t begin, end;
double time_spent;
begin = clock();
pthread_attr_init(&something);
for (i = 0; i < NUM_THREADS; i++)
{
pthread_create(&threads[i], NULL, count3s_v2, (void *)i);
}
pthread_attr_destroy(&something);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
return count;
//printf("Thread Version 2: Number of threes = %d\nThread Version 2: Time Spent = %f\n", count, time_spent);
}
int main()
{
create_list(array);
clock_t begin, end;
double time_spent;
for (i = 0; i < Length; i++)
{
printf("%d\n", array[i]);
}
// Beginning of serial version. Timer begins, serial version
// is ran and after it's done, the timer stops.
begin = clock();
int serial = count3s_serial();
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Serial Version: Number of threes = %d\nSerial Version: Time Spent = %f\n", serial, time_spent);
// End of serial version.
/*
*********************************************************************
*/
// Beginning of first theaded version. Timer begins, first
// threaded version is ran and after it's done, the timer stops.
int the_thing = 0;
count = 0;
the_thing = count3s_thread(i);
printf("Thread Version 1: Number of threes = %d\nThread Version 1: Time Spent = %f\n", the_thing, time_spent);
// End of first threaded version.
/*
*********************************************************************
*/
// Beginning of second theaded version. Timer begins, second
// threaded version is ran and after it's done, the timer stops.
int the_other_thing = 0;
count = 0;
the_other_thing = count3s_thread_2(i);
printf("Thread Version 2: Number of threes = %d\nThread Version 2: Time Spent = %f\n", the_other_thing, time_spent);
pthread_exit(NULL);
}
问题是您生成了线程,但是在打印结果之前不要等待它们完成。 两个线程版本都有相同的问题。 使用pthread_join
等待线程退出或实现一些其他同步,以使父级知道线程何时完成工作。
例如,下面的代码块添加到两者的端count3s_thread
和count3s_thread_2
。 在打印结果之前,它将等待线程完成。 注意:您必须将其添加到两个功能(即使您可以将第一个计数错误也可以)。 否则,当您运行第二个线程版本时,第一组线程可能仍在执行,并且会弄乱全局count
。
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
好的,仔细研究一下您的代码并进行编译。 在宣布结果之前,您不必等待线程返回。 返回之前,您需要在count3s_thread()
pthread_join()
每个线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.