[英]Counting a number in a list using pthreads.. C
I am having trouble getting using pthreads to count the number of 3's in a list. 我在使用pthreads计算列表中3的数量时遇到麻烦。 Using a serial version of my code works fine but trying to use pthread_create is giving me trouble.
使用我的代码的串行版本可以正常工作,但是尝试使用pthread_create给我带来麻烦。 Currently the problem is that count3s_thread_2(int id) is not giving me the same value as the serial version.
当前的问题是count3s_thread_2(int id)没有为我提供与串行版本相同的值。 What do I need to change?
我需要更改什么? PS, sorry for the mess.
PS,对不起,一团糟。 I am new to programming in C.
我是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);
}
The problem is that you spawn the threads but don't wait for them to finish before printing the result. 问题是您生成了线程,但是在打印结果之前不要等待它们完成。 Both thread versions have the same problem.
两个线程版本都有相同的问题。 Use
pthread_join
to wait for the threads to exit or implement some other synchronisation for parent to know when the threads have completed their work. 使用
pthread_join
等待线程退出或实现一些其他同步,以使父级知道线程何时完成工作。
For example, add the following block of code to the end of both count3s_thread
and count3s_thread_2
. 例如,下面的代码块添加到两者的端
count3s_thread
和count3s_thread_2
。 It will wait for the threads to complete before printing the result. 在打印结果之前,它将等待线程完成。 NOTE: You must add it to both functions (even though you are ok for the first one to have the wrong count).
注意:您必须将其添加到两个功能(即使您可以将第一个计数错误也可以)。 Otherwise when you run the second threading version the first set of threads are likely to still be executing and will mess up the global
count
. 否则,当您运行第二个线程版本时,第一组线程可能仍在执行,并且会弄乱全局
count
。
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
Okay, mucked around with your code and got it to compile. 好的,仔细研究一下您的代码并进行编译。 You aren't waiting for your threads to return before you announce the result.
在宣布结果之前,您不必等待线程返回。 You need to
pthread_join()
each of your threads in count3s_thread()
before it returns. 返回之前,您需要在
count3s_thread()
pthread_join()
每个线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.