[英]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.