473,695 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_threadsafe ty.cpp begins =============

#include "threadsafe_que ue.hpp"
#include <iostream>

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

template <typename T>
void produce(threads afe_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(threads afe_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_queu e<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_threadsafe ty.cpp ends =============

=========== threadsafe_queu e.hpp begins =============

#ifndef PNW__THREADSAFE _QUEUE_HPP
#define PNW__THREADSAFE _QUEUE_HPP

/***
* @file threadsafe_queu e.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_queu e
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
boost::conditio n c; ///< Condition.
public:

/***
* Push @p value.
*/
void push(const T & value) {
boost::mutex::s coped_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::s coped_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::s coped_lock sl(m); // NOTE: lock mutex
// wait until queue has at least on element()
c.wait(sl, boost::bind(&st d::queue<T>::si ze, q));
T value = q.front();
q.pop();
return value;
}
};

#endif

=========== threadsafe_queu e.hpp ends =============

Aug 16 '07 #1
3 6810
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...@gm ail.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_queu e<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_threadsafe ty.cpp ends =============

=========== threadsafe_queu e.hpp begins =============

template <typename T>
class threadsafe_queu e
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
^ noncopyable
boost::conditio n c; ///< Condition.
>
=========== threadsafe_queu e.hpp ends =============
threadsafe_queu e 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
2951
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 one that works is the first which requires "DataManager" to be a template class. Since I don't want "DataManager" to be a template class (no advantages), I am trying various ways and none is working. Did quite a bit of search via Google on this...
4
2605
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 the code below. The function template < typename T >
3
1678
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. The problem is linking, VC6++ give me this error: unresolved external symbol "class std::vector<double,class std::allocator<double> > __cdecl VectorFromStr(class EFAString
5
1474
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 two images together. I would have like to
4
1426
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 the FO square_o in the following. When MUST you use one over the other, and when MIGHT you use one over the other? Thanks a bunch..... #include <iostream.h>
3
1711
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(); template<class TO, class F1, class F2> TO convert_to(); template<class TO, class F1, class F2, class F3>
6
4016
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 from a user's program and use a C backend to evaluate the tree. I'm using Lex, Yacc and C. Now, there are some functions that are built into this language and others which are not. The problem I'm having is that I haven't found a way to represent...
5
2112
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
1762
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 I felt I could post it here. Also, I did ask in the Intel forums, without much success. And maybe there are some c++ coders in here that are familiar with the Intel compiler. The code below highlights the problem I have. Essentially, I have a
0
8640
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9122
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7670
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.