Before c++11, there was always a problem with how to get high-precision time, and different platforms needed to be obtained in different ways. Now C++11 provides the Chrono. This is one that can solve all the need for time.Three concepts are presented in Chrono:durations: represents a time interval that can be used in different time units, such as hours, days, and smallest to nanoseconds. You can also customize a special time interval.Time points: sav
New Features of C ++ 11, Using std: chrono to streamline the traditional method of getting system time, stdchronoI. Traditional methods for obtaining system time
The traditional method for obtaining time in C ++ needs to be defined by platform. I believe there are also a lot of Baidu code.
I wrote it myself, as shown below.
const std::string getCurrentSystemTime(){if (PLATFORM_ANDROID || PLATFORM_IOS){struct timeval s_now;struct tm* p_tm;gettimeofday
Convenient tool in C ++ 11-chrono Library (processing date and time)
Chrono is the name of a header, but also of a sub-namespace: All the elements in this header (partition t for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std: chrono namespace.
The elements in this header dea
The so-called explanation is only a reference to the www.cplusplus.com of the description of the collation, because did not find that others have detailed explanation.Chrono is a time library, derived from boost, and is now a C + + standard. This year seems to be a new standard, so look forward to Ah!To use the Chrono Library, #include1.durationsstd::chrono::d uration for a period of time, such as two hours
( date);} Return "";}Second, C++11 STD standard library cross-platform approach obviously, we notice that the code similarity is very high in different platforms, so can we use the new features inside the c++11 to make them merge?The answer is yes.Very simple, the code is as follows:Const std::string getcurrentsystemtime () {Auto TT = std::chrono::system_clock::to_time_t (std::chrono::system_clock:: Now ()
Always feel that the time library of boost is not very good, of course, there may be I do not understand, so, the code out to see perhaps better, time processing, take the current time is really too common, and the various clock in the boost is not clear, however, the code can tell everything,As you can see from the code below, Steadyclock and Systemclock represent different counts at all. QueryPerformanceCounter This function takes the value of the hardware timer, so that the main use of the co
Std::this_thread::yield:defined in the header filefunction prototype :void yield() noexcept; The accuracy of this function is dependent on implementation, especially in the use of the OS Scheduler mechanism and system State. For example, the FIFO real-time Scheduler (Linux SCHED_FIFO ) hangs the current thread and places it at the end of the queue of the same priority thread that is ready to run (and no effect if no other thread is in the same priority yield )Code:1#include 2#include 3#includ
C + + Acquisition timeHeader file Chrono, namespace Std.Now timeStd::chrono::system_clock::now () returns the current time of the system clockClockStd::chrono::system_clock represents the current time of the system, is an unstable clock, and provides a function to convert a point in time to a value of time_t typeStd::chrono
data is increased from 0 to 1, the results have yet to be analyzed.Appendix:Test program EnvironmentSystem: Windows 7.Development tools: Vs208,release compilation.Program:#include #include #include void Test (const int N){std::cout "n =" n std::Endl;std::setint> si;boost::chrono::high_resolution_clock::time_point T1 = Boost ::Chrono::high_resolution_clock::now (); for(int i = 0;I N; I+ +){si. Insert (i);}b
//Logical ThreadsvoidLogic_thread_proc () { while(true) {printf ("_____________ logic begin \ n"); //blocking, waiting for timed wakeup of the render threadUnique_lockLock(LOGIC_MT); Logic_cv.wait (Lock, [](){ returnto_do_logic; }); To_do_logic=false;//This condition can only be reset by the render thread//Logical Tasks ... This_thread::sleep_for (Chrono::microseconds (1000000)); printf ("_____________ logic \ n"); //logical task com
C++11 Language-level threadingCreation of ThreadsCreating a thread with Std::thread is as simple as providing a thread function or function object, and you can specify parameters for the thread function at the same time.#define_crt_secure_no_warnings#include#includestring>#include#includevoidfunc1 () { while(true) {std::this_thread::sleep_for (Std::chrono::milliseconds ( the)); Std::coutStd::endl; }}voidFunc2 () { while(true) {std::this_thread::sleep_
e). Message delayed send (not much different from the front directly on the code)#include #include"caf/all.hpp"#include"caf/io/all.hpp"#includestring>#includeusing namespacestd;using namespacecaf;behavior Fun (Event_based_actor*Self ) { return{[Self] (Const stringstr) {aout (self)Endl; Auto T2=std::chrono::system_clock::to_time_t (Std::chrono::system_clock::now ()); cout"dalay Time:"Endl; Self-quit (); }
library provides time functions, but it is extremely difficult to use. Now the Boost time function chrono has been standardized, although it is still not comparable. net TimeSpan is easy to use, but at least it is much better than standard C.
# Include# Include# IncludeUsingNamespace std;
Int fibonacci (intN){If (n Return maid (n-1) + maid (n-2 );}
Int main (){Auto start = chrono: system_clock: now ();Int
receives a notification or a specified point-in-time abs_time times out. And once the timeout is received or the other thread is notified, WAIT_UNTIL returns, and the remaining processing steps are similar to Wait_for ().//Condition_variable::wait_for Example#include //Std::cout#include //Std::thread#include //Std::chrono::seconds#include //Std::mutex, Std::unique_lock#include //std::condition_variable, Std::cv_statusstd::condition_variable CV;intval
effect as using Try_lock. In fact, Try_lock_for internally is also called Try_lock_until implementation.Tm.try_lock_for (Std::chrono::milliseconds (1000)) and Tm.try_lock_until (Std::chrono::steady_clock::now () + std:: Chrono::milliseconds (1000)) is equivalent to waiting for 1s.Std::recursive_timed_mutex classStd::recursive_timed_mutex on the basis of Std::rec
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.