Solved

can a C++ template be runnable?

Posted on 2008-10-17
4
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 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