boost thread example

Discussion in 'C++' started by Gary Wessle, Nov 20, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    given the Boost thread example here
    http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/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;
    }
    }
    }
    Gary Wessle, Nov 20, 2006
    #1
    1. Advertising

  2. * Gary Wessle:
    > Hi
    >
    > given the Boost thread example here
    > http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/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?
    Alf P. Steinbach, Nov 20, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    "Alf P. Steinbach" <> writes:

    > * Gary Wessle:
    > > Hi
    > > given the Boost thread example here
    > > http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/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.
    Gary Wessle, Nov 20, 2006
    #3
  4. Gary Wessle

    Gary Wessle Guest

    "Alf P. Steinbach" <> writes:

    > * Gary Wessle:
    > > Hi
    > > given the Boost thread example here
    > > http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/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?
    Gary Wessle, Nov 20, 2006
    #4
  5. Gary Wessle

    dasjotre Guest

    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;
    };
    dasjotre, Nov 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,889
    Julie
    May 17, 2004
  2. Steve Knight
    Replies:
    2
    Views:
    745
    Steve Knight
    Oct 10, 2003
  3. Miroslaw Makowiecki

    boost.thread library and the thread is running

    Miroslaw Makowiecki, Aug 9, 2007, in forum: C++
    Replies:
    1
    Views:
    455
    Victor Bazarov
    Aug 9, 2007
  4. Lars Uffmann
    Replies:
    13
    Views:
    990
    Lars Uffmann
    Feb 19, 2008
  5. Christopher
    Replies:
    0
    Views:
    1,347
    Christopher
    Jun 8, 2011
Loading...

Share This Page