473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.

May 18 '06 #1
2 1181
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.

May 18 '06 #2
Jerry Coffin wrote:
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();
}

May 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: rhmd | last post by:
Just found Python and I love it. What an elegant language! I would like to use it for various applications, but the mathematical calculations are way too slow (a million sines 8 seconds in Python...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
1
by: Dominic | last post by:
Just in case someone is interested. I came up with this solution since my previous posting. Maybe you've got some other ideas or critcism. I'll listen ;-) Ciao, Dominic P.S. Python is...
5
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
19
by: petantik | last post by:
Where can I find a good resource, on the web, that will give me a good comprehensive idea of how to write a compiler in C for C or maybe another language. http://petantik.blogsome.com - A...
0
by: Marty U. | last post by:
I have a button action that executes several tasks. I need for all those tasks to be within a transaction so I can guarantee that they all suceed or all fail. I have set the @Page...
46
by: pkirk25 | last post by:
I own a legit copy of Visual C++ 6.0 Its nice but it is 10 years old and aimed at C++. Downloaded Bloodshed Dev-C++ Very nice but the default auto-indent woupd break my heart as I depend on...
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
5
by: jhc0033 | last post by:
My experience has generally been that, for CPU-intensive tasks, the Intel compiler produces code that is about as fast as that produced by the Gnu compiler. However, on this simple Shootout...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.