« first day (289 days earlier)      last day (2570 days later) » 

1:19 PM
hi there
i need help
i've a map
there are 2 threads
one ONLY deletes / adds elements to the map
the second ONLY reads the map
is this thread safe?
 
no
you need a mutex to make it thread safe
 
can you give me an example?
i don't wanna use boost
is this good example?
i would just replace int with map
 
shared_mutex is useless for you
because you only have 1 thread reading
 
thank you
very much
 
nwp
you should still let yourself be inspired by the shared_mutex example because it encapsulates the mutex better (users don't need to care about mutexes) whereas in the mutex example you must remember to do locking every time which will get annoying soon
 
1:28 PM
yeah
so
in the mutex example i need to lock every time i change data right?
 
nwp
you could also think about how to make something that gives you a reference to a map under a lock so you don't need to copy all of std::map's functions
 
when I read data i need to lock it too?
 
nwp
yes, for reading too
 
you must ensure that no other thread can change the data while you are reading, that requires a lock
 
nwp
unless you know nobody else is writing, but that can only be ensured with the mutex in this case
 
1:29 PM
okay, thanks for the help!
 
@nwp I think that's a bit much though for a map shared between 2 threads
 
nwp
@ratchetfreak what is too much?
 
making a map wrapper with automatic locking
 
@nwp you mean a reference to the element or whole map?
 
nwp
@SzymonMarczak whole map so you don't need to reimplement all the functions
@ratchetfreak possibly over engineered, I don't know
 
1:34 PM
@nwp could you give me simple example about referencing to the whole map?
??
do you mean that after locking I need to return the reference of whole map?
i think so
 
nwp
@SzymonMarczak you must also make sure that the lock is only released after the reference is gone
 
to start just keep the map and mutex next to each other and every time you access the map you lock the mutex.
 
nwp
I can't find an example -.-
 
@nwp I understand what you mean, there's no need for the example. that's ok
 
keep it simple to start with
 
1:42 PM
std::mutex mtx;
[...]
mtx.lock();
/ references the map here /
mtx.unlock();
is that good example?
 
@SzymonMarczak No. You want to use std::lock_guard (or one of its cousins), so it's more like:
 
std::mutex mtx;
std::map<...>map;
[...]
{
std::lock_guard g(mtx);
/* references the map here */
}
 
{
    std::lock_guard<std::mutex> m(your_mutex);
    use_map();
}
This way, a normal exit from the scope or exiting via an exception will unlock the mutex.
 
thanks @jerrycoffin !
 
and make sure no references to elements within the map escape that scope
 
1:46 PM
@SzymonMarczak RAII: It's not a pattern. It's a way of life!
 
nwp
@SzymonMarczak Something like this. I think you can get rid of the .get().value part with some operator shenanigans, not sure if it is worth it.
I don't know why the standard library doesn't have something like that done right.
 
because I don't think the lifetime semantics required are properly setup for that to be foolproof
 
@nwp The code is more like a hack!
 
also each invocation of get will acquire and release the lock (granted released only at the ;) so doing multiple operation will very likely turn badly
atomic operations doesn't mean thread safety
 
nwp
@ratchetfreak you can just store the Proxy
and if you store the reference you are screwed
 
1:55 PM
@ratchetfreak what about this: stackoverflow.com/questions/23597999/… ?
 
it's very tempting to do foreach(auto a: pm.get()) but I believe that can release the lock before the iteration starts
@SzymonMarczak the atomic operation itself is thread-safe, sure. But doing multiple atomic operations will lead to race conditions
 
I see
 
the obvious example is a read-update-write operation
when you finally write the result of the update the value may have been changed
 
void read(void (*func)(std::map<...> *map)) {
std::lock_guard<std::mutex> guard(mutex);
func(&map);
};
is this thread safe?
 
2:00 PM
ok
 
hey! i hate to butt in here, but s anyone more or less proficient in OpenGL? Im getting headaches with a problem i have no idea how its caused
 
as long as func doesn't leak the pointer to map (or data contained in it) to somewhere else
@calcyss don't ask to ask
 
nwp
@SzymonMarczak you can add a return value to func/read without threading issues
 
@rat
Excuse me? what do you mean?
 
@calcyss just ask the question don't ask to ask the question
 
2:02 PM
void read(void (*func)(std::map<...> *map)) {
std::lock_guard<std::mutex> guard(mutex);
return &map;
};
so this is thread safe too
??
 
no, once the function returns the lock will be released
 
@nwp what do you mean by that?
 
template<T>
T read(T (*func)(std::map<...> *map)) {
std::lock_guard<std::mutex> guard(mutex);
return func(&map);
};
 
ah well - anyway. I have a small test voxel engine written in C++. The chunk/geometry generation part works perfectly (i checked it multiple times), but i have some immense problems with my camera/view/projection setup. Blocks either render mirrored in the middle or completely upside down, and camera movement has the opposite effect. I have absolutely no idea what is causing it, and i checked the code multiple times.
 
nwp
@SzymonMarczak your read function doesn't let me read properly. For example, I want to be able to say auto value_of_element_5 = read([](std::map<...> *map){return map[5];}); but your read doesn't return anything.
 
2:05 PM
i do have the project up on github if that helps, its not too big.
 
@calcyss for graphics debugging the best way is to cut down on the variables
remove stuff get soemthing that works and then go step by step until it doesn't anymore and find out why
 
yes thats exactly what ive been doing
problem is, i just dont see anything anymore after the slightest change
i also looked up how other people generated their matrices, and couldnt see any mistakes in my code
 
@nwp oh, I understand what you mean, but func called by read doesn't have to return anything for me, it will just do a few operations on the map
that's all
 
@calcyss order of multiplication matters as does column/row majority
 
i did check that multiple times
 
2:09 PM
by the way, what does std::owner_less as 3rd argument of the map stands for?
for example:
std::map<key, value, std::owner_less<key>>
 
opengl, or rather glm, uses column major matrices. So for a MVP matrix i'd have to multiply P * V * M
which is what i am already doing
 
so it's only for ordering the map?
 
it's the sort order (map is a really an ordered map)
 
It's a comparison function for ordering the elements by key, the default one is std::less<key>
 
2:11 PM
which (by default) uses the less than operator to compare keys
 
okay, thanks for the explanation!
 
sigh
im just gonna give up then
i tried fixing it for hours already
 
maybe you need to relax
as sometimes I do
@calcyss maybe send some code here?
if you're able to
 
2:33 PM
@calcyss it's all about projection/view/model/whatever matrices, pay really close attention to what other people are doing, we were building game engine with friends from scratch with opengl for almost two years now
these kinds of problems are really annoying but it's more linear algebra than programming really, pay really close attention to your c++ code, shaders (glsl) and what you are expecting to see
 
adding debug visuals tends to help a lot
 
using something colorful like this as a texture for your quads helps a lot with rotations libpng.org/pub/png/img_png/AlphaEdge.png
also, make sure to know whether or not these variables are set: GL_CULL_FACE, GL_DEPTH_TEST
 
yay my server is now thread safe!!! it doesn't crash if there's large amount of disconnections (im using multiple threads)
 
programmer art happens for a reason
just because it doesn't crash doesn't mean it's thread safe though
 
@SzymonMarczak especially talking about multithreading, don't be sure about crashes in your project after just one test launch without proper testing
 
2:46 PM
@ratchetfreak @login_not_failed I use ASAN and valgrind to check if it's thread safe
 
@SzymonMarczak there are a lot of really obscure bugs that would happen unexpectedly, even when you use said projects to help you
 
there are not tools to exhaustively check whether an application is thread safe
 
yea, it's a whole another level of bug hunting, tools won't really help there
 
whether something is thread safe is halting problem level of hard
 
hmm, always if something goes wrong (for example: memory leak or deleting the same pointer 2 times by 2 threads (because they hadn't have lock yet), or accessing deleted pointer) ASAN tells me about that
that helps a lot for me
but I agree with @ratchetfreak
 
nwp
2:50 PM
@SzymonMarczak should be using Tsan
 
@nwp You're great! I didn't know that TSAN exists!
 
@SzymonMarczak there are different bugs like the classic one, when you access a variable from another thread when it is not initialized yet — you would get garbage in the best case
 
@ratchetfreak level hard? For me that was super hard
 
the halting problem is impossible to solve in the general case
 
agreed
10000% true
ASAN doesn't slow down the app so much, but will TSAN do?
@nwp how can I add TSAN to my cmake project
?
 
nwp
2:55 PM
@SzymonMarczak it says that right on top, significant slowdown and memory usage increase
the most costly thing though is that you must annotate your code properly to make it work
 
can a library be both static and shared at the same time? meaning it is self sufficient and other libraries can refer to it. on stackoverflow, looking at some answers made me think that a library can either be shared or static.
 
@nwp I've noticed the memory usage increase (up to 1GB), but significant slowdown? I haven't noticed it yet
 
nwp
> Typical slowdown introduced by ThreadSanitizer is about 5x-15x. Typical memory overhead introduced by ThreadSanitizer is about 5x-10x.
 
@RaviUpadhyay No
@nwp I use this for ASAN: github.com/arsenm/sanitizers-cmake
 
nwp
@RaviUpadhyay being self-sufficient and having other libraries refer to a library have nothing to do with a library being static or shared
 
2:59 PM
shared library means, it references some other library?
 
nwp
@RaviUpadhyay no
@SzymonMarczak wait, I confused it with this one
 
nwp
@SzymonMarczak I have no clue.
 
in the answer, shared library is described as "All the code relating to the library is in this file, and it is referenced by programs using it at run-time". This implies self sufficiency. But this is not the case.
 
"shared" typically means dynamically linked
which is independent of whether it needs another lib (self-sufficiency)
 
nwp
3:10 PM
@RaviUpadhyay static vs dynamic libraries
 
ok. so static and dynamic/shared only decides, that whether the program using it, takes a copy for itself, or references the original library for future use.
another question that i have is, when we do, gcc -c somefile.c -o sf.o, is the resulting file a static library or shared one?
 
nwp
@RaviUpadhyay it also decides if the library is included in the program which decides if you need to keep the library around
 
TSAN is already build in: github.com/arsenm/sanitizers-cmake/blob/master/cmake/… (ASAN port to CMAKE)
 
@RaviUpadhyay the resulting file is an object file which needs to be linked into a library/executable
in some implementations static library is just a archive of object files
 
ahh yes. i just saw somewhere a code, doing the gcc -c, and then archiving it into .a file and then saying this is a static library.
 
3:23 PM
@nwp TSAN fails on Ubuntu 16.10: FATAL: ThreadSanitizer: unexpected memory mapping 0x560a1068a000-0x560a106ac000
 
nwp
@RaviUpadhyay you are supposed to use -shared to make a shared library
@SzymonMarczak do you also have Asan or valgrind running? I think they are incompatible with each other.
 
@nwp yes, im running ASan for now
if asan doesn't give me what I want I switch to valgrind
summary: Im running asan
 
ok. understood, thanks @nwp @ratchetfreak @SzymonMarczak
 
nwp
@SzymonMarczak try without
 
running with these flags: -DSANITIZE_THREAD=On -DSANITIZE_ADDRESS=Off
FATAL: ThreadSanitizer: unexpected memory mapping
I think I need to leave ported sanitizers to cmake alone
 
nwp
3:29 PM
@SzymonMarczak possibly a bug
 
I think I need to compile the sanitizers again
@nwp You're right.
The ported sanitizers are just 99% real sanitizers, that guy from github.com/arsenm/sanitizers-cmake just compiles them his way
but I don't think that'll change so much
Yeah, that's a bug, indeed.
I think so
how i can run thread sanitizer other way? or there aren't any other ways to fix it?
how i can stroke strike text? [nvm]
ported sanitizers They aren't ported. That guy doesn't compile them. I was mistaken. He just finds them.
just forget what I said before lol
 
3:55 PM
us where its hosted...
 
4:46 PM
Hello?
 
4:59 PM
Hey guys, const int* is a constant pointer to a non constant int, right?
class is_ub
{
private:
const int* ptr;

public:
is_ub()
:ptr(new int[10])
{
memset(const_cast<int*>(ptr), 0, sizeof(int) * 10);
}
};
Does the constructor invoke UB?
 
nwp
@Trauer other way around
 
Hum?
What do you mean?
 
nwp
mutable pointer to constant int
 
Humm
 
nwp
and it is not UB since the integers allocated by new are not top-level const, so the const_cast is allowed
 
5:06 PM
How can I declare a pointer that always point to the start of an array? But the array itself is mutable
i see
 
nwp
int *const ptr
const refers to the thing left of it, if there is nothing left of it it refers to the thing right of it
probably an over-simplification, but good enough for pointers
 
I see
If I change my class to int const* ptr
It still safe to const_cast, right?
 
nwp
you wouldn't need to
 
const int * ptr is that same as
int const * ptr
 
nwp
right
 
5:10 PM
Thanks nwp :)
Last one
std::vector<whatever> *const ptr; is a immutable pointer to a mutable vector of whatever?
Is it common to see such constructs? const pointers to mutable stuff?
 
nwp
@Trauer it is not common to see const pointers
@Trauer you can test it
 
Humm... I'm using them in my classes
Just to make sure that I don't accidentaly change a pointer and lose a reference to something
 
nwp
also const members in classes are not nice since they prevent you from writing a proper assignment operator
 
That wouldn't be a problem, since I'm deleting copy and move constructors and operator=
It's a simple class to help me move buffers from "normal memory" to "gpu memory".
I'm making the pointers to buffers const just... I don't know... To be safe
 
nwp
@Trauer that sounds like it should be a function instead of a class
 
5:15 PM
Okay, maybe i oversimplified
It 'maps' memory from on side to the other
it contains functions that modify host and device memory at the same time
there are some async calls
I thought it would be 'nice' to wrap it all in a class
Can I use decltype to get the type a pointer points too? Is it well defined?
I.e.:
size_t* p1;
decltype(*p1) v1 = *p1;
assuming p1 is pointing to something
 
nwp
sure
might want to consider auto v1 = *p1;
 
That would be nicer
humm
thanks :D
sizeof(decltype(*p1)) == sizeof(size_t)?
Lol iwll test in that website you posted earlier
 
5:57 PM
sup? How do static variables in templates and generic lambdas work?
1) Does every instance of template gets own variable? How does this work with lambdas if it's true?
2) Is it different if static variable type depends on template argument or lambda argument?
 
