Git Product home page Git Product logo

ariadnaproject's Introduction

🧶 The Ariadna Project

This is the Win32 threading library with functions, wrappers and classes with support of:

  • Usermode scheduling (UMS-threads)
  • Fibers
  • Normal threads
  • Threadpools

How to:

  • Usermode scheduling:
#include <Windows.h>
#include "Ariadna.h"

DWORD WINAPI UmsThread(PVOID Arg)
{
    // Your code here
}

int main()
{
    using namespace Ariadna;
    UmsScheduler& Scheduler = UmsScheduler::GetInstance();
    
    // Initializing:
    Scheduler.StartUmsSheduler();
    
    // Starting threads:
    for (unsigned int i = 0; i < 5; ++i) {
        Scheduler.StartThread(UmsThread);
    }
    
    ...
    
    // Stopping scheduler:
    Scheduler.StopUmsScheduler();
    
    return 0;
}
  • Fibers:
#include <Windows.h>
#include "Ariadna.h"

int main()
{
    Ariadna::Fibers::CallInFiber([](PVOID) {
        printf("Fiber %p in thread %i\r\n", Ariadna::Fibers::Current(), GetCurrentThreadId());
    });

    printf("Non-fiber main thread %i\r\n", GetCurrentThreadId());
    return 0;
}
  • Normal threads:
#include <Windows.h>
#include "Ariadna.h"

using namespace Ariadna;

int main()
{
    volatile int i = 1337; // Value that will be pass to another thread

    // Run async task:
    Threads::Async([&]() -> DWORD { 
        Sleep(1000);
        printf("[%u] External value = %i\r\n", Threads::Id(), i);
        return 0;
    });

    i = 0; // Changing the 'global' value

    printf("[%u] Value 'i' changed to %i\r\n", Threads::Id(), i);

    // To prevent an app closing:
    MSG Message = {};
    while (GetMessage(&Message, NULL, 0, 0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }

    return 0;
}

or

#include <Windows.h>
#include "Ariadna.h"

using namespace Ariadna;

class CustomThread : public AbstractThread {
private:
    int SampleValue;
protected:
    DWORD ThreadProc() override
    {
        printf("Hi from a CustomThread!\r\n");
        printf("- SampleValue = %i\r\n", SampleValue);
        return SampleValue;
    }
public:
    CustomThread(int Value) {
        SampleValue = Value;
    }
};

int main()
{
    CustomThread thread(1337);
    thread.Start();
    thread.Wait();
    printf("CustomThread is finished with value %i!\r\n", thread.GetExitCode());
    return 0;
}
  • Threadpools:
#include <Windows.h>
#include "Ariadna.h"

using namespace Ariadna;

static ThreadPool CustomThreadPool;

int main()
{
    // Queue to the defaul threadpool:
    ThreadPool::DefaultQueueWrapped(NULL, [&](int a, int b) {
        printf("[%u] Hi from threadpool: %i\r\n", Threads::Id(), a + b);
    }, 10, 20);

    // Queue to the CustomThreadPool:
    CustomThreadPool.CreatePool(8, 16); // Min: 8 threads; Max: 16 threads
    CustomThreadPool.QueueWrapped([&](int a, const std::string& b) {
        printf("[%u] Hi from custom threadpool: %i, %s\r\n", Threads::Id(), a, b.c_str());
    }, 10, "Sample text");
}

ariadnaproject's People

Contributors

fengjixuchui avatar hoshimin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.