Skip to content

Instantly share code, notes, and snippets.

@jintgeorge
Forked from Justasic/test.cpp
Created March 5, 2024 22:20
Show Gist options
  • Save jintgeorge/33f39372462dc0f3e1a36370c3f9f419 to your computer and use it in GitHub Desktop.
Save jintgeorge/33f39372462dc0f3e1a36370c3f9f419 to your computer and use it in GitHub Desktop.
Use C++11 features to implement a function queue which calls functions later but queues them up in a FIFO queue. Compile with: clang -std=c++11 -stdlib=libc++
#include <iostream>
#include <functional>
#include <queue>
// These includes are for later experimentation
#include <thread>
#include <atomic>
std::queue<std::function<void()>> funcs;
template<typename _Callable, typename... _Args>
void QueueFunction(_Callable&& __f, _Args&&... __args)
{
std::function<void()> func = std::bind(std::forward<_Callable>(__f), std::forward<_Args>(__args)...);
funcs.push(func);
}
void CallFuncs()
{
while(!funcs.empty())
{
std::function<void()> func = funcs.front();
funcs.pop();
func();
}
}
void print_things(const std::string str)
{
std::cout << str << std::endl;
}
int main()
{
std::string str("Hello");
QueueFunction(print_things, str);
CallFuncs();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment