Solved

can a C++ template be runnable?

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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