placement new


Some points about overriding new, regular and placement.
Four items need attention: ctor, dtor, and operator new, delete. see CNew

Regula
caller’s new calls “operator new” and ctor, similarly dtor.
new[] takes only one param as non-array version; the iSize already taken into consideration of array elememt number.

Placement:
also need delete(p, pLoc) so that placement can be handled.
caller delete like this: CNew::operator delete(a3, aPool);
for placement, do not just call delete since it does not know if memory is from application or OS
make sure dtor called for placement.

Download: http://riowing.net/p/wp/other.cpp

Private: scpp ra s “S:\Rio\proj\wordpress\algo\other.cpp” /mnt/ebs/rio/html/riosite/p/wp

atomic_flag


Goal: achive the result of unique_lock without mutex but with atomic_flag
How it’s tested: watch what’s in aOutput
main thread pushes 0 1 to aOutput , and task thread pushed 10 11 to aOutput. 0 1 should be ahead of 10 11
How it’s achieved:

1. By function SyncWithAtomic(atomic_flag& flag, packaged_task<void()>&& task) which runs task based on flag.
2. By busy_mutex as wrapper of atomic_flag
!flag.test_and_set() means we get the lock, proceed to run task and unlock with flag.clear()
otherwise we yield().

Notes: unknown how to use ATOMIC_FLAG_INIT when atomic_flag is a class data member in VS2022. I just used .clear()
these don’t work:

mFlag = ATOMIC_FLAG_INIT;  
atomic_flag mFlag{ ATOMIC_FLAG_INIT };
busy_mutex() : mFlag{ ATOMIC_FLAG_INIT } { } or mFlag(ATOMIC_FLAG_INIT)

Download: http://riowing.net/p/wp/thread.cpp

char8_t


char8_t: new in c++20, holds utf8 encoded byte.
char8_t*: e.g PChar8 = u8″ChineseCharsInUnicode” with u8 converted to utf8
Here Chinese charactor ZHONG, meaning Center, is encode/decoded and printed.

Decode:
Support to decode utf8 like std::wstring_convert was deprecated;
here use platform specific, like windows MultiByteToWideChar(CP_UTF8…, which takes no char8_t
use reinterpret_cast

Printing:
_setmode tells what is going to console, e.g _O_U16TEXT for unicode

Sample data Zhong:
Unicode = 4E2D = binary 0100 111000 101101 (4+6+6 bits)
utf8 = E4B8AD = decimal 228 184 173 = binary 11100100 10111000 10101101
decode by removing the leading 1110 10 10 to get unicode 4+6+6 bits above

Download: http://riowing.net/p/wp/other.cpp

coroutine 3


Goal: see coroutineWaitTest()
We get promise.mValue by calling coroutine_handle like this: crh();

How it’s achieved:

Coroutine cofuncAwait() 

Init: set myPromise by calling co_await CAwaiterPromise, in which await_suspend passes it in and returned by await_resume().
Loop: sets myPromise->mValue and calls co_await suspend_always{}
The caller coroutineWaitTest:
Init: to get myPromiseInCaller:
cofuncAwait() returns CReturnObj, which gives mHandle to crh thru the overriden cast operator.
myPromiseInCaller = crh.promise();
Loop: call coroutine_handle crh(); -> print myPromiseInCaller.mValue

Same as coroutine-2: caller get promise thru coroutine_handle.promise();
Different from coroutine-1 and -2:
-1 and -2: coroutine doesn’t need promise; it just co_yield the value
-3: coroutine get promise thru CAwaiterPromise
Here it has to implement CAwaiterPromise .

Download: http://riowing.net/p/wp/coroutine.cpp