// 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;
}
Sleep(1000);停頓,你給了線程運行完成的時間,所以你如果你要去掉,就要考慮同步或者做一些限制,保證線程及其內部對象正常運轉