nwp
@EuriPinhollow you get one per instantiation
 
Same for generic lambdas?
 
nwp
yes
 
Well yuck. What generic lambda instance gets passed when I try to pass it?
Oh I may as well try it.
Kinda understand it now. std::function is a template and whenever I pass a lambda to something templated it gets instatiated according to what I pass it to.
 
nwp
6:17 PM
@EuriPinhollow you explicitly specify the template parameter for std::function, the lambda's type doesn't matter there
 
That's part of what I mean. Whenever I pass lambda somewhere the argument types get known and it's instantiated.
Am I wrong in that the std::function can appear in templated function arguments without own template arguments?
 
nwp
@EuriPinhollow yes
 
6:36 PM
@EuriPinhollow It's possible to pass an un-instantiated template as an argument to another template (it's called a template-template parameter). template <typename T, template<typename> holder> foo, then instantiate as: foo<int, my_template>(...) is fine (and inside that template, holder will mean "my_template". Default template parameters can be tricky though. e.g., vector isn't a template<class>, it's a template<class, class>.
 
@JerryCoffin Thanks, that's insightful.
 
@EuriPinhollow Oh cool. People more often find me inciteful... :-)
 
@JerryCoffin it flew over my head and it ain't stopping. :3
 
@EuriPinhollow Incite, v., to stir up or encourage conflict or other violent/illegal behavior...
 
