小_黄_人 2015-04-01 11:56 采纳率: 100%
浏览 1692
已采纳

在添加Sleep()后,程序运行正确,注掉错误

// Parallel.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include
#include
#include

using namespace std;

int sum;
int sum1,sum2;
int **a;
int n;
CRITICAL_SECTION cs;
HANDLE evFin[2];

int power(int order){
if(order % 2 == 0)
return 1;
else return -1;
}

int cal_matrix(int** m, int n, int row, int col){

int** temp;
int sum = 0;

int temp_row = 0, temp_col = 0;

// EnterCriticalSection(&cs);
if(n == 0)
return sum = 1;

temp = new int* [n];
for(int i = 0; i < n ;i++)
{
    temp[i] = new int[n];
}

for(int i = 0 ; i <= n; i++){
    if(i == row - 1)
        continue;
    for(int j = 0;j <= n; j++){
        if(j != col - 1){
            if(temp_col < n){
                 //EnterCriticalSection(&cs);
                 temp[temp_row][temp_col++] = m[i][j];
                // LeaveCriticalSection(&cs);
            }
             else{
                 temp_col = 0;
                 temp_row++;
                 temp[temp_row][temp_col] = m[i][j];
                 temp_col++;
             }
        }   
    }
}

for(int i = 0; i< n; i++)
    for(int j = 0; j < n; j++)
        cout<<temp[i][j];
cout<<" ";
for(int i = 0; i < n; i++){
    sum += temp[row-1][i] * power(row+i+1) *  cal_matrix(temp,n-1,row,i+1);
}
return sum;
delete[] temp;
//LeaveCriticalSection(&cs);

}

void ThreadFunc1(PVOID param){

for(int i = 0; i < n/2; i++){
    sum1 += a[0][i] * power(1+i+1) *  cal_matrix(a,n-1,1,i+1);
}

//SetEvent(evFin[0]);

}

void ThreadFunc2(PVOID param){

for(int i = n/2; i < n; i++)
    sum2 += a[0][i] * power(1+i+1) *  cal_matrix(a,n-1,1,i+1);

}
int _tmain(int argc, _TCHAR* argv[])
{

//evFin[0] = CreateEvent(NULL,FALSE,FALSE,NULL);
// evFin[0] = CreateEvent(NULL,FALSE,FALSE,NULL);

cout<<"The degree of Matrix: "<<endl;
cin>>n;
a= new int* [n];
for(int i = 0; i < n ;i++)
{
    a[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n; j++)
    {
         cin>>a[i][j];
    }
}
/*for(int i = 0; i < n; i++){
    sum += a[0][i] * power(1+i+1) *  cal_matrix(a,n-1,1,i+1);
}*/

_beginthread(ThreadFunc1,0,NULL);
_beginthread(ThreadFunc2,0,NULL);


Sleep(1000);

sum = sum1+sum2;
cout<<sum;

delete[] a;

return 0;

}

  • 写回答

2条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-04-01 12:04
    关注

    Sleep(1000);停顿,你给了线程运行完成的时间,所以你如果你要去掉,就要考虑同步或者做一些限制,保证线程及其内部对象正常运转

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)