gpt4 book ai didi

c - 在 Win32 API 上将结构传递给线程时出现指针问题

转载 作者:行者123 更新时间:2023-11-30 15:41:07 25 4
gpt4 key购买 nike

用户提供用于计算分区数量和线程数量的命令行参数,其中每个线程对大数组的特定分区进行最小线性搜索。线程找到的每个最小值都存储在一个小的全局数组中。然后,主函数对小数组进行最小线性搜索,并对大数组进行最小搜索,并确认在小数组和大数组中找到的最小值相等。我遇到的问题是,小全局数组内的最小值有时是垃圾,有时与大数组中找到的最小值匹配。我试图找出问题所在,但似乎没有找到。我们将非常感谢您的帮助。我正在用 C 进行编码,在 win32 API 上使用 Dev-C++。代码如下:

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>

#define RAND_DIVISOR 800

int number_items = 0;
int size = 1;
int partits = 1;
int P = 0;
int N = 0;
int Index = 0;
int index_global = 0;
int min;


#define NUM_THREADS 65536 //or 2^16


typedef struct thread_data
{
int thread_id;
int a;
int b;
int * copy_array;
int * glob_array;
int nbr_items;
int subarraysize;

} s_param, *p_s_param;


int compare (const void *a, const void *b)
{
return( *(int*)a - *(int*)b);

}


DWORD WINAPI CompMin( LPVOID lpParam )
{
int i, tmp;
int SubArSize,nbrItems,thrid;
p_s_param param2;
param2 = (p_s_param)lpParam;


min = param2->copy_array[Index];
min = param2->copy_array[param2->a];
param2->glob_array[index_global] = min;
Index++;
index_global++;

}


int main(int argc, char *argv[])
{
int sub_array_size;


p_s_param pDataArray[NUM_THREADS];
DWORD dwThreadIdArray[NUM_THREADS];
HANDLE hThreadArray[NUM_THREADS];
HANDLE myhandle;
//pthread_t thID, thread;

p_s_param param[NUM_THREADS];
int rNum, rc = 0, i, j, large_min;


double time1, time2, time3, time4;

//get initial timestamp in micro seconds
struct timeval tv;
gettimeofday( &tv, NULL );
time1 = tv.tv_sec + ( tv.tv_usec / 1000000.0 );
printf( "Start timestamp: %f\n", time1 );


if(argc < 2 )
{
printf("Need %d arguments, only %d provided\n", 2, argc);
printf("The program will exit now!\n");
return 1;
}

P = atoi(argv[1]); /* will be used to define size of large array */
N = atoi(argv[2]); /* will be used to define number of threads */

if(N>P)
{
printf(" Argument 1 should be greater than argument 2\n");
printf("The program will exit now!\n");
return 1;
}

/*compute the size of the array*/
for (i=1; i<=P; i++)
size = size * 2;

/*Create a dynamic array of size size*/
int *array = (int*) malloc(size*sizeof(int));

srand(time(NULL));

for (i=0; i<size; i++)
{
rNum = rand() / RAND_DIVISOR;
array[i] = rNum;

}

/*compute the number of partitions*/
for (i = 1; i<=N; i++)
partits = partits * 2;

/*numbers of elements per sub array*/
sub_array_size = size/partits;

/*Global array*/
int *Globalarray = (int*) malloc(partits*sizeof(int));


for (i=0; i<partits; i++)
{

/*Allocate memory for thread data*/
param[i] = (p_s_param) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(s_param));

if( param[i] == NULL)
{
ExitProcess(2);
}

param[i]->a=i;

param[i]->copy_array=array;

param[i]->glob_array = Globalarray;

hThreadArray[i] = CreateThread(NULL, 0, CompMin, param[i], 0, &dwThreadIdArray[i]);

if(hThreadArray[i] == NULL)
{
puts("Error, cannot create Thread!");
puts(strerror(errno));
ExitProcess(3);

}


//printf("Number of partitions: %d\n",partits );
} WaitForMultipleObjects(NUM_THREADS,hThreadArray, TRUE, INFINITE);



/*find mimimum value from Global array returned by threads*/
min = Globalarray[0];
for(i = 0; i< partits; i++)
{
printf("Index: %d, value into small array: %d\n",i, Globalarray[i] );
if(Globalarray[i] < min)
min = Globalarray[i];
}


gettimeofday( &tv, NULL );
time2 = tv.tv_sec + ( tv.tv_usec / 1000000.0 );


gettimeofday( &tv, NULL );
time3 = tv.tv_sec + ( tv.tv_usec / 1000000.0 );

/*sorting the large array in ascending order and find minimum value*/
//qsort(array,size, sizeof(int), compare);
large_min = array[0];
for(i = 0; i< partits; i++)
{
printf("value into large array: %d\n",array[i] );
if(array[i] < large_min)
large_min = array[i];
}
//large_min = array[0];

gettimeofday( &tv, NULL );
time4 = tv.tv_sec + ( tv.tv_usec / 1000000.0 );

/*display result*/
printf("Min from small array : %d\n", min);
printf("Min from large array : %d\n", large_min);
if(min == large_min)
printf("Same minimum found in small and large array! : %d\n", large_min);

else
{
printf("error!, the min from small %d array is different from large array %d!\n", min, array[0]);

return 1;
}
printf("length of time recorded to search min in small array: %f\n", time2-time1);

printf("length of time recorded to search min in large array: %f\n", time4-time3);


free((void*) Globalarray);
free((void*) array);


exit (0);

}

最佳答案

我刚刚在等待后添加了一个 sleep(3),它解决了问题。

关于c - 在 Win32 API 上将结构传递给线程时出现指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20649463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com