求教:WINAPI并行问题
有一个串行的程序,想并行化,但是修改成并行程序后运行失败,下面附上提取出来的串行代码和修改后的并行代码,麻烦大神帮我看一下,在此先表示感谢!串行代码如下:
typedef struct{
BYTE *streamBuf;
int streamlen;
int decImageOffset;
BYTE *decBuf;
}JParam;
int fun(int id, JParam *decParam)
{
BYTE *streamBuf = decParam->streamBuf;
int streamlength = decParam->streamlen;
int decImageOffset = decParam->decImageOffset;
BYTE *decBuf = decParam->decBuf;
Init(streamBuf, streamlength);
f2();
f1();
memcpy(coef_1_48 + id * 4096 * 128, coef_1, 4096 * 128 * sizeof(int));
return 0;
}
main()
{
JParam dp;
for (i = 0; i < 48; i++)
{
dp.streamBuf = streamsBuf + nStart + 101;
dp.streamlen = streamInfo.len - nStart - 101;
dp.decImageOffset = decImageOffset;
dp.decBuf = decBuf + i*decImageOffset;
fun(i, dp + i);
}
}
转换成并行程序如下:
#define MAX_THREADS 48
typedef struct
{
BYTE *streamBuf;
int streamlen;
int decImageOffset;
BYTE *decBuf;
}JParam;
typedef struct MY_PARA
{
int id;
JParam *decParam;
}my_para;
DWORD WINAPI fun(LPVOID lpParam)
{
my_para *pmd = (my_para *)lpParam;
BYTE *streamBuf = pmd->decParam->streamBuf;
int streamlength = pmd->decParam->streamlen;
int decImageOffset = pmd->decParam->decImageOffset;
BYTE *decBuf = pmd->decParam->decBuf;
int id = pmd->id;
Init(streamBuf, streamlength);
f2();
f1();
memcpy(coef_1_48 + id * 4096 * 128, coef_1, 4096 * 128 * sizeof(int));
return 0;
}
main()
{
JParam dp;
my_para j2para;
HANDLE hThread;
for (int i = 0; i < 48; i++)
{
dp.streamBuf = streamsBuf + nStart + 101;
dp.streamlen = streamInfo.len - nStart - 101;
dp.decImageOffset = decImageOffset;
dp.decBuf = decBuf + i*decImageOffset;
j2para.id = i;
j2para.decParam = (dp + i);
hThread = CreateThread(NULL,0,fun,&j2para,0,NULL);
if (hThread == NULL)
ExitProcess(i);
}
for (i = 0; i < 48; i++)
WaitForSingleObject(hThread,INFINITE);
for (i = 0; i < MAX_THREADS; i++)
CloseHandle(hThread);
}
页:
[1]