<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Threads\Process' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Threads\Process' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Tue, 18 Jun 2013 16:11:41 -0700</pubDate>
    <lastBuildDate>Tue, 18 Jun 2013 16:11:41 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Threads\Process</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/336784/336784/threadsprocess/</link>
      <description>Can somebody help me out?&lt;br /&gt;
--------------------------------------&lt;br /&gt;
Input: Square matrix A of nxn integers, where n is even.&lt;br /&gt;
&lt;br /&gt;
Rotate the elements of both diagonals in the clock direction.&lt;br /&gt;
&lt;br /&gt;
          Input matrix 4x4&lt;br /&gt;
&lt;br /&gt;
                     1  2  3  4 &lt;br /&gt;
                     5  6  7  8&lt;br /&gt;
                     9 10 11 12&lt;br /&gt;
                    13 14 15 16 &lt;br /&gt;
&lt;br /&gt;
          Matrix 4x4 after rotation:&lt;br /&gt;
&lt;br /&gt;
                    13  2  3  1 &lt;br /&gt;
                     5 10  6  8&lt;br /&gt;
                     9 11  7 12&lt;br /&gt;
                    16 14 15  4 &lt;br /&gt;
&lt;br /&gt;
Rotation is executed by 2n threads/processes.&lt;br /&gt;
One quarter n/2 of threads/processes can&lt;br /&gt;
read data from the upper half of the &lt;br /&gt;
main diagonal (e.g. 4,7) and write them&lt;br /&gt;
to the lower half of the secondary diagonal (e.g. 11,16).&lt;br /&gt;
The second quarter of threads/processes can read data from&lt;br /&gt;
the lower half of the secondary diagonal (e.g. 11,16) and&lt;br /&gt;
write them to the lower half of the main diagonal (e.g. 10,13).&lt;br /&gt;
The third quarter of threads/processes can read data from the &lt;br /&gt;
lower half of the main diagonal (e.g. 10,13) and write them to &lt;br /&gt;
the upper half of the secondary diagonal (e.g. 1,6). The last&lt;br /&gt;
quarter of threads/processes can read data from the upper half of&lt;br /&gt;
the secondary diagonal (e.g. 1,6) and write them to the upper &lt;br /&gt;
half of the main diagonal (e.g. 4,7).&lt;br /&gt;
&lt;br /&gt;
Output: Input and output matrix.&lt;br /&gt;
&lt;br /&gt;
-------------------------------&lt;br /&gt;
I've tried to begin the code so just pick out some mistakes and correct so that it can work..Thanks&lt;br /&gt;
----------------------------------&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
#define MAX_THREADS  100&lt;br /&gt;
&lt;br /&gt;
int Elmnt[10000];&lt;br /&gt;
int **tmp;&lt;br /&gt;
int m, n, Elmntsize, t, k, kAbs;&lt;br /&gt;
pthread_mutex_t mutex;&lt;br /&gt;
pthread_cond_t cond[MAX_THREADS];&lt;br /&gt;
&lt;br /&gt;
/////////////////////////////////////&lt;br /&gt;
void Print () {&lt;br /&gt;
  int i;&lt;br /&gt;
  for(i=0; i&amp;lt;Elmntsize; i++){&lt;br /&gt;
    if((i+1)%n == 0 &amp;amp;&amp;amp; i != 0) {&lt;br /&gt;
       printf("%d\n",Elmnt[i]); &lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
     printf("%d ",Elmnt[i]);  &lt;br /&gt;
  }&lt;br /&gt;
  printf("\n");  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 ////////////////////////////////&lt;br /&gt;
//       read file     //&lt;br /&gt;
int  Readfile(char *fname){&lt;br /&gt;
  FILE *f;&lt;br /&gt;
  int i;&lt;br /&gt;
  f = fopen(fname, "r");&lt;br /&gt;
  if (f == NULL) {&lt;br /&gt;
    fprintf(stderr, "file \"%s\" cannot open.\n", fname);&lt;br /&gt;
    return -1;&lt;br /&gt;
  }&lt;br /&gt;
  fscanf(f,"%d %d",&amp;amp;m, &amp;amp;n);&lt;br /&gt;
  Elmntsize = m*n;&lt;br /&gt;
  for (i=0; i &amp;lt; Elmntsize; i++){&lt;br /&gt;
    fscanf(f,"%d",&amp;amp;Elmnt[i]);&lt;br /&gt;
  }   &lt;br /&gt;
  fclose(f);&lt;br /&gt;
  return 0;  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //////////////////////////////////////////////////
//////&lt;br /&gt;
//       Find the average number of threads  //&lt;br /&gt;
int avgCntRwTh(){&lt;br /&gt;
  if(m == t) return 1;&lt;br /&gt;
  if(t == 1) return m;&lt;br /&gt;
  return floor((float)m/t+0.5);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //////////////////////////////////////////////////
///////////////////////&lt;br /&gt;
//   SHift of auxiliary tmp to new position(for right rotation)      //&lt;br /&gt;
void tmpShiftRR(int id, int iL){&lt;br /&gt;
  int i, preID;  &lt;br /&gt;
  if(id == 0) preID = t-1;&lt;br /&gt;
    else preID = id-1;&lt;br /&gt;
  for (i=0; i&amp;lt;kAbs; i++){&lt;br /&gt;
    Elmnt[iL+i]= tmp[preID][kAbs-1-i];&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
 ----------------------------------------&lt;br /&gt;
//       right rotation function      //&lt;br /&gt;
----------------------------------------&lt;br /&gt;
void RotationR(int id, int iL, int iR){&lt;br /&gt;
  int i;  &lt;br /&gt;
  for (i=0; i&amp;lt;kAbs; i++){&lt;br /&gt;
    tmp[id][i] = Elmnt[iR-i];&lt;br /&gt;
  }  &lt;br /&gt;
  for (i=iR-kAbs; i&amp;gt;=iL; i--){&lt;br /&gt;
Elmnt[i+kAbs] = Elmnt[i];&lt;br /&gt;
  }  &lt;br /&gt;
  if(id == t-1) {&lt;br /&gt;
    tmpShift(id, iL);&lt;br /&gt;
    pthread_cond_signal(&amp;amp;cond[0]);    &lt;br /&gt;
  } &lt;br /&gt;
  else { &lt;br /&gt;
    pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex); &lt;br /&gt;
    tmpShiftRR(id, iL);&lt;br /&gt;
    pthread_cond_signal(&amp;amp;cond[id+1]);&lt;br /&gt;
  }  &lt;br /&gt;
}&lt;br /&gt;
---------------------------------------&lt;br /&gt;
//       Left rotation function      //&lt;br /&gt;
---------------------------------------&lt;br /&gt;
void RotationL(int id, int iL, int iR){&lt;br /&gt;
  int i;  &lt;br /&gt;
  for (i=0; i&amp;lt;kAbs; i++){ tmp[id][i] = Elmnt[iL+i]; }  &lt;br /&gt;
  for (i=iL; i&amp;lt;=iR; i++){ Elmnt[i] = Elmnt[i+kAbs];  }  &lt;br /&gt;
  if(id == t-1) {&lt;br /&gt;
    tmpShiftRL(id, iR);  &lt;br /&gt;
  } &lt;br /&gt;
  else { &lt;br /&gt;
    pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex); &lt;br /&gt;
    tmpShiftRL(id, iR);&lt;br /&gt;
  }  &lt;br /&gt;
  if(id != 0) pthread_cond_signal(&amp;amp;cond[id-1]);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 --------------------&lt;br /&gt;
//  rotate      //  &lt;br /&gt;
--------------------&lt;br /&gt;
void *rotate(void *threadid){&lt;br /&gt;
  int id, cntR, pocR, konR, iL, iR;&lt;br /&gt;
  id = (int)threadid;&lt;br /&gt;
  printf("The working Thread is number: %d\n", id+1);&lt;br /&gt;
  cntR = avgCntRwTh();                         // To find out how many rows to be worked on average amount&lt;br /&gt;
  pocR = (id)*cntR+1;                          // To provide rows where to begin rotation&lt;br /&gt;
  if(id == t-1) {&lt;br /&gt;
    cntR = m - (t-1)*cntR;&lt;br /&gt;
    sleep(1);&lt;br /&gt;
  }     // For the Last thread of the remaining rows.&lt;br /&gt;
  konR = pocR+cntR -1;&lt;br /&gt;
  printf("     - This Threa is working with rows: %d-%d\n", pocR, konR);&lt;br /&gt;
  //&lt;br /&gt;
  iL = (pocR-1)*n;           //First element of respective Thread&lt;br /&gt;
  iR = konR*n -1;            // Last element &lt;br /&gt;
  //&lt;br /&gt;
  pthread_mutex_lock(&amp;amp;mutex);  &lt;br /&gt;
  //&lt;br /&gt;
  if(k &amp;lt; 0) rotationL(id, iL, iR);              // left&lt;br /&gt;
    else    rotationR(id, iL, iR);              // right&lt;br /&gt;
  // &lt;br /&gt;
  pthread_mutex_unlock(&amp;amp;mutex); &lt;br /&gt;
  //&lt;br /&gt;
  pthread_exit(NULL);  &lt;br /&gt;
  return NULL;  &lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
----------------------------------&lt;br /&gt;
//             main            //&lt;br /&gt;
-----------------------------------&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  char File;&lt;br /&gt;
  int i, j, kN;&lt;br /&gt;
  pthread_t ths[MAX_THREADS];&lt;br /&gt;
 &lt;br /&gt;
  do {     &lt;br /&gt;
       // The reading of data from the file&lt;br /&gt;
    do {&lt;br /&gt;
      printf("\nType the name of the file (pro ukonceni programu: -1): ");&lt;br /&gt;
      scanf("%s", &amp;amp;File);&lt;br /&gt;
      if(atoi(&amp;amp;File) == -1) exit(0);&lt;br /&gt;
    } &lt;br /&gt;
    while(ReadFile(&amp;amp;File) != 0 );&lt;br /&gt;
    printf("\nThe file was read:\n");&lt;br /&gt;
    Print();&lt;br /&gt;
        // The given number of Thread  &lt;br /&gt;
    do {&lt;br /&gt;
      printf("Type total number of Threads(1-%d): ", m);&lt;br /&gt;
      scanf("%d", &amp;amp;t);&lt;br /&gt;
      if(t == -1) exit(0);&lt;br /&gt;
    } &lt;br /&gt;
    while(t &amp;lt; 1 || t &amp;gt; m); &lt;br /&gt;
    if(t&amp;gt;MAX_THREADS) t = MAX_THREADS;&lt;br /&gt;
   &lt;br /&gt;
       // The number of positions to be rotated  &lt;br /&gt;
    printf("Enter number of positions to be rotated: ");&lt;br /&gt;
    scanf("%d", &amp;amp;k);&lt;br /&gt;
    kAbs = abs(k);&lt;br /&gt;
    if(kAbs &amp;gt; ElmntSize-1 &amp;amp;&amp;amp; k &amp;gt;= 0) kAbs = kAbs %ElmntSize;      // oriznuti kroku na velikost pole&lt;br /&gt;
    if(kAbs &amp;gt; ElmntSize &amp;amp;&amp;amp; k &amp;lt; 0) kAbs = kAbs %ElmntSize;   &lt;br /&gt;
    kN = kAbs /n;                                                 // pocet presahu na dalsi radek&lt;br /&gt;
    kAbs = kAbs %n;                                               // total number of positions during the first pass                                          &lt;br /&gt;
      // alokovani pomocneho 2d pole pro pomocne presuny   &lt;br /&gt;
    tmp = (int **)malloc(t*sizeof(int*));  &lt;br /&gt;
    for(i=0; i&amp;lt;t; i++)               &lt;br /&gt;
      tmp[i]=(int *)malloc(n*sizeof(int));&lt;br /&gt;
&lt;br /&gt;
      //  Created Thread&lt;br /&gt;
    pthread_mutex_init(&amp;amp;mutex, NULL);&lt;br /&gt;
    for (j=0; j&amp;lt;=kN; j++){&lt;br /&gt;
      if (kAbs == 0) { kAbs = n; continue; }&lt;br /&gt;
      for(i=0; i&amp;lt;t; i++) {&lt;br /&gt;
        if(pthread_cond_init (&amp;amp;cond[i], NULL)) {&lt;br /&gt;
          printf("ERROR:During pthread_cond_init() - number %d\n)", i); exit(-1); &lt;br /&gt;
        }&lt;br /&gt;
        if(pthread_create(&amp;amp;ths[i], NULL, rotate, (void *) i)){&lt;br /&gt;
          printf("ERROR: During thread_create() - number %d\n)", i); exit(-1); &lt;br /&gt;
        }        &lt;br /&gt;
        usleep(500000);&lt;br /&gt;
      }    &lt;br /&gt;
      for(i=0; i&amp;lt;t; i++){ &lt;br /&gt;
        pthread_join(ths[i], NULL); &lt;br /&gt;
        pthread_cond_destroy(&amp;amp;cond[i]);&lt;br /&gt;
      }&lt;br /&gt;
      kAbs = n;&lt;br /&gt;
    }  &lt;br /&gt;
    pthread_mutex_destroy(&amp;amp;mutex);&lt;br /&gt;
    free(tmp);&lt;br /&gt;
        // Rotated array to be printed&lt;br /&gt;
    printf("\n\nRotated Array:\n");&lt;br /&gt;
    Print();&lt;br /&gt;
    printf("--- --- ---\n\n");&lt;br /&gt;
    sleep(2);&lt;br /&gt;
  } while (1);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/336784/336784/threadsprocess/</guid>
      <pubDate>Thu, 11 May 2006 08:45:35 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: Threads\Process</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/336784/336798/re-threadsprocess/#336798</link>
      <description>: Can somebody help me out?&lt;br /&gt;
Perhaps, when you'd have used code tags (i.e. &amp;#91;code] and &amp;#91;/code]). &lt;br /&gt;
Perhaps until then,&lt;br /&gt;
bilderbikkel&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/336784/336798/re-threadsprocess/#336798</guid>
      <pubDate>Thu, 11 May 2006 11:54:35 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: Threads\Process</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/336784/336820/re-threadsprocess/#336820</link>
      <description>: : Can somebody help me out?&lt;br /&gt;
: Perhaps, when you'd have used code tags (i.e. &amp;#91;code] and &amp;#91;/code]). &lt;br /&gt;
: Perhaps until then,&lt;br /&gt;
: bilderbikkel&lt;br /&gt;
: Ok i'd like to find out how to interchange main and secondary diagonal  or processing them by threads according to 2n-&amp;gt;n/2-&amp;gt;... like an exercise say above&lt;br /&gt;
:&lt;pre class="sourcecode"&gt;
 //////////////////////////////////////////////////
///////////////////////
// SHift of auxiliary tmp to new position(for right rotation) //
void tmpShiftRR(int id, int iL){
int i, preID;
if(id == 0) preID = t-1;
else preID = id-1;
for (i=0; i&amp;lt;kAbs; i++){
Elmnt[iL+i]= tmp[preID][kAbs-1-i];
}
}
----------------------------------------
// right rotation function //
----------------------------------------
void RotationR(int id, int iL, int iR){
int i;
for (i=0; i&amp;lt;kAbs; i++){
tmp[id][i] = Elmnt[iR-i];
}
for (i=iR-kAbs; i&amp;gt;=iL; i--){
Elmnt[i+kAbs] = Elmnt[i];
}
if(id == t-1) {
tmpShift(id, iL);
pthread_cond_signal(&amp;amp;cond[0]);
}
else {
pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex);
tmpShiftRR(id, iL);
pthread_cond_signal(&amp;amp;cond[id+1]);
}
}
---------------------------------------
// Left rotation function //
---------------------------------------
void RotationL(int id, int iL, int iR){
int i;
for (i=0; i&amp;lt;kAbs; i++){ tmp[id][i] = Elmnt[iL+i]; }
for (i=iL; i&amp;lt;=iR; i++){ Elmnt[i] = Elmnt[i+kAbs]; }
if(id == t-1) {
tmpShiftRL(id, iR);
}
else {
pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex);
tmpShiftRL(id, iR);
}
if(id != 0) pthread_cond_signal(&amp;amp;cond[id-1]);
}


--------------------
// rotate //
--------------------
void *rotate(void *threadid){
int id, cntR, pocR, konR, iL, iR;
id = (int)threadid;
printf("The working Thread is number: %d\n", id+1);
cntR = avgCntRwTh(); // To find out how many rows to be worked on average amount
pocR = (id)*cntR+1; // To provide rows where to begin rotation
if(id == t-1) {
cntR = m - (t-1)*cntR;
sleep(1);
} // For the Last thread of the remaining rows.
konR = pocR+cntR -1;
printf(" - This Threa is working with rows: %d-%d\n", pocR, konR);
//
iL = (pocR-1)*n; //First element of respective Thread
iR = konR*n -1; // Last element
//
pthread_mutex_lock(&amp;amp;mutex);
//
if(k &amp;lt; 0) rotationL(id, iL, iR); // left
else rotationR(id, iL, iR); // right
//
pthread_mutex_unlock(&amp;amp;mutex);
//
pthread_exit(NULL);
return NULL;
}


&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/336784/336820/re-threadsprocess/#336820</guid>
      <pubDate>Thu, 11 May 2006 15:47:53 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: Threads\Process</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/336784/336887/re-threadsprocess/#336887</link>
      <description>: : : Can somebody help me out?&lt;br /&gt;
: : Perhaps, when you'd have used code tags (i.e. &amp;#91;code] and &amp;#91;/code]). &lt;br /&gt;
: : Perhaps until then,&lt;br /&gt;
: : bilderbikkel&lt;br /&gt;
: : Ok i'd like to find out how to interchange main and secondary diagonal  or processing them by threads according to 2n-&amp;gt;n/2-&amp;gt;... like an exercise say above&lt;br /&gt;
&lt;br /&gt;
And a correct indentation of course!&lt;br /&gt;
Instead of &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void f()
{
for(int x=0; x&amp;lt;100;++x)
{
for(int y=0; y&amp;lt;100;++y)
{
for(int z=0; z&amp;lt;100;++z)
{
//Something
}
}
}
}
&lt;/pre&gt;&lt;br /&gt;
use&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void f()
{
  for(int x=0; x&amp;lt;100;++x)
  {
    for(int y=0; y&amp;lt;100;++y)
    {
      for(int z=0; z&amp;lt;100;++z)
      {
        //Something
      }
    }
  }
}
&lt;/pre&gt;&lt;br /&gt;
It's not that I don't want to answer your question, I just don't want to do the indenting for you.&lt;br /&gt;
See ya,&lt;br /&gt;
bilderbikkel&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/336784/336887/re-threadsprocess/#336887</guid>
      <pubDate>Fri, 12 May 2006 05:14:52 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: Threads\Process</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/336784/337043/re-threadsprocess/#337043</link>
      <description>: : : : Can somebody help me out?&lt;br /&gt;
: : : Perhaps, when you'd have used code tags (i.e. &amp;#91;code] and &amp;#91;/code]). &lt;br /&gt;
: : : Perhaps until then,&lt;br /&gt;
: : : bilderbikkel&lt;br /&gt;
: : : Ok i'd like to find out how to interchange main and secondary diagonal  or processing them by threads according to 2n-&amp;gt;n/2-&amp;gt;... like an exercise say above&lt;br /&gt;
: &lt;br /&gt;
: And a correct indentation of course!&lt;br /&gt;
: Instead of &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: void f()
: {
: for(int x=0; x&amp;lt;100;++x)
: {
: for(int y=0; y&amp;lt;100;++y)
: {
: for(int z=0; z&amp;lt;100;++z)
: {
: //Something
: }
: }
: }
: }
: &lt;/pre&gt;&lt;br /&gt;
: use&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: void f()
: {
:   for(int x=0; x&amp;lt;100;++x)
:   {
:     for(int y=0; y&amp;lt;100;++y)
:     {
:       for(int z=0; z&amp;lt;100;++z)
:       {
:         //Something
:       }
:     }
:   }
: }
: &lt;/pre&gt;&lt;br /&gt;
: It's not that I don't want to answer your question, I just don't want to do the indenting for you.&lt;br /&gt;
: See ya,&lt;br /&gt;
: bilderbikkel&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
I hope this looks much better than b4,sorry for that ,at least now i know how to post a Thread next time.&lt;br /&gt;
Thanks.&lt;br /&gt;
Mozala&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
----------------------------------------------------------------
// SHift of auxiliary tmp to new position(for right rotation) //
-----------------------------------------------------------------
void tmpShiftRR(int id, int iL)

  {
    int i, preID;
    if(id == 0) preID = t-1;
    else preID = id-1;
     for (i=0; i&amp;lt;kAbs; i++)
      {
        Elmnt[iL+i]= tmp[preID][kAbs-1-i];
      }
 }
----------------------------------------
// right rotation function //
----------------------------------------
void RotationR(int id, int iL, int iR)

{
   int i;
     for (i=0; i&amp;lt;kAbs; i++)
    {
         tmp[id][i] = Elmnt[iR-i];
    }
     for (i=iR-kAbs; i&amp;gt;=iL; i--)
       {
         Elmnt[i+kAbs] = Elmnt[i];
       }
    if(id == t-1)
  {
   tmpShift(id, iL);
    pthread_cond_signal(&amp;amp;cond[0]);
  }
    else
   {
        pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex);
           tmpShiftRR(id, iL);
           pthread_cond_signal(&amp;amp;cond[id+1]);
   }
}
---------------------------------------
// Left rotation function //
---------------------------------------
void RotationL(int id, int iL, int iR)

{
    int i;
    for (i=0; i&amp;lt;kAbs; i++)
     { 
       tmp[id][i] = Elmnt[iL+i];
     }
      for (i=iL; i&amp;lt;=iR; i++)
    {  
      Elmnt[i] = Elmnt[i+kAbs];  
    }
    if(id == t-1) 
     {
        tmpShiftRL(id, iR);
     }
     else 
  {
      pthread_cond_wait(&amp;amp;cond[id], &amp;amp;mutex);
      tmpShiftRL(id, iR);
  }
   if(id != 0) pthread_cond_signal(&amp;amp;cond[id-1]);
}


--------------------
// rotate //
--------------------
void *rotate(void *threadid)

{
    int id, cntR, pocR, konR, iL, iR;
     id = (int)threadid;
     printf("The working Thread is number: %d\n", id+1);
   cntR = avgCntRwTh();         // To find out how many rows to be worked on average amount
    pocR = (id)*cntR+1;          // To provide rows where to begin rotation
   if(id == t-1) 
   {
     cntR = m - (t-1)*cntR;
     sleep(1);
    }                             // For the Last thread of the remaining rows.
                konR = pocR+cntR -1;
        printf(" - This Thread is working with rows: %d-%d\n", pocR, konR);

       iL = (pocR-1)*n;     //First element of respective Thread
       iR = konR*n -1;      // Last element

     pthread_mutex_lock(&amp;amp;mutex);

     if(k &amp;lt; 0) rotationL(id, iL, iR);        // left
       else rotationR(id, iL, iR);           // right

       pthread_mutex_unlock(&amp;amp;mutex);

           pthread_exit(NULL);
        return NULL;
}&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/336784/337043/re-threadsprocess/#337043</guid>
      <pubDate>Sun, 14 May 2006 17:10:19 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>