If you want to have a thread pool with only 10 threads, which of the following techniques is best to achieve this purpose?
Invoke the newCachedThreadPool() method of the java.util.concurrent.Executors class by passing 10 as the parameter.
Invoke the newCachedThreadPool() method of the java.util.concurrent.Executor class by passing 10 as the parameter.
Invoke the newFixedThreadPool() method of the java.util.concurrent.Executors class by passing 10 as the parameter.
Invoke the newFixedThreadPool() method of the java.util.concurrent.Executor class by passing 10 as the parameter.
FixedThreadPool fx = new FixedThreadPool(10);
Of the 5 things you have listed, only the 3rd actually exists. So I would suggest using that.
Related
I have a multithreading C++ design question for Windows. Suppose I have a C++ class as follows:
class CCamera_AxisNew : public CCamera_IPBase64 and suppose in the base class, CCamera_IPBase64, implementation of PTZThreadProc(..) , I read commands from a queue and asynchronously send PTZ commands only through either
the object pointer indirectly, IPTZControl->SetProperty(..) where the object pointer IPTZControl = new CCamera_AxisNew or other similar constructors or
directly through this->SetProperty()
The method PTZThreadProc(..) is spawned on a separate thread.
Also, in the class CCamera_IPBase64, I can bypass the PTZThreadProc(..) which runs on a separate thread by synchronously sending non-PTZ commands through either
the object pointer indirectly, IPTZControl->WriteCamera(...), or
directly through this->WriteCommand(..).
The client program sends requests through a REST endpoint to an CCamera_IPBase64 object which uses a main thread to receive the request and places only PTZ commands on a queue.
My question is it better to use delegation IPTZControl->SetProperty(..) or avoid delegation such as this->SetProperty()
Any help is greatly appreciated.
It is better to avoid delegation by using this->SetProperty() and taking advantage of inheritance since it avoids extra redundant code required by the delegation design pattern.
The dilemma I have is I want to create a timer (also called an "alarm", using Game Maker terminology) that can call ANY function after its time expires. Not just have it call a specific function pointer based on a specific signature.
So, simple question: Is this possible?
You can use C++11's std::function and std::bind to wrap whatever function you want to call after the timer expires.
Q&A with the Question poster
I have a function that takes in an Egg and a Cheese and returns an Omelet. I hand my function to your timer. When the timer's alarm goes off, with what Egg and what Cheese will you call my function? - Robert Cooper
#RobertCooper with the Egg and Cheese you gave me when you handed me the function of course! – nightcracker
#nightcracker: So when I set an alarm, I need to give the timer a function, an Egg, and a Cheese. Then again, if my callback instead takes in a Bacon, a HomeFries, Sausage, and an Oatmeal, I need to give the alarm a function, a Bacon, a HomeFries, Sausage, and an Oatmeal. For any number and combination of types, the alarm must be able to remember and store members of those types for later use on the callback function.
Recommendation
I wouldn't recommend building an alarm capable of storing any combination of cooking supplies. Keep the type signature of the alarm constant and simple. If a client want to make the alarm to make breakfast or start a band or launch a rocket-ship, make the client wrap whatever functions and variables they need into a simple functor or lambda function: one that remembers all the variables it needs and only one way to set it off (operator (), called with no arguments).
dear C++ professionals. I got a problem. I have a program, which has 1 abstract class base_class and 2 derived classes: sippeers and dbget. It also has 2 threads. First thread gets commands from user, second thread pocesses these commands. Both derived classes represent different commands. So, I have to create some kind of stack, where I should put user commands from first thread and get them in second thread to process. To make one stack for all commands, I got to use polymorphism. First, I tried to use std::list. But there was first problem: I can't make a list of abstract classes. Then I tried to use boost::ptr_list, but there was the second problem: classes, created in the first thread, dissappear with end of procedure, that created them. So pointers become illegal. The question: what kind of realization should I use? It looks like I must store every copy of derived class. But where?
An std::queue of shared_ptr<base_class> is the most straightforward solution to pass classes from 1 thread to another without worring about memory management. Combined with a conditional variable to signal that the queue is not empty, so the consumer-thread can wait.
For the polymorphism part, have an extra virtual function (execute()?), so the consumer thread shouldn't be aware what class it actually receives.
First, I tried to use std::list. But there was first problem: I can't make a list of abstract classes.
You can, however, create an std::list containing pointers to the abstract base class, e.g.
std::list<base_class *> commands;
Then I tried to use boost::ptr_list, but there was the second problem: classes, created in the first thread, dissappear with end of procedure, that created them. So pointers become illegal.
Do you mean the new commands are created on the stack of the first thread? Don't do that - the first thread doesn't know when the second thread finished handling the commands, so the first thread shouldn't define when they end. Just let the first thread allocate objects using new.
That being said, your use case sounds like a classical example of the producer-consumer problem. Consider having a look at the Wikipedia page on this for some inspiration on how to implement such a system properly (it's not too hard to get it wrong).
When i creating TThread i can't pass parameters to thread, so i need use global variables or what?
I am using Embarcaderos Rad Studio C++ Builder 2010
You have a class derived from TThread, right? Can you just make your class constructor take additional arguments (beyond the bool suspended one that seems to be common)?
An alternative to providing a different constructor is to simply assign properties of the thread between the time you create the object and the time you start it.
bool suspended = true;
TSergeyThread* thread = new TSergeyThread(suspended);
thread->Property1 = 4;
thread->SetValue("foo");
thread->Start(); // or ->Resume(), if your VCL is too old
Better is to provide all that information in the constructor, though. (RAII, and all that.)
I rewriting some code that i written a long time ago.
The code is a class that start another worker thread with AfxBeginThread. When the thread ends, it needs to return it work to the calling class.
Actually when the thread ends it send a message by PostMessage with its results to the called class.
But this way is really dependent of MFC, and to do this my class have to implement all the MFC stuffs.
May be correct if instead of send a message it directly call a non-static method of this class ?
Rather than trying to call a method directly (which will introduce a whole new set of threading problems of its own), try using the native Win32 ::PostMessage() instead of the MFC implementation of the same function. Any thread can call ::PostMessage() to deliver a message to another thread safely.
It sounds as though you want to use regular threading primitives, not window messaging primitives.
Which version of AfxBeginThread are you using? If you pass it a class instance, you should be able to access the members of that class directly once you know its finished running. If you passed it a function pointer, you can pass any class pointer in with the lParam parameter, then use that as a communication context.
You just want to make sure that when you access the class you do it in a thread safe manner. If you wait till the thread has ended you should be fine. Otherwise you could use Critical Sections or Mutexes. See the MSDN article on thread synchronization primitives for more info.