Solved

can a C++ template be runnable?

Posted on 2008-10-17
4
592 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now