problem with multithreading!!!

Hello everyone!!
I must finish an exercise :" writing a program that use multithreading to slove matrix multiplication "
I have Matrix A (dim1*dim2) and matrix B(dim3*dim4).Matrix C=A*B.
I used dim1*dim4 threads to calculate matrix C.
I just finished my code but it have a problem with WaitForMultipleObjects() Function.
I use this function to make main thread wait all worker thread before printing the output.
This function didn't work.
My code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
using namespace std;
#define max 50
int a[max][max];
int b[max][max];
int c[max][max];
int dim1;
int dim2;
int dim3;
int dim4;
struct v
{
	int i;

	int j;
};

typedef v* pv;

DWORD WINAPI result_rowi_columnj(LPVOID);


void main()
{
	ifstream inFileA;
	 
	inFileA.open("matrixA.txt");
	inFileA>>dim1;
	inFileA>>dim2;

	for(int i=0;i<dim1;i++)
		for(int j=0;j<dim2;j++)
		{
			inFileA>>a[i][j];
		}
	inFileA.close();

	///////////////////////////////////////////////////////
	ifstream inFileB;
	
	inFileB.open("matrixB.txt");
	inFileB>>dim3;
	inFileB>>dim4;
	for(int i=0;i<dim3;i++)
		for(int j=0;j<dim4;j++)
		{
			inFileB>>b[i][j];
		}
	inFileB.close();
	///////////////////////////////////////////////////////////////////////////
	cout<<endl<<" matrix A:"<<endl;
	for(int i=0;i<dim1;i++)
	{
		for(int j=0;j<dim2;j++)
		cout<<"  "<<a[i][j];
		cout<<endl;
	}
	cout<<endl<<" matrix B:"<<endl;
	for(int i=0;i<dim3;i++)
	{
		for(int j=0;j<dim4;j++)
		cout<<"  "<<b[i][j];
		cout<<endl;
		
	}////////////////////////////////////////////////////////////////////////////

HANDLE* hThread = new HANDLE[dim1 * dim4];
DWORD *pThreadID= new DWORD[dim1*dim4];
pv arraypos= new v[dim1*dim4];
DWORD state;


for(int i=0;i<dim1;i++)
		for(int j=0;j<dim4;j++)
		{

			arraypos[i*dim1+j].i=i;
			arraypos[i*dim1+j].j=j;
			hThread[i*dim1+j]=CreateThread(NULL,0,result_rowi_columnj,(arraypos+i*dim1+j),0,&pThreadID[i*dim1+j]);
					if (hThread[i*dim1+j])
					printf ("..................Thread  %d  launched successfully.....\n",i*dim1+j);// Create dim1*dim4 thread to calculate matrix C
					
		}

WaitForMultipleObjects(dim1 * dim4, hThread, TRUE, INFINITE);// main thread wait all worker thread, after that it prints output, BUT this function didn't work.
		
			
	
	/////////////////////////// MAtrix C//////////////////////////////////


	ofstream outFile;
	outFile.open("output.txt");
	outFile<<dim1<<" "<<dim4<<endl;
	for(int i=0;i<dim1;i++)
	{
		for(int j=0;j<dim4;j++)
		outFile<<c[i][j]<<" ";
		outFile<<endl;
		
	}
	outFile.close();
/////////////////////////////////////////////////////

		
for(int i = 0; i < dim1 * dim4; i++)
			CloseHandle(hThread[i]);
delete [] hThread;
delete [] arraypos;
delete [] pThreadID;



}


DWORD WINAPI result_rowi_columnj(LPVOID lpara)
{

	pv pos=(pv)lpara;
	c[pos->i][pos->j]=0;
	for(int k=0;k<dim2;k++)
			c[pos->i][pos->j]+=(a[pos->i][k]*b[k][pos->j]);
			return 0;
}




I don't know how to slove this problem and hope someone help me.
Thank you very much
This might help, it's a multithreaded dot product example.
http://www.cplusplus.com/forum/beginner/20372/#msg106339
Topic archived. No new replies allowed.