• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

C++ Multithreading in Boost

C++:
//    cout<<"Consumer "<<IntToString(m_id).c_str()<<" consumed: ("<<m_queue->Dequeue().c_str()<<")"<<endl;
 
Use:!!
            std::cout << m_id << ", " << m_queue() << std::endl;

You seem like a C++ novice (yes?) and PC pattern is very advanced.
 
C++:
//    cout<<"Consumer "<<IntToString(m_id).c_str()<<" consumed: ("<<m_queue->Dequeue().c_str()<<")"<<endl;
 
Use:!!
            std::cout << m_id << ", " << m_queue() << std::endl;

You seem like a C++ novice (yes?) and PC pattern is very advanced.
I tried and still get same error.:cry:
Actually, I'm new to multi-thread programing in C++ with Boost library.
I want implement the following functionality :


  • There are 2 threads: producer and consumer.
  • Consumer only processes the firstest value, i.e., first in first out (FIFO).
  • using condition variable...

Everything is hard for me. You can send me the complete code about Shared queue problem ?
Thank.
 
It does work; I think you are doing something wrong.

It's discussed in my Boost Volume I book.
 
Just comment out that cout line. Does the code work?
I 've run this code topic and get some error when running...
error C3861: 'IntToString': identifier not found
error C3861: 'IntToString': identifier not found
error C2228: left of '.c_str' must have class/struct/union type is ''unknown-type''
error C3861: 'IntToString': identifier not found

Build FAILED.

:cry:.I really tried to find out problem....Can you help me again.
I really appreciate your help with my project!
 
C++:
template<typename T>
std::string toString(const T& value)
{
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

std::string IntToString (const int& i)
{
return toString<int>(i);
}
 
C++:
template<typename T>
std::string toString(const T& value)
{
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

std::string IntToString (const int& i)
{
return toString<int>(i);
}

:).It does work. Thanks a lot !
I have to bother you with this problem.
 
Hi Daniel,

This is a great reference. It's clearly written and well explained. And the task is explaining the boost libraries.

In my opinion, however, the boost thread library is a horror. Doing simple things, like creating a threat class, is complex. In fact, almost everything that one might want to do is complex in the Boost thread library.

Instead of the Boost thread library, I highly recommend the Qt thread classes from the Qt Core. Most people think of Qt as a graphic library, which it is. But the Qt Core classes are very nice. And the Qt thread library is much like Java threading: easy to use and easy to understand (at least compared to the Boost thread library).

Best,

Ian
 
Hi Ian,
Boost thread (and C++ 11 concurrency) are very flexible but low-level indeed. We coded the producer-consumer a few years ago because they were no libraries for them and as a good test case.

QT is very good but I have not worked with it.

These days task libraries such as TBB, PPL and PPL hide low-level details and allow developers to think in parallel design patterns.

The book by Mattson et al 2005 gives a global overview of these patterns.

regards

Daniel
 
Thanks for the post, Daniel. As always you're very informative. I didn't know about the Intel Threading Building Block library. It looks interesting. I develop on Linux unless I have a compelling reason to use Windows, so PPL is not an option for me.

After I wrote that post about Qt I tried to get Qt to work on Linux (Fedora 14). I can't get the libraries right and I have found no documentation on what libraries I need to link with (other than Qt5Core). Its possible it's not compatible with the version of Linux/gcc I have, but I can't tell. So the TBB library looks like a good alternative now that I've reached a road block with Qt. Which is too bad. I've used Qt on Windows and I really like the class library.

One problem with Qt is that it uses its own allocation model, so you have to call a Qt initializer in main(). I'm fine using the STL or Boost containers. I just want object based threading. So I'd like something lighter weight than Qt and TBB looks like it might be what I'm looking for. It also looks like it supports loop level parallelism, which is not what I need right now. But I've used this in R, so I might want it in the future.

Best,
Ian
 
I have just got interested in this topic. I am using MS Express 2013
could some one post the code for Fibonacci ?

1. using threads ...// no boost please
2. using cores ...// #pragma

I would like to see the difference.

So far what I have learned is not so clear to me. Eager to learn. Thnkx.
 
Back
Top