delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/02/15:37:03

Date: Tue, 2 Dec 1997 15:20:37 -0500 (EST)
Message-Id: <199712022020.PAA08815@delorie.com>
To: DJGPP mailing list <djgpp AT delorie DOT com>
From: Alexander Bokovoy <bokovoy AT bspu DOT ac DOT by>
Subject: Demo for PThreads library
MIME-Version: 1.0

Hi there.

I modified example4.c from PDMLWP package to show how to work with
threads using PThreads library. Also this code works with GRX 2.2 library.
Please email me if you know other sofisticated examples for PThreads
that working with DJGPP.
Currently I cut off code that kills threads because under Windows'95 it
causes GPF. In this version demo works under plain DOS and Windows'95
without any error messages.  

Alexander Bokovoy, <bokovoy AT bspu DOT ac DOT by>
---== The Soft Age coming soon ==---

//  This demo simulates planetary motions around a common
//  sun. Each planet is handled by a process. Written by
//  Josh Turpen (snarfy AT goodnet DOT com) and
//  Sengan Short(sengan DOT short AT durham DOT ac DOT uk)
//  Very minor modifications by Paolo De Marino (pdemarin AT mbx DOT idn DOT it).
//  Modification for using with PThreads and GRX 2.2
//   by Alexander Bokovoy (bokovoy AT bspu DOT ac DOT by)
//
//  Compilation:
//  gcc -o planets.exe planets.c -lgthreads -lgrx20
//
//  Note that pthread.h must be in your include paths!!!
//
//  This file is FREEWARE - you can do whatever you want with it.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING. If not, write to the
//  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include "pthread.h"
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <grx20.h>

#define BLOB_COUNT 9  // 9 threads 
#define CENTERY 0.0
#define CENTERX 0.0

volatile int plot_count = 0;
void putpixel(int x, int y, char col);
void *blob_proc(void *arg);
void waitVBL(void);
void getNewCoords(double *x, double *y,double *xspeed,double *yspeed,double centerx,double centery);
double gravConst = 0.00025;
int main()
{
   int i;
   char answer=0;
   int ThreadCount=0;
   pthread_t ids[1024]; /* Maximum 1024 threads supported */
   GrTextOption textOption;
   pthread_init();
   
   srandom(time(0));
   GrSetMode(GR_width_height_color_graphics, 800, 600, 256);
   GrTextXY(50,20,"Planetary motions around a common sun - Each planet is handled by a process",14,0);
   GrTextXY(50,40,"Written by Josh Turpen and Sengan Short originally for LWP package.",90,0);
   GrTextXY(50,60,"Very minor modifications by Paolo De Marino.",90,0);
   GrTextXY(50,80,"PThread's Planetary Motions version by Alexander Bokovoy",90,0);

   textOption.txo_bgcolor.v=0;
   textOption.txo_direct=GR_TEXT_UP;
   textOption.txo_xalign=textOption.txo_yalign=GR_ALIGN_DEFAULT;
   textOption.txo_chrtype=GR_BYTE_TEXT;
   textOption.txo_font=&GrDefaultFont;
   textOption.txo_fgcolor.v=90;
   GrDrawString("GRX 2.2 support added by Alexander Bokovoy.",43,50,100,&textOption);

   GrTextXY(50,560,"Use <Q> to quit, <D> to decrease gravity and <U> to increase gravity",90,0);
   GrTextXY(50,580,"By pressing <N> you can create new planets",90,0);
   GrSetClipBox(100,100,790,550);

   for(i=0;i<BLOB_COUNT;i++)
      {
      pthread_create(&ids[i], NULL, blob_proc, NULL);
      ThreadCount++;
      }
   while((toupper(answer) != 'Q'))
      {
      while(!kbhit())
         {
         if(plot_count >= BLOB_COUNT)
            {
            plot_count = 0;
                        putpixel(0,0,90); 
            waitVBL();
            }  
         pthread_yield(NULL);
         }
      answer = toupper(getch());
      if (answer == 'U')
         {
         gravConst+=0.001;
         }
      if (answer == 'D')
         {
         gravConst-=0.001;
         }
      
      if ((answer == 'N')&&(ThreadCount<1024))
         {
         pthread_create(&ids[ThreadCount], NULL, blob_proc, NULL);
         ThreadCount++;
         }
      
      }

   return(0);
}

void *blob_proc(void *arg)
{
   double x, y, xspeed,yspeed;
   char col;
   x = (double) ((random() % 350) - 175);
   y = (double) ((random() % 200) - 100);
   col = random() & 0xF0;  
   xspeed = (double) ((random() & 1) - 0.5);
   yspeed = (double) ((random() & 1) - 0.5);
   while(1)
      {
      putpixel((int) x,(int) y,col);   
      plot_count++;
      pthread_yield(NULL);
      putpixel((int) x,(int) y,0);  
      /* get new X,Y position from vector function */
      getNewCoords(&x,&y,&xspeed,&yspeed,CENTERX, CENTERY);
      }
   return NULL;
}  
 
void putpixel(int x, int y, char col)
{
   if(!((x < -GrMaxX()) || (x > GrMaxX()) || (y < -GrMaxY()) || (y > GrMaxY()))) 
      {
      x+=GrMaxX()>>1;
      y+=GrMaxY()>>1;
      GrFilledCircle(x,y,6, col);
      if(col)
       {GrPlot(x-2,y,90);GrPlot(x-1,y-2,90);
        GrPlot(x-1,y-1,90);GrPlot(x-2,y-1,90);
       }
      }
}

void waitVBL(void)
{
   while(!(inportb(0x3DA) & 8));
   while(inportb(0x3DA) & 8);
}

void getNewCoords(double *x, double *y,double *xspeed,double *yspeed,double centerx,double centery)
{
        double tmp1, tmp2, tmp3, tmp4;
   double divisor1, divisor2;
        tmp1 = (centerx)-(*xspeed)-(*x);
        tmp2 = (centery)-(*yspeed)-(*y);     
   divisor1 = sqrt((tmp1*tmp1)+(tmp2*tmp2));
   divisor2 = ((centerx-(*xspeed))*(centerx-(*xspeed)))+((centery-(*yspeed))*(centery-(*yspeed)));
        if((divisor1 != 0) && (divisor2 != 0))
                {
      tmp3 = tmp1/divisor1;
      tmp4 = tmp2/divisor1;
      (*xspeed) = ((*xspeed) + tmp3*gravConst/divisor2);
      (*yspeed) = ((*yspeed) + tmp4*gravConst/divisor2);
      (*x) += ((*xspeed)+1)*cos(divisor1);
      (*y) += ((*yspeed)-1)*sin(divisor1);
                }
        else
                {
      (*x) += (*xspeed);
      (*y) += (*yspeed);
                }
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019