Matlab functions and parallel_invoke [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In my project I have a lot of MATLAB functions. For each function I call Initialize function, when the application starts. I tried to call this functions using parallel_invoke. I tried it several times and allways it takes more time, that code without this. Can somebody explain this ?
Is there is something specific in MATLAB or Initialize functions ?

The Matlab Runtime only has a single interpreter thread, so calling Matlab functions in parallel does not gain you anything: when the first function A is called, the MCR acquires a lock and only releases it when that function exits. Calling another function B during that period results in trying to acquire the lock, which then obviously just blocks until A finishes. The reason you see it taking up more time is probably due to the overhead of the locking/parallel_invoke.
I'm not sure what you mean with for each function I call Initialize function: unless you are using multiple Matlab dlls (which will afaik be less performant than having a single dll) you only need to call it's Initalize/Terminate once.

Related

Is it okay to shorthand a function call using a macro? C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am slowly writing an emulator for a gameboy using C++, I am currently working on the CPU side of things. I have written a generic function that takes any two registers of the CPU and returns as a Word data type. As it is necessary to access individual registers and also a combination of registers.
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
calling this function gets tedious as you have to specify each register
get_word(&this->registers.h, &this->registers.l)
My question is if it okay to define a macro like so
#define get_HL() get_word(&this->registers.h, &this->registers.l)
since now I can call it using
get_HL()
The reason why I want to do it like this since I don't want to create more private/public functions that just perform function calls.
I have tried compiling and it seems to work as it should since its just a pre-processor macro but I am not sure of the design implication
EDIT:
Okay I mean there are glaring flaws with this and you should just make a function, just as much work to make a function or write a macro.
const Word get_HL() { return this->get_word(&this->h, &this->l); };
Let this be a post for people who had the same idea and hopefully stop making the same mistake
No, this isn't OK in my opinion. It hides what arguments you're passing into the function, and macros don't respect scopes and as such are highly susceptible to name conflicts. This seems like an ideal use case for a non-static member function.

(mutex) lock for callback function C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In a multi-threaded application, mutex::lock is used to prevent two threads accessing the same memory location at the same time. In my application, I do not use multiple threads but I have a callback function that writes to a vector and a loop in the main function, that reads it. Sometimes, I get this error:
Vector iterator + offset out of range
After checking the vector in debugging, all seems to be fine: the number of elements is right and none of the elements in invalid. Is there a similar possibility for callback functions?
No, there isn't. Mutexes are irrelevant for this case. Your out-of-range exception has nothing to do with concurrent access; it's just a logical error in your code. Find and fix that error, using your debugger.
If you don't have any concurrent calls to your callback function, there's no need for a std::mutex.
Is there a similar possibility for callback functions?
No, a callback function is called in sequential order, if there aren't any concurrent threads.
If you have out of range errors, use the debugger and check the index values used to access the vector elements.

Is it possible to send a pointer to function via socket? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two processes of the same program, possibly running on two different machines.
I'd like the process P2 to receive a function pointer from process P1 via socket.
Is is possible? Is it a good practice?
EDIT: more interesting would be to send the code of the function too, but I'm skeptic about this. Would it be possible?
You can send a function pointer from one process to another, the same way you can send a pointer to some other object.
The problem is that the pointer may not actually point to the function as it exists in the target process. Especially if the OS is protecting itself with things like ASLR.
You could also send the code across, provided you had some way of figuring out where it ended, and that it was position independent code, and that your environment allowed you to write arbitrary data to memory and then call it.
But, to be honest, there are better ways to achieve what you seem to want, such as the use of RPC (remote procedure calls), in a more portable manner.

how to create more then one thread at a time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone tell me how to create more then one thread at a time
is there any such ?
like:
pthread_create (thread, attr, start_routine, arg)
should give me more than one thread at a time.
Your actual question seems to be more like "How can I make sure my tests exercise a potential race condition by calling the same function at the exact same time?"
The answer is, you sort of can't. Imagine your test runs on a single-processor machine. There's no way for the function to be called twice at the same time (but it could partially execute once then start again in the other thread).
Think about other ways to test for race conditions. At the more formal end of the spectrum, you can use tools like Spin and Promela to verify such things, but it's a lot more involved than spawning two threads in a unit test. For more, see here: http://lwn.net/Articles/243851/
A less formal way is to call your function in some way where it will sleep in its middle when the first thread calls it. Then you're very likely to see execution continue with the second call while the first one is suspended. But be mindful of wasting time on this approach, because it mostly validates that you've written the test correctly, and may not prove anything much about the thread safety of your code in practice.

Default exit function implementation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to implement default behavior of exit call. I don't know what should I do and what is the most suitable way to do this. I have read that it should close file descriptors and something else.
Should I close default streams (stdout,err and in) ?
How to exit from nested functions calls ? Using goto is bad practice, what is the best way to break out ?
Thanks.
Do all of the things listed in exit(3), then invoke the _exit(2) system call. Alternatively, use longjmp(3) to jump back up to the main() function, then return from it. This invokes the same behavior as calling exit(3), and is just as dependent on the C runtime, so if exit(3) is unavailable for some reason, returning from main() will probably not work correctly either.
Unfortunately, AFAIK there is no portable way to enumerate all of the functions which may have been registered with atexit(3) and on_exit(3), so you'll have to keep track of those manually (i.e. every time you call atexit(3) or on_exit(3), append the function pointer to a list). Flushing stdio(3) is 3 straightforward fflush(3) calls.
You do not need to close any streams or file descriptors; the OS should do that automatically (the OS must not leak streams and fd's, so it is responsible for cleaning them up).
NB: longjmp() is almost always wrong under C++; throw an exception instead. It generally should only be used under straight C.