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
Let's say I have a vector of exact future times of multiple events. I want those events to launch when their respective times have been reached. How do I do this without constantly using if statements to check that the current time is one of those set times? This constant checking, especially if many other things are running in my program (and some of those times may be a bit away from now) will reduce the performance of my program. I'm using c++11, latest version of GCC.
If you're using C++11, it's probably easiest to create a thread, have it sleep until the next alarm time. The most efficient way to store the times for the alarms is probably a priority queue.
If you don't have C++ 11 available, you might consider Boost Threads instead. The standard's threads are based closely on Boost threads, so using them will be fairly similar.
If you don't want to use that, you'll pretty much need to use operating system facilities to do the job. With almost any reasonably modern OS, you can create a thread and have it sleep until the next alarm time, but you'll probably also have something that supports what you want a little more directly (call a function at a specified time). The details of that will be specific to the OS though.
Related
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 3 years ago.
Improve this question
I have to generate & publish data (as protocol buffer messages) with a frequency of 1Hz and I have been thinking of alternatives to just sticking it into a While loop with a clock() function. Maybe I am over thinking the issue but any advice on the matter would be much appreciated thanks
There are many ways to call a function on a regular interval. Which one is best depends on the situation and needs of the program. There's no single "best way".
A few possibilities (there are many more):
Sit in a loop and sleep for some duration every time round the loop. Simple, easy to understand. But rarely the best solution, since nothing else can happen while you sleep.
Sit in a loop waiting for some event to occur. Like a timer event. Process each event as it arrives - if not enough time has passed, ignore the event (maybe).
Set up a timer with a callback function that will call the function at regular intervals.
If your intervals are very tiny, then maybe spin / busy-wait in a loop and check elapsed time each time around the loop and do something when enough time has passed (rarely a good idea since it will burn a lot of CPU time doing nothing, but sometimes it's the right option).
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 7 years ago.
Improve this question
I am a hobby programmer interested in dynamic systems like game engines etc. and I had the Idea of a task scheduling system using something like a priority queue to sort different tasks dynamically and maybe include a parallel feature to use multiple cores efficiently. My explicit idea was to use some kind of Task class that itself stores a function pointer and two queue parameters, one being the gravity of the task and one being the time since it was pushed onto the queue, which then would be multiplied to archieve the position in the listing.
Now here comes my question. Would such a system be more efficient in general or at least pay up in any way in comparisation to a hard-coded system (like some 'main loop')?
e.g. is it a better solution / is it faster?
Thanx for the replies.
This is exactly what priority queue's where designed for. Start your design with priority queues and see how well it goes. Then you may want to tweak it if specific issues come up.
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.
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
How can we give a process in taskmanager (like notepad.exe) as an input as process for my Bankers Algorithm (Deadlock detection) ???
It's going to be hard and probably unfeasible to keep track of all the OS / external conditions to implement a real deadlock-prevention algorithm on a real application. Modern OSes (when we're not talking about RT-aware systems) prefer not to implement such algorithms due to their overwhelming complexity and expensiveness.
In other terms you can get away from a Windows deadlock, in the worst case, with a simple reboot. And given how many times this happens it isn't deemed a huge problem in the desktop OSes market.
Thus I recommend to write a simple test case with a dummy application that will either
Serve your purpose
Allow you to know exactly what's being used by your application and let you manage the complexity
As a sidenote: applications like notepad.exe or similar are not real-time processes even if you give them "Real Time" priority in the Windows task manager (and not even soft-real time). Real real-time processes have time constraints (i.e. deadlines) that they MUST observe. This isn't true in any desktop OS since they're just built with a different concept in mind (time sharing). Linux has some RT patches (e.g. Xenomai) to render the scheduling algorithm in the kernel a real real-time one, but I'm not aware of the status of that patch right now.
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 9 years ago.
Improve this question
i'm learning c/c++ and i'm wondering if it is possible to detect a process by it's name and kill it when it's cpu/memory usage exceed a certain value. I would apreciate any help with the actual code or just pointing me in the right direction.
In recent versions of Windows, you can ask the OS to take care of this for you -- create a Process Job Object and configure limits. The accounting features of the job object will let you track resource usage.
In Linux/Unix you would use ulimit.
Don't try to enforce this yourself. If you have a runaway process, the most likely scenario is that your enforcer won't be scheduled in a timely manner to kill it. You really want help from the kernel, and in particular the thread scheduler.
For Windows and c++:
Memory Usage
Check out GetProcessMemoryInfo. There's also an example at msdn.
The GetProcessMemoryInfo() function will provide you a pointer to a PROCESS_MEMORY_COUNTERS struct which contains all of the memory usage information.
CPU Usage
For CPU usage it's a little bit more difficult but the following stackoverflow question is related to that: Getting current cpu usage in c++/windows for particular process
You need to create a process to monitor your other processes.
To do this in Linux in c++:
You can read from /proc/stat directly, or create a process to run 'ps' and parse its output to find processes with high %cpu. To create a process that runs ps, you'll need to use the 'fork' command.
Then you can call 'execvp' to call 'kill ', a function from .
Same idea goes for the bash script (this is probably quite a bit easier though).