Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

can a C++ template be runnable?

Posted on 2008-10-17
4
Medium Priority
?
641 Views
Last Modified: 2012-05-05
HI, got a question on how to implement the multi-threading:
can I make a C++ template runnable, something like:
template<class, class> public: Runnable { ... }

If someone can provide an example, that will be great. Thanks.
0
Comment
Question by:cw1592
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22747889
>>>> can a C++ template be runnable
Actually I have only a weak idea what you mean by 'be runnable'.

Generally functions - template or not - only can be called. As you were talking of 'multi-threading' you may be ask whether a template function could be used as start function for a thread. If so, the answer is yes. If using _beginthread (Windows) for creating a new thread, the function signature is 'void threadFunc(void *)' (for CreateThread it is 'DWORD threadFunc(void *)' and for pthread_create it is 'void threadFunc(void *)'). So you may have a template function

class template<typename T>
void threadFunction<T> (void* p);

which you may use like

   _beginThread(threadFunction<int>, 0, this);

to create a new thread that runs the template function with int template type.
0
 

Author Comment

by:cw1592
ID: 22750958
my idea is like this (implement on linux):  
1. I had defined a calss template first -
template<class A, class B>
class consumer:public class C {
    run_input(argument,....) {process some input data}
    run_output(argument,...) {finish a computation and print out result}
    other members and functions...
}

2. I also used this template to generate different kinds of classes (as consumers) already, for example:
consumer(milk.egg) buyerA;
consumer(sandwich, fries) buyerB;
consumer(beef, soup) buyerC;

3. now, in a supermarket, you want have several lines open to process the check-out, so I want the multi-threading; therefore my question is how can I (or can I) make the template of consumer be runnable such that I can call the buyerA::run_input() in a thread or the buyerA::run_output() in a thread without changing a lot of code.

should I do something like ( a Runnable class is assumed defined)
template<class A, class B>
class consumer:public class C, public Runnable {
    run_input(argument,....) {process some input data} // same function actually
    run_output(argument,...) {finish a computation and print out result} // same funciton actually
    other members and functions... // all the same as before
}

or there is a better way to get what I want?

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1500 total points
ID: 22751318
The problem with templates is that they need a resolution of the template type at compile time, not at runtime. Hence, in your code for the threading you must *know* the template type (or types).

For a generic approach you would need to take the polymorpghic way of C++ rather than the templates. You would derive from a common base class where you can put all functionality into which is common to all derived classes, pass a pointer of a derived class object as argument to your thread, cast it to the base class pointer in the thread function and call virtual base class functions which would virtually call the overloaded functions of the derived classes.

class Product
{
      ...
      virtual double getPrice();

};

class BreakfastProduct : public Product
{
};

class Milk : public BreakfastProduct
{
      ..
      virtual double getPrice() { return 1.50; }

}


    Milk* pMilk = new Milk;
    _beginThread(ThreadFunction, 0, pMilk);


 ...

    void ThreadFunction(void* p)
    {
         Product* ptrProd = (Product*)p;
         double price = p->getPrice();   // get price of Milk here in that sample
         ...
    }


Note, the above sample only should show the principle. For products of a food company, or a restaurant you can't have a own class (type) for each concrete product. But you could have some different categories which need different handling and where you could use the virtual approach.


0
 

Author Comment

by:cw1592
ID: 22770368
ok; sounds like I have to get the calsses defined first; an approach using a template won't save me too much in this  multi-threading case.

thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question