Is it possible to let the c++ compiler run tasks parallely?

C

cdrsir

maybe the title is a too big question, actually I just have a small
problem, see following codes:

*********************************************************************************
#include <iostream>
#include <windows.h>

#define OneSecond 1000

using namespace std;

int main()
{

int totalTime = 60; //seconds
cout<<"Please input a char within 60 seconds"<<endl;
char ch = NULL;
bool flag = true;
while ( flag ) {
/*task 1:
show the left time on the sreen
*/
cout<<"\rYou still have "<<totalTime--<<" seconds"
<<" to input a char:";
if(totalTime<=0) {
cout<<"\nTime Out!"<<endl;
flag = false;
}

/*take 2:
get a char from the user
*/
cin>>ch;
if(ch) flag = false;

// wait one second
Sleep(OneSecond);
}

cout<<"You have input the char "<<ch<<endl;

return 0;
}
*********************************************************************************

So I want the left time printed on the screen every seconds, at the
same time, the programm waits a input, how to do this?

I can only have seconds when the line with "cin>>ch;" commented. But
then I can not get any more input.
 
J

Jerry Coffin

cdrsir wrote:

[ ... ]
#include <iostream>
#include <windows.h>

[ ... ]
So I want the left time printed on the screen every seconds, at the
same time, the programm waits a input, how to do this?

I can only have seconds when the line with "cin>>ch;" commented. But
then I can not get any more input.

There's no standard (portable) method for doing this so it's not really
topical here. Since you're apparently a) using Windows, and b) fine
with writing non-portable code, you probably want to ask about
_beginthread somewhere that it's topical. The most obvious choice is
probably comp.os.ms-windows.programmer.win32. If memory serves, there's
also a newsgroup dedicated to multithreading, but what you're asking
for at the moment is sufficiently general that it probably won't gain
you a lot.
 
K

kwikius

Jerry said:
cdrsir wrote:

[ ... ]
#include <iostream>
#include <windows.h>

[ ... ]
So I want the left time printed on the screen every seconds, at the
same time, the programm waits a input, how to do this?

I can only have seconds when the line with "cin>>ch;" commented. But
then I can not get any more input.

There's no standard (portable) method for doing this so it's not really
topical here.

FWIW I believe that Boost.Threads is well regarded, widely available
and portable as well as being discussed in the context of concurrent
programming for C++ by the standardisation committee, so IMO it is
quite acceptable to discuss in the context of the C++ language
newsgroup:
http://www.boost.org/doc/html/threads.html

Below is a program that demonstrates the sort of functionality
described by the OP. To run it you will need to download and build the
Boost.Threads threads library in the boost distro. http://www.boost.org

regards
Andy Little

--------------------

#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string>
/*
thread demo. Give user 7 seconds to enter a string
*/
struct call_back_timer{
typedef boost::posix_time::time_duration time_duration;
typedef boost::posix_time::ptime ptime;
typedef boost::posix_time::second_clock second_clock;
static void reset_flag()
{
flag = false;
}
static void set_flag()
{
flag = true;
}
call_back_timer(
time_duration const & p, // interval
int c, // number of cycles to repeat
void(*f1)(int current,int tot ), // per cycle function with
count arg
void(*f2)() // end function
)
:period(p),count(c),pf1(f1),pf2(f2){}
private:
int count;
static bool flag;
const time_duration period;
void(*pf1)(int n,int count);
void(*pf2)();
public:
void operator()()
{
ptime t = second_clock::local_time();
for (int n = 0; n <= count;++n){
t += period;
while( second_clock::local_time() < t)
{
if (!flag){
return;
}
}
pf1(n,count);
}
pf2();
}
};

bool call_back_timer::flag = true;

void print_time(int n,int count)
{
if ( (count -n) > 0){
std::cout << "you have "<< (count - n) << " seconds left\n";
}
else {
std::cout << "\nSorry... too late. Press return to quit\n";
}
}

struct user_input{
private:
static bool flag;
void(*pf)();
public:
static void set_flag()
{
flag = true;
}
static void reset_flag()
{
flag = false;
}
user_input(void(*f)()):pf(f){}
void operator()()
{
std::cout << " Enter a string and press return: ";
std::string str;
getline(std::cin, str);
if (flag){
std::cout << "\nyou entered '" << str << "'\n";
}
pf();
}
};

bool user_input::flag = true;

int main()
{
call_back_timer timer(
boost::posix_time::seconds(1),
5,
print_time,
user_input::reset_flag
);
timer.set_flag();
user_input user(call_back_timer::reset_flag);
user.set_flag();
boost::thread_group thrds;
thrds.create_thread(user);
thrds.create_thread(timer);
thrds.join_all();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top