473,394 Members | 1,371 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,394 software developers and data experts.

Problem using boost::bind() with template functions having arguments

Hey there, C++ Coders!

I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?

Many thanks in advance,
Per Nordl÷w

=========== test_threadsafety.cpp begins =============

#include "threadsafe_queue.hpp"
#include <iostream>

using std::cout;
using std::endl;

template <typename T>
void produce(threadsafe_queue<T& q, uint n)
{
for (uint i = 0; i < n; i++) {
T x = i;
q.push(x);
cout << "i:" << i << " produced: " << x << endl;
}
}

template <typename T>
void consume(threadsafe_queue<T& q, uint n)
{
for (uint i = 0; i < n; i++) {
T x = q.wait_pop();
cout << "i:" << i << " consumed: " << x << endl;
}
}

int main()
{
threadsafe_queue<floatq;
boost::thread pt(boost::bind(produce<float>, q, 10));
boost::thread ct(boost::bind(consume<float>, q, 10));
pt.join();
ct.join();
return 0;
}

=========== test_threadsafety.cpp ends =============

=========== threadsafe_queue.hpp begins =============

#ifndef PNW__THREADSAFE_QUEUE_HPP
#define PNW__THREADSAFE_QUEUE_HPP

/***
* @file threadsafe_queue.hpp
* @brief Thread Safe wrapper on std:queue using Boost.
*/

#include <queue>
#include <iostream>

#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>

template <typename T>
class threadsafe_queue
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
boost::condition c; ///< Condition.
public:

/***
* Push @p value.
*/
void push(const T & value) {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
q.push(value);
}

/***
* Try and pop into @p value, returning directly in any case.
* @return true if pop was success, false otherwise.
*/
bool try_pop(T & value) {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
if (q.size()) {
value = q.front();
q.pop();
return true;
}
return false;
}

/// Pop into @p value, while possibly waiting forever.
T wait_pop() {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
// wait until queue has at least on element()
c.wait(sl, boost::bind(&std::queue<T>::size, q));
T value = q.front();
q.pop();
return value;
}
};

#endif

=========== threadsafe_queue.hpp ends =============

Aug 16 '07 #1
3 6786
Nordl÷w wrote:
I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?
[..]
Do you know that Boost.org has its own online forums? Just checking...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #2
On Aug 16, 4:57 pm, Nordl÷w <per.nord...@gmail.comwrote:
Hey there, C++ Coders!
Hey there, you!

Can you copy paste your g++ output?
And if nobody can save you ass here, there you'll find the boost-
user mailing list: http://www.boost.org/more/mailing_lists.htm#users

Cheers,
LS

Aug 16 '07 #3
Nordl÷w wrote:
Hey there, C++ Coders!

I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?

Many thanks in advance,
int main()
{
threadsafe_queue<floatq;
boost::thread pt(boost::bind(produce<float>, q, 10));
boost::thread ct(boost::bind(consume<float>, q, 10));
pt.join();
ct.join();
return 0;
}

=========== test_threadsafety.cpp ends =============

=========== threadsafe_queue.hpp begins =============

template <typename T>
class threadsafe_queue
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
^ noncopyable
boost::condition c; ///< Condition.
>
=========== threadsafe_queue.hpp ends =============
threadsafe_queue has mutex as its member, which is noncopyable, when you
do boost::bind, it triggers error
Aug 21 '07 #4

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

Similar topics

3
by: claude uq | last post by:
Hello, Am trying to use template functions within some class to convert int, float, doubles, etc into strings. Below, three ways to do it via use of "to_string(const T & Value)" . The only...
4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
3
by: Filipe Valepereiro | last post by:
I all. I need to write a function that convert one string into a vector. This string represent a serialized form of the vector. So i come up with this piece of code, that compile just fine....
5
by: Bolin | last post by:
I am frustrated as I am trying to design a toy image library. I want the user to write mul(im, 2) if s/he wants to multiply the image by 2, and mul(im1, im2) if s/he wants to multiply...
4
by: richardclay09 | last post by:
Hi Can someone please write some "compare and contrast" notes for "Template functions vs. function objects"? When to use one and not the other? For example, the TF square_f does the same thing as...
3
by: REH | last post by:
I need to create template functions with the same name but different number of template parameters. My compiler says this is illegal: template<class TO, class F1> TO convert_to(); ...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
5
by: Andrei Tarassov | last post by:
Hi! I would like to know if it is possible to implement something like this: template <class X, class Y> X void func(X, Y) { ... } template <class X, class Y> Y void func(X, Y) { ... }
2
by: vectorizor | last post by:
Hello all, I am attempting to vectorize few template functions with the Intel compiler, but without much success so far. Ok granted, this question is not 100% c++, but it is related enough that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.