473,287 Members | 2,682 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,287 software developers and data experts.

boost thread example


Hi

given the Boost thread example here
http://www-eleves-isia.cma.fr/docume...ple/thread.cpp

the code below attempts to run the example
thread while giving the user a prompt for data input.
I am failing to get the prompt while the thread is running, what am I
doing wrong?

thanks

************************************************** **************
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>
using namespace std;

struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;

boost::thread::sleep(xt);

std::cout << "alarm sounded..." << std::endl;
}

int m_secs;
};

int main(){
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
for( ;; ) {
cout << "type a number please:" << endl;
int opt;
cin >opt;
switch( opt ) {
case ( 1 ): {
thread_alarm alarm(secs);
boost::thread thrd(alarm);
thrd.join();
break;
}
case (99 ): cout << "Exiting ...\n"; break;
default: cout << "you typed " << opt << endl;
}
}
}
Nov 20 '06 #1
4 42594
* Gary Wessle:
Hi

given the Boost thread example here
http://www-eleves-isia.cma.fr/docume...ple/thread.cpp

the code below attempts to run the example
thread while giving the user a prompt for data input.
I am failing to get the prompt while the thread is running, what am I
doing wrong?

thanks

************************************************** **************
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>
using namespace std;

struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;

boost::thread::sleep(xt);

std::cout << "alarm sounded..." << std::endl;
}

int m_secs;
};

int main(){
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
for( ;; ) {
cout << "type a number please:" << endl;
int opt;
cin >opt;
switch( opt ) {
case ( 1 ): {
thread_alarm alarm(secs);
boost::thread thrd(alarm);
thrd.join();
break;
}
case (99 ): cout << "Exiting ...\n"; break;
default: cout << "you typed " << opt << endl;
}
}
}
The code above says to wait for the thread to finish before going on
with issuing the next prompt. From your description it seems the
program is doing exactly what the code says it should do. Btw., beware
of doing unsynchronized i/o in threads.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 20 '06 #2
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
Hi
given the Boost thread example here
http://www-eleves-isia.cma.fr/docume...ple/thread.cpp
the code below attempts to run the example
thread while giving the user a prompt for data input. I am failing
to get the prompt while the thread is running, what am I
doing wrong?
thanks
************************************************** **************
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>
using namespace std;
struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;
boost::thread::sleep(xt);
std::cout << "alarm sounded..." << std::endl;
}
int m_secs;
};
int main(){
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
for( ;; ) {
cout << "type a number please:" << endl;
int opt;
cin >opt;
switch( opt ) {
case ( 1 ): {
thread_alarm alarm(secs);
boost::thread thrd(alarm);
thrd.join();
break;
}
case (99 ): cout << "Exiting ...\n"; break;
default: cout << "you typed " << opt << endl;
}
}
}

The code above says to wait for the thread to finish before going on
with issuing the next prompt. From your description it seems the
program is doing exactly what the code says it should do. Btw.,
beware of doing unsynchronized i/o in threads.
I need the code to issue the next prompt without waiting for the
thread to finish so that the user can select another "opt" while the
first thread job is still going.
Nov 20 '06 #3
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
Hi
given the Boost thread example here
http://www-eleves-isia.cma.fr/docume...ple/thread.cpp
the code below attempts to run the example
thread while giving the user a prompt for data input. I am failing
to get the prompt while the thread is running, what am I
doing wrong?
thanks
************************************************** **************
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>
using namespace std;
struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;
boost::thread::sleep(xt);
std::cout << "alarm sounded..." << std::endl;
}
int m_secs;
};
int main(){
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
for( ;; ) {
cout << "type a number please:" << endl;
int opt;
cin >opt;
switch( opt ) {
case ( 1 ): {
thread_alarm alarm(secs);
boost::thread thrd(alarm);
thrd.join();
break;
}
case (99 ): cout << "Exiting ...\n"; break;
default: cout << "you typed " << opt << endl;
}
}
}

The code above says to wait for the thread to finish before going on
with issuing the next prompt. From your description it seems the
program is doing exactly what the code says it should do. Btw.,
beware of doing unsynchronized i/o in threads.
if I comment out the line
thrd.join();
it gives me the desired effect, is that the correct way to do it?
Nov 20 '06 #4

Gary Wessle wrote:
Hi
struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;

boost::thread::sleep(xt);
cout is not synchronized, it might
overlap with main thread's output

<snip>
std::cout << "alarm sounded..." << std::endl;
}
<snip>
switch( opt ) {
case ( 1 ): {
thread_alarm alarm(secs);
boost::thread thrd(alarm);
join will wait till thrd terminates, that doesn't seem
to be what you want
thrd.join();
break;
}
case (99 ): cout << "Exiting ...\n"; break;
default: cout << "you typed " << opt << endl;
I believe you want the alarm to sound if the user
takes more than 5 seconds to type

In that case you will need to cancel the alarm
here. your current implementation of thread_alarm
doesn't provide such functionality

maybe looping around a flag for a fraction
of alarm time and then changing the flag
to exit the loop would do.

general idea:

struct thread_alarm
{
bool loop_;
thread_alarm(int secs) : m_secs(secs) , loop_(true){ }

void cancel(){ loop_= false; }

void operator()()
{
while(loop_ && m_secs)
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
// sleep a second at a time
xt.sec += 1;
--m_secs;
boost::thread::sleep(xt);
}
if(loop_)
std::cout << "alarm sounded..." << std::endl;
}

int m_secs;
};

Nov 20 '06 #5

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

Similar topics

3
by: dingounan | last post by:
how to compile boost.thread,and set it? I have trouble. platform: windows XP compiler: Borland C++ Compiler 5.5.1 commandline tools thanks.
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
4
by: Lighter | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; using namespace boost; void hello() { cout << "Hello world, I'm a thread!" << endl; }
5
by: linyanhung | last post by:
I used a boost multi thread in VS 2005 on a Duo Core PC, and made a two thread process. The code is something like this: #include <boost/thread/thread.hpp> void fun1() { //do something
3
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void preform();
2
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio> #include <boost/thread/thread.hpp> class A {
2
by: Hans Mull | last post by:
Hi! I'm experimenting with boost::thread. I have a GUI application with a slot function: void someFunction(){...} void Frame::OnOkButtonClick(...) { switch(someInteger) { case 0: break
3
by: Lars Uffmann | last post by:
I have this wxWidgets OnButtonClick event handler, that apparently holds a lock on all widgets in my form, but this event handler is supposed to end a thread in the background - while that thread...
1
by: Boogie | last post by:
Hi, A specific problem arises when I try to use boost::thread 1.35.0 with Borland Turbo 2006. Code below compiles but when run it throws (in commented line - thread creation): "assertion...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.