Like Tree24Likes

my questions on Boost

This is a discussion on my questions on Boost within the C++ Programming forums, part of the General Programming Boards category; Hello all . Im starting to learn boost , and now im on threads. and for the time being , ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    my questions on Boost

    Hello all .
    Im starting to learn boost , and now im on threads.
    and for the time being , im trying to use two threads to sum lets say 1000000 numbers as an example , i want these threads iterate through a loop , one after another , i mean if thread one counted to 3 , thread 2 continues with 4 and so on ,how can i share that variable with these threads ? how can i do that ?
    i already tried to use mutex .
    Code:
    //in the name of GOD
    #define BOOST_THREAD_USE_LIB
    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/thread.hpp>
    
    
    boost::uint64_t  i = 0;
    boost::uint64_t  sum=0;
    boost::mutex mutex;
    
    void IteratorFunc()
    {
         mutex.lock();
        for (i ; i<1000000000 ; i++)
            {
                sum+=i;
            }
            mutex.unlock();
    }
    
    using namespace std;
    
    int main()
    {
        boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
        boost::thread thread(IteratorFunc);
        boost::thread thread2(IteratorFunc);
    
        boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
         cout << "sum =\t" << sum<< "\t"<<end-start<<endl;
         thread.join();
         thread2.join();
    
        return 0;
    }
    but it seems im doing it wrong! , i also tried using the condition variable (condition_variable_any) so that threads can notify eachother and thus speed up the process but im stuck how to use wait(mutex) in first place cause there is only one function!
    i dont want to separete the loop into two functions! i just want that one function to be used by two threads in the specified manner
    can someone help get this going ?
    ------
    how can i get all threads ID issued by me in my app?
    how can i iterate through running threads in my app ?
    is there any kind of means to get all the running threads using boost library? if it does whats the calss? if it doesnt how can i do that?
    can i resume a thread after pausing it ? ( how can i pause a thread rather than using sleep? )

    thank you in advance
    Last edited by Masterx; 04-11-2012 at 04:37 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    and also why is this not working ?
    im trying to get track of all threads i create and then whenever i wish , terminate them or query them .
    what am i missing here?
    Code:
        vector<thread> threadStore;
        
        threadStore.push_back(thrd);
        threadStore.push_back(thrd2);
        
        BOOST_FOREACH(thread th,threadStore)
        {
            cout<<th.get_id();
        }
    and i have difficulties using the timed_join() method as in
    Code:
    boost::thread mythread(myfunction);
    mythread.timed_join(sth should be here!);
    i dont know what im supposed to send as an argument so that it works , i checked the documentation , it says either TimeDuration ? ! or system_time?
    im clueless , can some one please do me a favor and enlight me on this too ?
    Last edited by Masterx; 04-11-2012 at 07:59 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Haven't used boost threads but this:

    Code:
    void IteratorFunc()
    {
         mutex.lock();
        for (i ; i<1000000000 ; i++)
            {
                sum+=i;
            }
            mutex.unlock();
    }
    Certainly won't do this:

    im trying to use two threads to sum lets say 1000000 numbers as an example , i want these threads iterate through a loop , one after another , i mean if thread one counted to 3 , thread 2 continues with 4 and so on ,how can i share that variable with these threads ?
    Instead, the first thread to acquire the lock will increment i to 1000000 then release it, then next thread to acquire the lock will have nothing to do. You want something more like:

    Code:
    void IteratorFunc()
    {
    
        for (i ; i<1000000000 ; i++)
            {
                mutex.lock();
                sum+=i;
                mutex.unlock();
            }
    
    }
    But even in this case, one thread will possibly do that in one pass anyway. Put a short sleep or something in there if you are trying to figure out how this all hangs together.
    Masterx likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by MK27 View Post
    Haven't used boost threads but this:

    Code:
    void IteratorFunc()
    {
         mutex.lock();
        for (i ; i<1000000000 ; i++)
            {
                sum+=i;
            }
            mutex.unlock();
    }
    Certainly won't do this:



    Instead, the first thread to acquire the lock will increment i to 1000000 then release it, then next thread to acquire the lock will have nothing to do. You want something more like:

    Code:
    void IteratorFunc()
    {
    
        for (i ; i<1000000000 ; i++)
            {
                mutex.lock();
                sum+=i;
                mutex.unlock();
            }
    
    }
    But even in this case, one thread will possibly do that in one pass anyway. Put a short sleep or something in there if you are trying to figure out how this all hangs together.
    thank you very much , i've already tried the second bit of code which you too suggested as well , as you mentioned again , the first thread just does the whole chore , is that because the given chore is not that time-consuming ? :-/ ?
    do we have sth , liked timed_mutex? i mean so that a thread releases a mutex in a period of time specified by locking method ? will the timed_lock() method do that ?
    by the way why doesnt this work ? :
    Code:
    boost::thread::sleep(boost::posix_time::microseconds(200));
    edited :
    got it , i should have used "this_thread instead" of" thread."
    and i also tried using yield() , so that when each one of those two threads gets one count done , they give out their remaining time slice , so that the other one could get inside .! seems not working though! what am i missing here again ? ( in know thats not the correct way , but just wana know what is happening here )
    and also for that
    Code:
    boost::thread mythread(myfunction);
    
    mythread.timed_join(sth should be here!);
    issue , i found
    Code:
    boost::posix_time
    , seems to be working , if im wrong please sb enlighten me
    Last edited by Masterx; 04-11-2012 at 09:30 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    i managed to get two threads iterate through the loop by adding a sleep() with 200 microseconds delay for each threads .
    i still want to know if we have a timed_lock so that any thread releases its mutex in the given timeslice .
    i also would appreciate any help on this ?
    i'm trying to get the tack of all created treads by me , by using thread_groups , i can add them to the group , but i cant use foreach to traverse inside that group .
    Code:
        boost::thread_group threadstore;
    
        threadstore.add_thread(&thread);
        threadstore.add_thread(&thread2);
        BOOST_FOREACH(boost::thread t ,threadstore.threads)
        {
            cout<<t.get_id();
        }
    and the threadstore has a threads member , which i dont know how to work around it , there is also an m member , which i have no clue of what it is ! and where it came form! and what to do with it! i couldnt find any information on these members on the documentation either !
    any help ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    What is this lock for? Why won't you just split this one big set of numbers into smaller subsets and then let each thread sum one set, finally adding computed sums in the main thread?
    manasij7479 likes this.
    I never put signature, but I decided to make an exception.

  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by kmdv View Post
    What is this lock for? Why won't you just split this one big set of numbers into smaller subsets and then let each thread sum one set, finally adding computed sums in the main thread?
    because im trying to teach myself this way , so that in area and situations where the data are not separable like this simple loop , i have a solution for that beforehand . thats why im trying to get this thing done this way .
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Masterx View Post
    thank you very much , i've already tried the second bit of code which you too suggested as well , as you mentioned again , the first thread just does the whole chore , is that because the given chore is not that time-consuming ? :-/ ?
    Yes. The computer can count to a million pretty quickly. On a multi-core system, there is some chance that you might get the two threads running in parallel and trading off a bit, although, using your code, how would you know? Throw some stderr reporting in there, that will also slow things down, and pass each thread a unique id to report with.

    and i also tried using yield() , so that when each one of those two threads gets one count done , they give out their remaining time slice , so that the other one could get inside .! seems not working though!
    Yeah, I noticed that yield() think looking at the docs. Did you unlock the mutex before you yielded?

    I think a possible complication with that will be the OS scheduler. I don't think you can tell it what to do in this regard, so if Boost yield() just causes the thread to cease execution in hopes that some other thread will then get picked up, that "other thread" might be the same thread again.

    You should really post the code you are using. Eventually someone who uses Boost::Threads will come along, but it might be while you are off-line, etc, and plain language descriptions of "what I did next" are very ambiguous.

    Also keep in mind that using some kind of sleep is fine for experimenting with how threads work, but don't get too into it as it is not reliable as a means of synchronization (even if it seems to work sometimes) and tends to negate the purpose of using threads.
    Last edited by MK27; 04-11-2012 at 12:24 PM.
    Masterx likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    thank you very much Mk27 ,
    here is the code :
    Code:
    #define BOOST_THREAD_USE_LIB
    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/thread.hpp>
    using namespace std;
    
    boost::uint64_t  i = 0;
    boost::uint64_t  sum=0;
    boost::mutex mutex;
    
    void IteratorFunc()
    {
    
        for (i ; i<100000000000; i++)
        {
            mutex.lock();
            sum+=i;
           cout<<i<<"\t"<<boost::this_thread::get_id()<<endl;
            mutex.unlock();
            boost::this_thread::sleep(boost::posix_time::microseconds(200));
           //boost::this_thread::yield();
        }
    
    }
    
    
    int main()
    {
        boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
        boost::thread thread(IteratorFunc);
        boost::thread thread2(IteratorFunc);
    
        boost::thread_group threadstore;
    
        threadstore.add_thread(&thread);
        threadstore.add_thread(&thread2);
    
        BOOST_FOREACH(boost::thread t ,threadstore.threads)
        {
            cout<<t.get_id();
        }
        thread.join();
        thread2.join();
        boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
    
    
    
        cout << "sum =\t" << sum<< "\t"<<end-start<<endl;
        return 0;
    }
    i cant get much out of boost::threading features , it doesnt have really much to offer .
    that yield() method , you are right , the scheduler takes the control and it is not guaranteed that my expected thread gets the chance to own the mutex! ( actually despite this very situation which it actually works just fine!!) so i cant rely on that any more.

    and about using thread_groups , it seems its kinda useless , you cant iterate through threads , or even rely on that , cause i heard that in the next version its going to be changed and the verion after that!
    there are no means in boost::threading to get the running threads nor is there any means to get the status of threads , nor is there any means to pause/resume threads and alot more .
    and according to my research , alot of functionalities such as threads priority needs me to implement the needed feature using the host OS API , what i was trying to avoid using boost!
    so the answers to my questions :
    how can i get all threads ID issued by me in my app? NO
    how can i iterate through running threads in my app ? NO
    is there any kind of means to get all the running threads using boost library? NO
    if it does whats the calss? if it doesnt how can i do that? use the OS API to do that!
    can i resume a thread after pausing it ? NO
    i'm really disapointed , there is'nt any kind of threading samples on boost either and this just makes it worst . all in all i think for a novice like me , with a C# background on threads , boost is not definitely an answer .Wxwidgets may be
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    so the answers to my questions
    O_o

    Okay.

    Let's just back this "rollicoster world" up! (Vincent Price was awesome.)

    The "answer" to those questions is: why on earth do you need those features?

    Your idea of threads seems to be poorly realized.

    I'm totally not trying to be rude here; you really don't seem to know the purpose and use of the threading and serialization primitives "Boost" provides.

    This "it is not guaranteed that my expected thread gets the chance to own the mutex" craziness for example. The job of a mutual exclusive lock is strictly ordering access of a facility. A mutual exclusive lock doesn't guarantee strict "fair" sharing. That is simply not the tool for that job. You can make such a facility, yes even using primitives provided by "Boost", but you aren't going to find such a beast ready "out of the box" from most libraries simply because it is rarely a necessary thing.

    Soma
    Masterx and MK27 like this.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Quote Originally Posted by Masterx View Post
    i'm really disapointed , there is'nt any kind of threading samples on boost either and this just makes it worst . all in all i think for a novice like me , with aC# background on threads , boost is not definitely an answer .Wxwidgets may be
    Try std::thread s from C++11. They are similar to boost's but differ in some places. I find it somewhat easier to comprehend that boost threads.
    If you need a more high level api, try Intel's tbb. As far as I've learnt, it is quite easy.

    For std::threads, you can pust the threads onto a vector and iterate throught them later to ..say..call join.
    There exists a get_id function of the thread in C++11.
    There also exists a way to get a native handle to the thread...but I'm not sure what it can be used for.

    >is there any kind of means to get all the running threads using boost library? NO
    That is the C++ way of thinking... you get only what you specify. For example, you can't get to iterate over all member functions of a class.

    >can i resume a thread after pausing it ? NO
    Look into sleep_for and sleep_until (again.. in std::thread.. no idea about boost's). Beyond that, it is the OS's scheduler's job, not your.
    Last edited by manasij7479; 04-11-2012 at 03:21 PM.
    Masterx and MK27 like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by phantomotap View Post
    O_o

    Okay.

    Let's just back this "rollicoster world" up! (Vincent Price was awesome.)

    The "answer" to those questions is: why on earth do you need those features?

    Your idea of threads seems to be poorly realized.

    I'm totally not trying to be rude here; you really don't seem to know the purpose and use of the threading and serialization primitives "Boost" provides.

    This "it is not guaranteed that my expected thread gets the chance to own the mutex" craziness for example. The job of a mutual exclusive lock is strictly ordering access of a facility. A mutual exclusive lock doesn't guarantee strict "fair" sharing. That is simply not the tool for that job. You can make such a facility, yes even using primitives provided by "Boost", but you aren't going to find such a beast ready "out of the box" from most libraries simply because it is rarely a necessary thing.

    Soma
    I agree with you on the part that says i dont know much about threading , thats right , and im just starting to learn about all of these .
    actually i was trying to experiment all the possibilities with the wrong example .
    there is a reason behind all of those craziness and sure the reason is my lack of knowledge on the field .
    and for the part why i would need such features :
    i imagined a situation in which there are messages going to a message dispatcher , message dispatcher gets one message at the time and process it and then tries to do what it was ordered to , so it fires a worker thread , the worker thread again
    either deals with reading or writing on a buffer ( suppose the message is to turn a light bulb on ! ) if i make the dispatcher multi treaded , instead of one message at a time it will get several messages at the time and will process them , then each of these threads will execute workers to do the job and so on .
    i needed a mechanism to make sure how a specific message is dealt with , was it successful or not ? how could i know about the status of threads being created in the process ? how could i impose some priority on threads , so that for example a writer thread has a more priority towards the reader threads , and how could i terminate a thread if the need arises ? how could i manage the concurrency of the application if i didnt know how many threads are working or blocked or what ever .
    i know i myself could implement them using windows api , or maybe some of these questions are just irrelavent and are because of poor planning or the scenario , but my first reason to start threading with boost was that i dont implement much of stuff, that some standard gets me in the correct way , i had a look at wxwidgets threading samples and its thread class , most of the features i requested here or asked here , are present in wxwidgets(http://docs.wxwidgets.org/trunk/classwx_thread.html) ,
    i think one of the most crucial reasons for my misplanning or mis understanding of threading capabilities , is just because of the lack of samples on threading itself , if there were practical examples demonstrating common scenarios , people like me would have a greater chance of success and not getting lost like this .!
    Last edited by Masterx; 04-11-2012 at 03:36 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  13. #13
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by manasij7479 View Post
    Try std::thread s from C++11. They are similar to boost's but differ in some places. I find it somewhat easier to comprehend that boost threads.
    If you need a more high level api, try Intel's tbb. As far as I've learnt, it is quite easy.

    For std::threads, you can pust the threads onto a vector and iterate throught them later to ..say..call join.
    There exists a get_id function of the thread in C++11.
    There also exists a way to get a native handle to the thread...but I'm not sure what it can be used for.

    >is there any kind of means to get all the running threads using boost library? NO
    That is the C++ way of thinking... you get only what you specify. For example, you can't get to iterate over all member functions of a class.

    >can i resume a thread after pausing it ? NO
    Look into sleep_for and sleep_until (again.. in std::thread.. no idea about boost's). Beyond that, it is the OS's scheduler's job, not your.
    thank you very much . but the problem is , no compiler supports all threading capabilities of C++11 yet. the gcc 4.7.0 has a partial implementation of C++11 threading but not much ( http://gcc.gnu.org/gcc-4.7/cxx0x_status.html ) , and those parts are of no use really .i need to wait for the 4.8.0 or 4.9.0 to see if they get implemented .
    Last edited by Masterx; 04-11-2012 at 03:40 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Quote Originally Posted by Masterx View Post
    the gcc 4.7.0 has a partial implementation of C++11 threading but not much , and those parts are of no use really .
    That isn't very true..
    The parts that aren't supported yet are not very comonly used.
    (The concurrency section in the Status site is misleading as it speaks of the core extensions needed, not the libraries.... AFAIK)
    Your original program, for example can easily be implemented with std::threads.
    Masterx likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    O_o

    For your example you have several options but at a primitive level the situation probably calls for a fairly standard "work queue" with a "reader/writer strategy" acting as "results" storage. Those fiddly bits that you are trying to find support for are never going to be native things because those fiddly bits are almost always need to be specially crafted.

    Look at your "how do I know if a job completed successfully" example. A lot of threading libraries don't have an answer for that because the notion of result is unique to the problem. However, it is pretty trivial to add locking around a shared container such as a map and store results of any kind within that map.

    As for not having any examples... you are clearly looking in the wrong place. Don't look for "Boost thread examples", "wxWidget thread examples", or anything like that. The "API" is just window dressing. Great programmers have already distilled out the core details from thousands of strategies, labelled them with a shared jargon, and thoroughly illustrated their use and implementation. What you should be looking for is "concurrency patterns" or similar. You may not find real code using a specific "API", but you'll certainly get a better idea of how to use threads and serialization primitives.

    Soma
    Masterx and manasij7479 like this.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. couple of questions concerning Boost::scoped_ptr
    By Masterx in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2012, 09:02 PM
  2. some questions about Boost libraries
    By Masterx in forum Tech Board
    Replies: 41
    Last Post: 11-10-2010, 09:58 AM
  3. Do you use Boost?
    By sarah22 in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2010, 03:15 PM
  4. boost
    By siavoshkc in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 11:58 PM
  5. erasing elements from a boost::ptr_vector (boost n00b)
    By Marcos in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2006, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21