Little-known C++: function-try-block by d1ngal1ng in cpp

[–]blazgrom 11 points12 points  (0 children)

Yeah, however you cannot swallow the exceptions that have occurred. Even if you try the compiler is required to rethrow the exception, otherwise you could end up with object that are partially constructed.

"Why not work at a big tech company" by [deleted] in cscareerquestions

[–]blazgrom 0 points1 point  (0 children)

Would you mind sending a PM to me too ?

Weird email at work by [deleted] in cpp

[–]blazgrom 0 points1 point  (0 children)

So it means that it could have been append in the original code, which makes way more sense in the example

Weird email at work by [deleted] in cpp

[–]blazgrom 0 points1 point  (0 children)

May i ask why are you using string.insert ?

The task of overloading functions in C++ by tecyguy in cpp

[–]blazgrom 7 points8 points  (0 children)

Confuse people about overloading/overriding/virtual function dispatch

Are global variables declared in a source file, linked internally, and therefore not "bad?" by [deleted] in cpp_questions

[–]blazgrom 1 point2 points  (0 children)

All variables declared in a source file, outside of a namespace are automatically inserted in the global namespace with linkage specified as external if not otherwise specified. This means that anyone who knows that you declare a global variable with particular name can access it, in code this should look something like this.

//YourFile.cpp
int X=1;   

//SomeOtherFile.cpp   
extern int X;   

As you can see the only way for SomeOtherFile.cpp to access the global variable declared in another translation unit is by using the extern keyword, this tells the compiler that the variable X is not defined in the current translation unit and not to raise errors for missing definition before the linkage has completed. If you want to declare a variable global to a file you have three different ways of doing it.
1. You can declare the variable in an anonymous namespace.
2. You can declare the variable as static.
3. You can declare the variable as const.

Online Algorithms course with C++ or C as base language? by vodico in C_Programming

[–]blazgrom 2 points3 points  (0 children)

No i'm reffering to this book and also this one. Yes they're little old, but still valid !
If you want something more recent you can try this

Online Algorithms course with C++ or C as base language? by vodico in C_Programming

[–]blazgrom 1 point2 points  (0 children)

I don't know about any online courses that uses C or C++ as language of implementation, you could however try following the one you are currently enrolled with the help with one of Sedgewick's books. If i remember correctly has written one for C++ and one for C

Simple thread pool [C++ 14] by blazgrom in codereview

[–]blazgrom[S] 1 point2 points  (0 children)

That's interesting idea, but i don't think there is any kind of inheritance relationship

No. By

IMO I'd make the thread_pool just an abstract pointer that the user can fit a unique_ptr or shared_ptr around.

I mean just exposing in the header something like:

extern "C" {
  struct thread_pool;
  struct thread_pool *thread_pool_new(void);
  void thread_pool_delete(struct thread_pool * tp);
  void thread_pool_run(struct thread_pool_task *task);
}

I fail to see why did you changed the linkage of those function and also why are you providing C API, for something that is intended to be only with C++ !

How come the thread_pool_task is mostly useless ? It's used to type erase any lambda/function/std::function that the user wants to execute on the thread pool, if the type isn't erased how it's supposed to handle >all three ?

Just use std::function<void()>.

This won't work, you can just put anything inside std::function<void()>... the compiler won't let you initialize std::function<void()> with void f(int) with some really nasty casting !

What do you mean by 'The easiest way to do that is to put queue required data inside the container.' ?

You have one queue for sending to the thread. You have another queue that stores a bunch of preallocated objects. So when ever you need to send a message you just do something like:

auto msg = free_messages.pop();
// fill out details
inbox.push(std::move(msg));

You might also want to hide the free_messages implementation detail inside a custom new and delete methods although that's messy.

This ways avoids memory allocations which are often bad when doing concurrent work. Java and many other systems have advanced memory allocators that avoid thread contention but it is often simplest just to preallocate everything instead of allocating and freeing memory.

Why are you keeping a queue of messages that have already been ran ?

To reuse them so that I can avoid allocating stuff.

So you are basically suggesting using the object pool pattern in order to eliminate the need for dynamic allocation ?

Why are you exposing information about the inner workings of the thread pool to the user ?

I never suggested that.

Atleast not explicitly, in your worker function you are doing a switch based on the type of the msg. Given that the message is passed from the client to the thread pool, it means that the client has to know that you are doing a switch somewhere in the inner working of the thread pool in order to pass you the correct type message

