找回密码
 注册
查看: 2012|回复: 0

求教:WINAPI并行问题

[复制链接]
发表于 2017-6-27 11:24:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
有一个串行的程序,想并行化,但是修改成并行程序后运行失败,下面附上提取出来的串行代码和修改后的并行代码,麻烦大神帮我看一下,在此先表示感谢!
串行代码如下:
  1. typedef struct{
  2.         BYTE *streamBuf;
  3.         int streamlen;
  4.         int decImageOffset;
  5.         BYTE *decBuf;
  6. }JParam;



  7. int fun(int id, JParam *decParam)
  8. {
  9.        
  10.        
  11.         BYTE *streamBuf = decParam->streamBuf;
  12.         int streamlength = decParam->streamlen;
  13.         int decImageOffset = decParam->decImageOffset;
  14.         BYTE *decBuf = decParam->decBuf;
  15.         Init(streamBuf, streamlength);
  16.        
  17.         f2();
  18.         f1();
  19.         memcpy(coef_1_48 + id * 4096 * 128, coef_1, 4096 * 128 * sizeof(int));
  20.        

  21.         return 0;
  22. }

  23. main()
  24. {
  25.     JParam dp[48];

  26.     for (i = 0; i < 48; i++)
  27.         {
  28.                 dp[i].streamBuf = streamsBuf[i] + nStart + 101;
  29.                 dp[i].streamlen = streamInfo[i].len - nStart - 101;
  30.                 dp[i].decImageOffset = decImageOffset;
  31.                 dp[i].decBuf = decBuf + i*decImageOffset;
  32.                 fun(i, dp + i);

  33.         }
  34. }
  35.        
复制代码


转换成并行程序如下:
  1. #define MAX_THREADS 48
  2.    
  3. typedef struct
  4. {
  5.           BYTE *streamBuf;
  6.           int streamlen;
  7.           int decImageOffset;
  8.           BYTE *decBuf;
  9. }JParam;
  10.        
  11. typedef struct MY_PARA
  12. {
  13.          int id;
  14.          JParam *decParam;

  15. }my_para;
  16.        
  17.        
  18.        
  19. DWORD WINAPI fun(LPVOID lpParam)
  20. {
  21.         my_para *pmd = (my_para *)lpParam;
  22.         BYTE *streamBuf = pmd->decParam->streamBuf;
  23.         int streamlength = pmd->decParam->streamlen;
  24.         int decImageOffset = pmd->decParam->decImageOffset;
  25.         BYTE *decBuf = pmd->decParam->decBuf;
  26.         int id = pmd->id;
  27.        
  28.         Init(streamBuf, streamlength);

  29.        
  30.         f2();
  31.         f1();
  32.         memcpy(coef_1_48 + id * 4096 * 128, coef_1, 4096 * 128 * sizeof(int));
  33.        
  34.         return 0;
  35. }
  36.        
  37.        
  38. main()
  39. {
  40.        
  41.         JParam dp[48];
  42.        
  43.         my_para j2para[MAX_THREADS];
  44.         HANDLE hThread[MAX_THREADS];
  45.        
  46.         for (int i = 0; i < 48; i++)
  47.         {
  48.                
  49.                 dp[i].streamBuf = streamsBuf[i] + nStart + 101;
  50.                 dp[i].streamlen = streamInfo[i].len - nStart - 101;
  51.                 dp[i].decImageOffset = decImageOffset;
  52.                 dp[i].decBuf = decBuf + i*decImageOffset;
  53.                 j2para[i].id = i;
  54.                 j2para[i].decParam = (dp + i);
  55.                 hThread[i] = CreateThread(NULL,0,fun,&j2para[i],0,NULL);
  56.                 if (hThread[i] == NULL)
  57.                         ExitProcess(i);

  58.         }
  59.         for (i = 0; i < 48; i++)
  60.                 WaitForSingleObject(hThread[i],INFINITE);
  61.         for (i = 0; i < MAX_THREADS; i++)
  62.                 CloseHandle(hThread[i]);
  63. }
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表