Thread: Threads implementations.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    Threads implementations.

    Can somebody help with this code:
    Task: Square matrix clockwise rotation using 2*N Threads 1)first quater i.e N/2 (4,7)->(11,6) ..2) second quater (11,16)->(10,13),so on and so forth...
    Thanks,
    Mozala

    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

    Code:
    #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;
      }

  2. #2
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Threads are actually os dependant, as each os handles them differently, I'm guessing from the libpthread inclusion that you are working with a posix os, so *bsd, macosx, irix or linux.....
    if you are working with a microsoft product then libpthread will cause problems, as microsoft killed posix support in win98 and has NOT brought it back yet.

    You might get better response from people if you specify which platform you are working on.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM