6 Replies - 1478 Views - Last Post: 20 November 2012 - 07:00 PM Rate Topic: -----

#1 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Multitasking? Timers and such...

Posted 19 November 2012 - 09:46 PM

So here is my issue. This is not a school project or anything but I am trying to expand my knowledge of c++ and right now I am working on a program that needs to have a timer run behind it and then when the timer is finnished move on to another part of the program. The problem is, I need the program to also be doing calculations while the timer is counting down. I googled it and from what I understood I need to learn about threading. I looked at Boost and some other things but I cant seem to get any of it running seeing as I am on Ubuntu and new to the OS. Is there anything (here on DIC or otherwise) that you guys can point me to so i can figure out what to do here? Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Multitasking? Timers and such...

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Multitasking? Timers and such...

Posted 19 November 2012 - 10:55 PM

Well, that's the hi-tech way to do it. The low tech way is to check the clock() in a loop, with this general idea:
 
#include <time.h>
#define AWAY <some reasonable value>

In your calling loop:

clock_t oldtime=clock();
clock_t newtime=oldtime;

:end of calling loop

call your function, passing oldtime and newtime as parameters, then inside that function:

In your called function loop:

newtime = clock();
if (newtime > oldtime + AWAY)
   return;
:end of called function loop




But that's C thinking, and old school at that. ;)/>/>

This post has been edited by Adak: 19 November 2012 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Multitasking? Timers and such...

Posted 19 November 2012 - 11:10 PM

I personally use timeGetTime(), which returns the system time in milliseconds. You need to include 'Windows.h' and link 'winmm.lib' to use it.

This post has been edited by Aphex19: 19 November 2012 - 11:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Re: Multitasking? Timers and such...

Posted 20 November 2012 - 01:38 AM

Aphex19 - does that work in Ubuntu?

Adak - let's say that I needed to have a variable i update every second for only as long as a timer was set for. For instance I want to add 1 to the value of i every second for only 10 seconds( I know that this is not practical by itself but for the sake of learning...) how would that help me to do that?
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Multitasking? Timers and such...

Posted 20 November 2012 - 09:25 AM

I'm using Sleep() from windows.h for this, but any sub-second timer can be a god send when you're doing a game or animation.

Whatever you can think through, you can work it out with the timers we have today. This one is the most primitive, but it still works. The robot is running low on his batteries:

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define VMAX 3
#define HMAX 3

void printRobot(char robot[][3],int x, int y);
void fillIn(int x,int y); 

int main(void) {
   int i,x,y;
   clock_t pause = 15;
   //clock_t timerNew,timerOld;
   char robot[VMAX][HMAX]={
      {'.','o','.'},
      {'-','','-'},
      {'/',' ','\\'}
      };

   printf("Say I want this 'robot', to get animated, but slow down as he travels\n\n");

   for(i=0;i<304;i++)
      printf("%c%c%c%c%c",'.','.','.','.','.');

   x=2;y=6;
   
   for(i=0;i<108;i++) {
      _gotoxy(x,y);
      printRobot(robot,x,y);
   
      Sleep(pause);
   
      fillIn(x,y);
      x+=1;  
      if(x>75){
         x=2;
         y+=VMAX;
      }
      if(i>95) {
         ++y;
         pause+=4;
      }   
      pause+=3;
      
   }
   x+=10; y+=VMAX;
   _gotoxy(x,y);
   printRobot(robot,x,y);
   _gotoxy(1,23);
   printf("\n");
   return 0;
}
void printRobot(char robot[][3],int x, int y) {
   int r,c;
   
   for(c=0,r=2;c<HMAX;c++) 
      printf("%c",robot[r][c]);
   _gotoxy(x,y-1);
   for(c=0,r=1;c<HMAX;c++)   
      printf("%c",robot[r][c]);
   _gotoxy(x,y-2);
   for(c=0,r=0;c<HMAX;c++)
      printf("%c",robot[r][c]);
}
void fillIn(int x,int y) {
   int i;
   for(i=VMAX;i>-1;i--) {
      _gotoxy(x,y-i);
      printf("...");
   }
}



Was This Post Helpful? -1
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Multitasking? Timers and such...

Posted 20 November 2012 - 10:26 AM

Adak, you shouldn't be using conio.h in the first place, let alone posting code using it to try and help someone.

To the OP, I'm currently reading The Linux Programming Interface. The only problem with the book for you is that the code is all in C. You'll still learn more about linux, and it probably would make it easier to approach multi-threading in C++. Just so you know, the new C++ standard includes a threading library: http://en.cppreferen...om/w/cpp/thread. If you browse that site, it has some of the most up-to-date information about the new standard (besides the standard itself).

I'm pretty sure that right now, the new threading library for C++ is basically a wrapper around a pthreads threading library. You can find more information by looking at the "pthreads" man page. Pthreads stands for POSIX threads which can be any threading library that adheres to the POSIX standard. I run ubuntu as well and the Pthreads library on my computer is NPTL (Native POSIX Threading Library).

POSIX is pronounced "paw six", not "poh six".

This post has been edited by vividexstance: 20 November 2012 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Multitasking? Timers and such...

Posted 20 November 2012 - 07:00 PM

I'm on 64 bit Windows 7, and conio.h functions work just fine on my 64 bit compiler.

Recently took a survey on another C programming forum (informally), and about 70% of the compilers supported conio.h.

FYI, the code I posted shows how to use a timer, with a pause. gotoxy(), just happened to be in the same program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1