I am running a thread pool where a function that is being called in the threads needs the mongocxx::pool pool variable so it can call pool.acquire() to get a client. I can't seem to pass the pool variable. How can I pass the pool variable. Or can I some how make pool global through out my application?
I am following this example https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/pool.cpp
The mongocxx::pool class isn't copyable, so you can't pass it around (though you could std::move it around, though that probably is not useful for your case). If you want a shared instance, you need to keep it on the heap and provide a way for different parts of the code to access that shared resource, perhaps via shared_ptr. Have a look at the instance_management example in the sources for one example of how to manage a pool, via a singleton.
Related
I have created a class, which has many public functions, some which write data and some that only read data.
It's required that I do this within 3 threads, I have no other option.
I know if I accessed a shared resource just to read, then I don't have to protect, but I don't know if it is any different when I am using a function to read a private variable of the shared resource.
E.g. I am trying to do...
globalObject.readColour();
which is a function that reads the colour of the global object.
Does it mean that I have to secure the thread at this point, or is it okay to just read the value without any risks?
I'm working on mbed, which supports c and c++98.
This question is similar to this one
If all your threads will only read the variable then you don't need mutex (or similar), but if any thread performs a writing operation you should use mutex.
I have a C++ multi-threaded application which run tasks in separate threads. Each task have an object which handles and stores it's output. Each task create different business logic objects and probably another threads or threadpools.
What I want to do is somehow provide an easy way for any of business logic objects which are run by task to access each task's output without manually passing "output" object to each business logic object.
What i see is to create output singleton factory and store task_id in TLS. But the problem is when business logic create a new thread or thread pool and those thread would not have task_id in TLS. In this way i would need to have an access to parent's thread TLS.
The other way is to simply grab all output since task's start. There would be output from different task in that time, but at least, better than nothing...
I'm looking for any suggestions or ideas of clean and pretty way of solving my problem. Thanks.
upd: yeah, it is not singletone, I agree. I just want to be able to access this object like this:
output << "message";
And that's it. No worry of passing pointers to output object between business logic classes. I need to have a global output object per task.
From an application point of view, they are not singletons, so why treating the objects like singletons?
I would make a new instance of the output storer and pass the (smart?) pointer to the new thread. The main function may put the pointer in the TLS, thus making the instance global per thread (I don't think that this is a wise design deision, but it is asked). When making a new (sub-?)thread, the pointer can again be passed. So according to me, no singletons or factories are needed.
If I understand you correctly, you want to have multiple class instances (each not necessarily the same class) all be able to access a common data pool that needs to be thread safe. I can think of a few ways to do this. The first idea is to have this data pool in a class that each of the other classes contain. This data pool will actually store it's data in a static member, so that way there is only one instance of the data even though there will be more than one instance of the data pool class. The class will then have accessor methods which access this static data pool (so that it is transparent). To make it thread safe you would then require the access to go through a mutex or something like that.
I'm using Apache Commons Pool library to maintaing a pool of couchbase connections (can be seen as any kind of connection, doesn't really matter).
The problem I'm facing is that the only way I've found to return objects to the pool is to do it programmatically by calling the returnObject method from the GenericObjectPool class. This forces the application to guarantee the return of the object once is borrowed from the pool, regardless any exception or unexpected behavior in the application.
Even though I'm controlling the return of the objects to the pool in the whole application, I find risky to depend exclusively on the programmer to return the objects. Does anybody knows a way to automatically return objects once a given timeout is exceeded (reclaim objects). It would also work any way to make the pool create new objects, once a timeout is exceeded for the borrowed objects.
PS: My application exposes a set of REST Web Services, that connect to distributed memcache server (Couchbase). The pool creates a set of connections to Couchbase.
Any suggestion would help!
EDIT
The first solution I've tried was to create a New Class (CouchbaseClientHandler) containing a connection Object from the type of Objects stored in the pool (CouchbaseClient).
I've implemented the finalize method on CouchbaseClientHandler, ensuring that the associated was actually returned to the pool, if the reference to this object was getting lost by an unexpected exception. The object would be returned when the garbage collector destroys the object. This didn't worked as expected. Is impossible to predict when the garbage collector will arrive a reclaim objects, and it was normally taken higher time than desired.
The solution I've actually working right now is a bit different, but wide safer.
Since my Pool is intended to be used by Web Services, and since every web service is running in a separate and unique thread (I'm using Jersey on Tomcat), I'm decided to use a static hashmap variable that maps unique Thread Id's to a list of CouchbaseClient objects created borrowed from the pool in the execution of the webservice. Since my web services are designed in such a way that, not matter what happens, a handler method will process the final output before returning, I can be sure to always run a method that returns to the pool
those borrowed objects (connections), that have not been effectively returned to the pool.
Although this worked pretty well for me, I would really like to know if there is a better way of claiming or wiping by timeout borrowed objects that have not been returned.
Assuming that the destroy / create new approach is OK and you are using version 2.0+ of commons pool, you can use abandoned object tracking and removal to make sure capacity is not permanently leaked when objects are borrowed and never returned. See the javadoc for AbandonedConfig for the configuration settings and the GenericObjectPool constructor that takes an AbandonedConfig instance as an argument. With abandoned object tracking and removal enabled, the pool will destroy instances that have been borrowed but not returned for longer than the removeAbandonedTimeout when the pool is low on capacity.
If for some reason you have to use a version 1.x pool, you can grab the source or use directly the AbandonedObjectPool that ships with DBCP 1.x.
I have an application that will be spawning multiple threads. However, I feel there might be an issue with threads accessing data that they shouldn't be.
Here is the structure of the threaded application (sorry for the crudeness):
MainThread
/ \
/ \
/ \
Thread A Thread B
/ \ / \
/ \ / \
/ \ / \
Thread A_1 Thread A_2 Thread B_1 Thread B_2
Under each lettered thread (which could be many), there will only be two threads and they are fired of sequentially. The issue i'm having is I'm not entirely sure how to pass in a datastructure into these threads.
So, the datastructure is created in MainThread, will be modified in the lettered thread (Thread A, etc) specific to that thread and then a member variable from that datastructure is sent to Letter_Numbered threads.
Currently, the lettered thread class has a member variable and when the class is constructed, the datastructure from mainthread is passed in by reference, invoking the copy constructor so the lettered thread has it's own copy to play with.
The lettered_numbered thread simply takes in a string variable from the data structure within the lettered thread. My question is, is this accceptable? Is there a much better way to ensure each lettered thread gets its own data structure to play with?
Sorry for the somewhat poor explanation, please leave comments and i'll try to clarify.
EDIT:
So my lettered thread constructor should take the VALUE of the data structure, not the reference?
I would have each thread create it's own copy of the datastructure, e.g. you pass the structure in the constructor and then explicitly create a local copy. Then you are guaranteed that the threads have distinct copies. (You say that it's passsed by reference, and that this invokes the copy constructor. I think you mean pass by value? I feel it's better to explicitly make a copy, to leave no doubt and to make your intent clear. Otherwise someone might later come along and change your pass by value to pass by reference as a "smart optimization".)
EDIT: Removed comment about strings. For some reason, I was assuming .NET.
To ensure strings are privately owned, follow the same procedure, create a copy of the string, which you can then freely modify.
There is a pattern called Active Object Pattern wherein each object executes in its own thread. Frameworks like ACE support this. If you have access to such frameworks, you should use those. In any case, i would believe creating a new instance of an object and allowing it to exetute in its own thread is much cleaner that invoking the copy-constructor to make a copy of the object. Else see if you can fit a solution that uses Thread Local Storage.
Have you looked at boost threads?
You would basically create a callable class that has a constructor that takes the parameters the thread is to work on and then launch the thread by passing objects of your callable class, initialized and ready to go.
This is very similar to how Java implements threads and it makes a good amount of sense most of the time from a design point of view.
You aparently are making a copy of the data for each trhead and everything works? then no problem.
Here are some additional thoughts:
If data is read only, you can share a single struct and everything will be ok, as long as each read is small and fast (basic types)
If data needs to be written, but "private" (or contained) to each thread, then send a copy to each thread (what you are doing). Caveat: I assume the data is not too big and a copy does not eat to much resources.
If the data needs to be written and the new values shared between threads, then you need to think about it (read on it) and create a proper design. I like a transactional object to centralize each threads read/write operation. Like a tiny database in memory. Check on thread mutex, semaphores and critical sections). Dealing with huge data set I have used a database to centralize requests (See ODBM). You can also check existing messaging queuing libraries (like MSMQ) to have data change ordered and synchronized.
Hope this helps.
It seems unlikely that you would want each thread to operate on the data and then not at least occasionally have another thread react to what another thread has done to another thread's work on the data. If you are truly independent meaning that no other thread truly will ever care about work that another thread has done, then I suggest making a copy of the data, otherwise in the case where you will want to do work in one thread and make that result of that work available to another thread I would suggest that you, pass a reference/pointer to the object around and then protect access to it via locks so that the threads can work with it, properly, I suggest a multi-read, single writer lock implementation.
I am pretty sure these classes are not thread safe.
But, is it safe to use different objects from these classes in different threads?
Do they have any global dependencies with each other like static data or anything to watch out for?
As long as the calls you are making are to static functions that do not access shared memory (shared between threads).
Basically the only time you will hit a problem is if the function you call accesses shared data. If your function simply does some work on data you provide it is thread safe.