Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Threads implementations


  • Please log in to reply

#1
mozala

mozala

    New Member

  • Member
  • Pip
  • 8 posts
Can somebody help me out with this sample program ,its not really working:
the task was to rotate square matrix clockwise using threads ,if "N" being number of either columns or rows then there will be 2n threads accomplishing this task
Example:

Input matrix 4x4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Matrix 4x4 after rotation:

13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4

have a look and tell me if u can at least have an idea how to get along with it
Thanks,
Mozala


#include <pthread.h>
#include <iostream>
#define MAX_ROW 10
#define MAX_COL 10
using namespace std;

struct matrix_index_type
{
   int	row;
   int  col;
};

struct matrix_index_type matrix_index[MAX_ROW][MAX_COL];
int aux[MAX_COL];
int matrix[MAX_ROW][MAX_COL];

/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][MAX_COL]) 
{
	int i,j;	 
	for(i = 0; i < MAX_ROW; i ++)
	{
	  for(j = 0; j < MAX_COL; j ++)
		{
		  cin<<matrix[i][j];
 
		}
	}
}

/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][MAX_COL])
{
	int i,j;
	for(i = 0; i < MAX_ROW; i ++)
		{
		  for(j = 0; j < MAX_COL; j ++)
		  {
			  cout<<matrix[i][j]<<endl;
			
		  }
	   cout<<"\n";
	}
	cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
	int i,j,x;
	struct matrix_index_type *pindex;
	pindex = (struct matrix_index_type *) index;
	i = pindex->row;
	j = pindex->col;

	aux[MAX_COL]=matrix[i][j];
	pthread_mutex_unlock(MD[j])
	   
	   aux[MAX_COL]=matrix[i][j];
	   pthread_mutex_unlock(MD[i]);
	   pthread_mutex_lock(SD[j])
		 
	for (x=0;x<MAX_COL;x++)
	{

		pthread_exit(NULL);
	}
}
	
/*-------------------MAIN PROGRAM-----------------*/
int main()

{

	int Imatrix[][MAX_COL]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
 	int i, j;
	int index[MAX_ROW][MAX_COL];
	pthread_t ptd[MAX_ROW][MAX_COL];
	
	read_matrix(Imatrix);
	cout<<"Input matrix is:\n"<<endl;
	output_matrix(Imatrix);
	
	for(i = 0; i < MAX_ROW; i ++)
	{
		for(j = 0; j < MAX_COL; j ++)
		{
			matrix_index[i][j].row = i;
			matrix_index[i][j].col = j;
			pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
		}

	}
	for(i = 0; i < MAX_ROW; i ++)
	{
		for(j = 0; j < MAX_COL; j ++)
		{
			pthread_join( ptd[i][j], NULL);
			
		}

	}
		cout<<"Rotated matrix is:\n";
		output_matrix(Imatrix);
} 
	   pthread_mutex_destroy(&mutex);
	   pthread_exit(NULL);
	   return 0;
  }

[/QUOTE]
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP