4 Nisan 2018 Çarşamba

std::atomic_signal_fence metodu

Örnek
Şöyle yaparız.
#define CompilerMemBar() std::atomic_signal_fence(std::memory_order_seq_cst)
SystemTime getSystemTime()
{
    LARGE_INTEGER t;
    CompilerMemBar();
    if (!QueryPerformanceCounter(&t)) {
        return static_cast<SystemTime>(-1);
    }
    CompilerMemBar();

    return static_cast<SystemTime>(t.QuadPart);
}

2 Nisan 2018 Pazartesi

gcc extension - __attribute__ destructor

Giriş
 __attribute__ ((constructor)) ile main metodundan önce kod çalıştırmak

 __attribute__ ((destructor)) ile main metodundan sonra kod çalıştırmak mümkün.

Örnek
Şöyle yaparız.
#include<stdio.h>

/* Apply the constructor attribute to myStartupFun() so that it
    is executed before main() */
void myStartupFun (void) __attribute__ ((constructor));


/* Apply the destructor attribute to myCleanupFun() so that it
   is executed after main() */
void myCleanupFun (void) __attribute__ ((destructor));


/* implementation of myStartupFun */
void myStartupFun (void)
{
    printf ("startup code before main()\n");
}

/* implementation of myCleanupFun */
void myCleanupFun (void)
{
    printf ("cleanup code after main()\n");
}

int main (void)
{
    printf ("hello\n");
    return 0;
}
Örnek
Şöyle yaparız.
int main(void)
{
  ...;
}

__attribute__ ((destructor)) void after_main(void)
{
  ...
}

std::packaged_task

Giriş
Şu satırı dahil ederiz.
#include <future>
packaged_task bir metodu başka bir thread içinden çağırmak için kullanılır. Çok alt seviye bir kullanım şeklidir. Bu kadar alt seviye bir işle uğraşmak istemiyorsak std::asynch'i kullanmak daha kolay.

Constructor - lambda
Şöyle yaparız.
std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; });
Constructor - function
int alan ve std::vector<int> dönen bir metod olsun. Şöyle yaparız.
std::vector<int> foo (int n) {

  std::vector<int> v { 2 * n, 3 * n };

  return v;
}

std::packaged_task<std::vector<int>(int)> task { foo };
Constructor - std::bind
Şöyle yaparız.
std::promise<int> promise;    
auto operation = [](std::promise<int>& promise) { promise.set_value(1); };
std::packaged_task<void ()> fn { std::bind(operation, std::move(promise)) };
Constructor - Allocator
Bu constructor C++17 ile kaldırıldı. Şöyle yaparız. Çıktı olarak 1 alırız.
int one() noexcept {
  return 1;
}

int main() {
  std::packaged_task<int()> task(std::allocator_arg, std::allocator<void>{}, one);
  task();
  std::cout << task.get_future().get() << std::endl;
}

get_future metodu
Şöyle yaparız.
std::packaged_task<...(...,...)> task (...);
auto future = task.get_future();
operator ()
Constructor içinde belirtilen metodu çağırmak için kullanılır.

Örnek
Şöyle yaparız. Bu örnekte packaged_task bir başka thread içinde çağrılmıyor.
task(); // invoke the function
Eğer parametre verilmesi gerekiyorsa şöyle yaparız.
task(2,3);
Örnek
Bir başka thread içinde çağırmak için şöyle yaparız.
std::packaged_task<...(...)> task { fooFunc };
auto& future = task.get_future();
std::thread { std::move(task), i }.detach();


future.wait();
future.get();