6:51 PM
@JerryCoffin sure I know that, but why would people find you inciteful?
 
@EuriPinhollow Because I sometimes find it humorous to do some borderline trolling (or maybe I don't, and just thought it was a cute play on words...sometimes I'm uncertain).
 
Makes sense now. Too meta for aLounge novice though.
 
 
3 hours later…
10:15 PM
I have a local code which has two very similar branches (there are few additional lines of code and few more members in class definition in one of them). Is it nice to put that into generic lambda and put enable_if in certain places?
 
sure why not
with classes such reuse is more difficult, but it's also possible
 
Classes are local.
Ok, fine. But then, how do I pass a runtime value as template argument? Should I make switch-case?
 
well, you can't
sometimes you can do such a dispatch (switch/case, or a table of function pointers), but it's not worth it IMO
 
That was my clue. So, switch-case with duplicated case values as template arguments, right?
 
duplicated case values?
 
10:23 PM
switch(whatever) { case 1: somefunction<1> ........
 
yeah, that's an option
another would be to produce an array of function pointers with some clever application of variadics
 
wat wat wat wat wat wat wat wat wat
Oh wait I kinda understand it.
Something like fpArr[i]=somefunction<i>?
Oh wait that's not how it works.
i is included in type.
Ah, but it does not matter as long as function return type and arguments are same.
I see.
Thanks!
 

« first day (289 days earlier)      last day (2570 days later) »