« first day (322 days earlier)      last day (2536 days later) » 

user1593881
5:11 AM
Meyers says : Prefer task-based programming to threadbased. Should that be taken with a pinch of salt? What's wrong with whizz-banging the std::thread function coupled with a std::lock_guard<std::mutex> g(m)` guard for simple tasks I don't care much about.
 
user1593881
A bit unclear from my side I know but I was wondering if I can get away without knowing what the std::async is and how it's used.
 
user1593881
5:38 AM
I read it as: "Always prefer".
 
user1593881
8:32 AM
Discard the above nonsense please. My bad ;)
 
spawning and killing threads creates a lot of overhead, if you instead have a queue (even an inefficient one) that you can push tasks into and threads pull tasks out of, all that overhead is gone.
 
user1593881
9:19 AM
@ratchetfreak I see. Thanks.
 
11:13 AM
BTW. threadpools are not provided by standard library, so, as usual, you need to resort to third-party libs
 
even a simple std::vector<std::function> guarded by a std::mutex is going to be faster than creating threads for each task. Though you will want to avoid forcing the O(n) copies when popping by using it as a circ buffer that you can grow.
 
user1593881
11:32 AM
Thank you both. Appreciate your time and effort.
 
4:23 PM
@ratchetfreak Or just use an std::deque instead of vector.
 
@JerryCoffin I was going for the simplest possible option
 
@ratchetfreak The simplest option would clearly be std::deque. It lets you just push_back on the producer side, and pop_front on the consumer side.
I suppose there's room for argument that std::queue provides some simplification, letting you just use push and pop instead. Most people rarely bother with it though (it's just a wrapper around a deque or list, so essentially all it adds is a mapping of the names from push_back to push and pop_front to pop),
 
can someone give me a crash course on std::optional?
 
In theory, it also provides a tiny bit of (compile-time) indirection from the type of container used, so you can easily switch it from using a deque for the underlying storage to instead use a list, or any other container that provides push_front and pop_back (in addition to the usual front, back, empty, etc.)
 
4:33 PM
I am a bit scared to use it
 
@Yashas what information are you looking for
 
I just need to know if I have done it correctly
the code works but I am a bit worried becaz I have never used it before
 
@Yashas A container of optional seems somewhat questionable, at least to me. I'd generally plan on simply removing items from the collection (or never adding them to it) if they don't exist.
 
if you scroll up a bit, you'll see my description of the intended usage
 
fire up a debugger and verify your assumptions
 
4:34 PM
@JerryCoffin In my case, constructing objects is expensive.
@ratchetfreak It still bothers me when I don't know what my code does.
 
@Yashas the only thing that will save is the allocation overhead
 
I also need to maintain the index. The index of the vector items should not change when an item is removed or added.
I was earlier using unique_ptr but that made my code ugly
I think it was someone here who suggested me to use std::optional
 
that's a direct improvement over std::unique_ptr in that you avoid dynamic allocation
 
yup
that's the only improvement I can see
 
but now can think of a different solution
 
4:38 PM
@Yashas An optional is basically little more than template <class T> class optional { T *object; };, with operators to automate always checking that the pointer is non-null before dereferencing it, giving you a bool to indicate whether it's non-null, and so on.
 
just use std::unordered_map<std::size_t, T> or std::map<std::size_t, T>
 
unordered map is quite slow for accessing items, right?
 
fast enough compared to a vector
unordered uses a hash table
 
it's a quick lookup
 
hash of a size_t is a few bit opts
 
4:39 PM
@Yashas That depends on the hash function, but it's generally fairly fast--mostly slows down if you have to hash large keys such as long strings.
 
@JerryCoffin or bad hashkey distribution
 
@ratchetfreak Always a possibility, of course, but in my experience one that's really quite unusual in practice. The main time it can become a problem is if a hostile client can generate keys, and use this as a form of DoS attack.
 
5:05 PM
Do I really need an unordered_map?
My keys are continous and start from 0.
 
except they don't
 
If an object with key 8 is removed, the next object takes the unused key (lowest unused key).
 
you said you need to respect the number when they get removed
 
yes
I wrote it wrongly but wanted to convey something else.
idk how to parse it
I don't need a particular object anymore
so put its key into a vector which stores unused keys
 
in other words, it's not continuous, unless the first thing you do after removing the item is adding another again
 
5:07 PM
I don't remove or add. I only mark them as used and unused.
My keys are continuous (not the object in the vector)
 
it's starting to smell strongly with XY problem
 
ok.. I'll explain what I'm doing
I am writing a plugin for an abstract machine to provide access to some of the C++ libraries.
The abstract machine, for obvious reasons, is incredibly slow.
Implementing a vector with the abstract machine is going to be 1000x slower (executed on the VM) than an implementation done via plugin (executed directly on the hardware)
I am trying to provide an interface for vectors such that all operations are carried out on it by the plugin using a key.
The abstract machine requests for a vector. The plugin creates a vector and returns a key. The abstract machine uses this key to operate on the vector.
Therefore, the key must not change when new vectors are added or if old vectors go out of scope in the abstract machine code.
There are global vectors. If I remove the object from my plugin's vector container which stores the vector implementation for the abstract machine, it will mess up the keys. When the abstract machine tries to access a global vector using the key which it was given earlier, it will access the wrong vector.
I have a class for the abstract machine vector implementation.
I need to store the objects of that class in a container.
^ all the talk which happened had to do with that container
 
5:23 PM
@Yashas From your description, it would appear that an unordered_map would be nearly ideal.
 
I am lost now. What's the difference b/w unordered map and a vector?
1. vector memory is contiguous
2. vector is essentially a map whose keys are continuous and start from 0
 
I wouldn't worry about re-using deleted keys. In fact, I'd tend more toward keeping them invalid as long as possible, so if a client attempts to use a key after deleting the associated vector, you maximize chances of detecting the problem. I'd tend toward using 64-bit keys consecutively, so you never re-use a key.
 
Abstract machine can be used in 16bit mode
The abstract machine can run up to 24 days.
I don't want to run out of keys.
 
@Yashas With 64-bit keys, you won't. If you run at 10 GHz, and pass out a new key every clock cycle, you'll still be able to run for years on end before you run out of keys.
 
That isn't a choice.
If the abstract machine is in 16bit mode, then the key assigned must be 16bit or smaller so that it can be stored in the abstract machine.
 
5:41 PM
what is the advantage of using an unordered_map over a vector?
It does make the code neat. Apart from that, what other improvement does it make?
 
@Yashas I think making the code actually work without jumping through flaming hoops would count as something of an advantage.
 
I am overly concerned with the performance changes
I can't digest that both vector & unordered_map perform nearly equally well for access.
also constructing my object would be expensive
 
nwp
6:28 PM
@Yashas 16 bit machine doesn't mean you have 16 bits of data. You can make 64 bit keys on a 16 bit machine just fine.
 

« first day (322 days earlier)      last day (2536 days later) »