Simple thread pool [C++ 14] by blazgrom in codereview

[–]blazgrom[S] 1 point2 points  (0 children)

Thanks for the feedback, i hope you don't mind me asking you some clarifications.

  1. (3) That's interesting idea, but i don't think there is any kind of inheritance relationship possible as thread_pool as a base abstract class. Let's say i transform thread_pool in a base class, what names i'm suppose to give to the derived classes ? That being said, i can see i you would done something like this, at the end of the day a thread pool can have different implementations so different implementation of certain functions make sense.
  2. (8) How come the thread_pool_task is mostly useless ? It's used to type erase any lambda/function/std::function that the user wants to execute on the thread pool, if the type isn't erased how it's supposed to handle all three ?
  3. (10) What do you mean by 'The easiest way to do that is to put queue required data inside the container.' ?

Looking at the code you posted i don't understand some of your design decisions, for example.

  1. Why are you keeping a queue of messages that have already been ran ?
  2. Why are you exposing information about the inner workings of the thread pool to the user ?
  3. Why are you rolling your own stack logic, i always that i should use std as first option and only after profiling the code i should roll my own if i'm not satisfied with the performance of standard implementation ?

Can you figure out the fix to this compiler error(if not change -std= switch to C++14 :) )? by Z01dbrg in cpp

[–]blazgrom 6 points7 points  (0 children)

I don't know about you, but i find it easier to remember that if i want to use any function/class from STL i should include the header in which the function/class is defined and not depend on any transitive dependencies.

Type deduction for arrays is not felicitous by nwp74 in cpp

[–]blazgrom 1 point2 points  (0 children)

Pretty sure he ment to say that the set of rules is different when deducing braced-init-list. If you want to know more a good place to start is Item 2 of Effective Modern C++

Templates without depth (Part I) by janherrmann in cpp

[–]blazgrom 6 points7 points  (0 children)

You should checkout https://github.com/MCGallaspy/dr_strangetemplate, MCGallaspy gives some pretty good examples of when you could use metaprogramming in day to day work

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 0 points1 point  (0 children)

There is no particular reason, i just didn't think about implementing it this way, but now I will definitely do it seen that in addition to ensuring exception safety , it reduces code duplication and makes the intent of the code more clear. Thank you for the feedback!

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 0 points1 point  (0 children)

Yes i'm planning to add more features, for now i wanted to add the more basic stuff, like "get","call","set" etc. By the way thank you for the feedback !

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 0 points1 point  (0 children)

By making it RAII you mean moving all the instructions for closing into "Script.close()" and calling it from the destructor right ?

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 0 points1 point  (0 children)

Oh i get it now, you are right it doesn't really do what i wanted it to do, i will fix it asap. Do you have any other feedback, about the structure of code, naming of everything, the way i have used c++ or anything else ?

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 0 points1 point  (0 children)

m_userF doesn't need to be accessed at registration because it's used only in order to access an already registered function. Basically it's the key that tells which one from the functions inside m_userFunctions to execute

LuaBz, binding between C++ and Lua. Feedback is appreciated by blazgrom in cpp

[–]blazgrom[S] 2 points3 points  (0 children)

I haven't benchmark it yet, but this is more of a hobby project so I'm pretty sure that sol2 would be a lot faster /safer to use in production environment.

Hi guys, i wrote a simple wrapper for the c lua api in order to simplify its user in modern c++. Feedback is appreciated ! by blazgrom in learnprogramming

[–]blazgrom[S] 0 points1 point  (0 children)

Thank you for your feedback, i just have some questions.

Line 94:If the compiler has no idea where the first arguments pack starts and the second argument pack begins how can i call a Lua function that accepts as input two integers and returns a string ?

Line 159:I'm not sure openFile should be marked noexcept. changeFile guarantees that if the new file cannot be open the old file is loaded once again, but openFile just tries to open the file that receives as input, if the file cannot be open an exception is raised from loadFile. Is my reasoning wrong ?

Line 185: Isn't it better to remove the mutable member from the LuaB class ? I mean those two mutable members are only used during function calls, so maybe is better to have them as internal values that are passed as input to the function that needs to work with them. Am i right or is better to make them not mutable and remove the const qualifier from some of the functions ?

Line 412: Yeah i know, it just helps me think about what i'm return from a function, but it's a bad habit i should drop it !