Is there a better way to generator/publish data at given Frequency other then using the clock() function [closed] - c++

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).

Related

Is there any chance that CLOCK_REALTIME changes time automatically? [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 7 months ago.
Improve this question
I'm using linux api clock_gettime(realtime) to print current time. Check log index ending with 649 and 917.
The real clock time was 1646948676.999081502(at index 649)
,but after 10 secs it suddenly jumped more than >24 hours back and was 1646860487.614595043(index 917).
Main thing i wanna know is what is VuC offset? Will VucOffset affect this realtime?
and
Could you please explain what is the reason for time jump? and how can we avoid this?
221649 2022/03/10 02:44:37.000000 PF_CLOCK_SYNC CLOCK_MONOTONIC_RAW=35.228995663 s CLOCK_MONOTONIC=35.228996808 s CLOCK_REALTIME=1646948676.999081502 s
230583 2022/03/10 02:44:46.000000 receiveTMPData data received !!
230584 2022/03/10 02:44:46.000000 TMP::Data received is: -56563019
230585 2022/03/10 02:44:46.000000 TMP:TM_OUTPUT_VUC_OFFSET IS SET!!
231917 2022/03/10 02:44:47.000000 PF_CLOCK_SYNC CLOCK_MONOTONIC_RAW=45.230177586 s CLOCK_MONOTONIC=45.230296214 s CLOCK_REALTIME=1646860487.614595043 s
CLOCK_REALTIME is your classic "wall clock", similar in spirit to what is returned by the older gettimeofday() function call. In particular, it is non-monotonic, which means that it can be expected under some circumstances to 'jump' forward or backwards by an arbitrary amount.
Generally "some circumstances" will be limited to "immediately after the local user calls settimeofday() or by some other mechanism changes his computer's clock setting"; but it is also possible for an automated clock-synchronization (like ntpd or ptpd) to automatically adjust the clock forwards or backwards if it feels the need to do so. (I wouldn't expect either of those to adjust the clock by 24 hours unless they decided that the computer's existing clock-value was 24 hours off from the clock they are trying to synchronize it to, of course).

Read only last message from Kafka topic [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 4 years ago.
Improve this question
I am trying to send a video stream through a Kafka topic and doing some processing between each messages consumption. This processing takes approximately 20 milliseconds, so after 100 frames, I have a consequent latency. I can detect this latency, however, I need to be able to force my consumer to read from the latest sent frame from the moment the latency is detected. I am aware that this would lead to many frames skipped.
I am using cppkafka which is based on librdkafka.
I can't find a lead that will put me in the right path since most of the answers are either different from my problem or use concepts existing only in other languages APIs.
Any thought on the matter ?
A simple, if kind of brute, way to do it is to check the number of frames in the queue you have. If above a certain upper threshold then do a tight loop where you "consume" and discard (i.e. does no processing at all) of the frames until you reach your lower threshold (in your case one).
Once that tight loop is done you resume normal processing of the queue.

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.

Elegant way to excecute many alarm clocks? [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
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.

Cheat-Proof techniques for Online Game(TCP) [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 9 years ago.
Improve this question
I'm creating a game and i'm really worried about hacking, so i'm writting a anti-cheat inside the game, i searched how the hacks now-a-days are written and i found some patterns, first of all, almost every hack uses a thread(Usually with CreateThread()) to create the checking loop(check if hack is on/off), do any of you have a good way to check if the thread is not from the game?PS:I also use threads from outside the code(inside DLLs)
It is almost certainly better to find a different way to "anticheat", since as cdhowie points out, there's nothing to prevent someone clever from splatting over your anticheat code with something that doesn't do what you want.
There are some techniques that work much better:
Let the server do all the "important" stuff, such as figuring out what score you get and how many lives you have left, and if the player becomes a zoombie (tries to move after he's dead), something is wrong.
Another method that I think works reasonably well is to basically record a "log" of how the player got to where they are - how many lives they used, how many enemies killed, what type of enemy, score of each enemy, weapon used, shots fired, how long it took to do all that, and then let a server verify that it's "reasonable" - so if someone ups the number of lives they have, or changes the weapon to create 100x the damage, or slows down the enemies or speeds up the players time, it will show up in the "log", and the log can then be discarded as "fake".