Boost logo

Boost Users :

Subject: Re: [Boost-users] [Threads] How to keep number of running thread constant, performing different operations
From: niXman (i.nixman_at_[hidden])
Date: 2011-09-01 08:33:12


"unsigned short int i;" - is 16bit variable.
see here: http://liveworkspace.org/code/b88123920e184955e61aee9cc0c217c6

i.e. in your code variable "i" is overflowed.

2011/9/1 Alessandro Candini <candini_at_[hidden]>:
> I've tryed implementing your suggestions with the following code:
>
> #include <iostream>
> #include </usr/local/include/boost/asio.hpp>
> #include </usr/local/include/boost/thread/thread.hpp>
> #include </usr/local/include/boost/bind.hpp>
> #include </usr/local/include/boost/date_time/posix_time/posix_time.hpp>
> #include </usr/local/include/boost/scoped_ptr.hpp>
>
> void handler1()
> {
>    unsigned short int i;
>
>    std::cout << "O--> Handler 1 <--O" << std::endl;
>    for(i=0; i<65000; i++);
>    std::cout << "X--> Handler 1 <--X" << std::endl;
> }
>
> void handler2()
> {
>    unsigned short int i;
>
>    std::cout << "O--> Handler 2 <--O" << std::endl;
>    for(i=0; i<1000; i++);
>    std::cout << "X--> Handler 2 <--X" << std::endl;
> }
> int main()
> {
>    unsigned short nProc,
>               i;
>    boost::asio::io_service io;
>    boost::scoped_ptr<boost::asio::io_service::work> work(new
> boost::asio::io_service::work(io));
>
>    boost::thread_group tg;
>    nProc = boost::thread::hardware_concurrency();
>
>    for(i=0; i<nProc; ++i)
>        tg.create_thread(boost::bind(&boost::asio::io_service::run,
> boost::ref(io)));
>
>
>    io.post(handler2);
>    io.post(handler1);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler2);
>    io.post(handler1);
>    io.post(handler2);
>
>    work.reset();
>    tg.join_all();
>
>    return 0;
> }
>
> It seems to work, but it is correct, in your opinion?
> The only problem is that it seems to never stop if I increase the value of i
> in handler1 up to 70000.
> Have you any other suggestions?
>
> --
> Alessandro Candini
>
> MEEO S.r.l.
> Via Saragat 9
> I-44122 Ferrara, Italy
> Tel: +39 0532 1861501
> Fax: +39 0532 1861637
> http://www.meeo.it
>
> ========================================
> "ATTENZIONE:le informazioni contenute in questo messaggio sono
> da considerarsi confidenziali ed il loro utilizzo è riservato unicamente
> al destinatario sopra indicato. Chi dovesse ricevere questo messaggio
> per errore è tenuto ad informare il mittente ed a rimuoverlo
> definitivamente da ogni supporto elettronico o cartaceo."
>
> "WARNING:This message contains confidential and/or proprietary
> information which may be subject to privilege or immunity and which
> is intended for use of its addressee only. Should you receive this
> message in error, you are kindly requested to inform the sender and
> to definitively remove it from any paper or electronic format."
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net