Solved

can a C++ template be runnable?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now