Skip to content

Instantly share code, notes, and snippets.

@danielhams
Last active June 27, 2019 19:55
Show Gist options
  • Save danielhams/ff82e6db7349d4a867016dba63d6779c to your computer and use it in GitHub Desktop.
Save danielhams/ff82e6db7349d4a867016dba63d6779c to your computer and use it in GitHub Desktop.
Toy C++ using some C++1X features
[didbsshell dan@leafy TestProj]$ g++ -pthread test.cc -o newtest.exe
[didbsshell dan@leafy TestProj]$ ./newtest.exe
This is a test.
Toast 1 constructed.
Toast 2 constructed.
Thread 3 with id 65537 began.
Thread 4 with id 65538 began.
Toast 3 constructed.
Toast 4 constructed.
Thread 4 with id 65538 ending.
Toast 4 destructed.
Thread 3 with id 65537 ending.
Toast 3 destructed.
Toast 2 destructed.
Toast 1 destructed.
Test complete.
Took 214.644 ms
isFinite of 256 is 1
isNan of nan0x7fffffff is 1
Pthread stack min is 16384
Found a key 2 and value banana
---- PROGRAM BEGINS ----
#include <iostream>
#include <ostream>
#include <chrono>
#include <memory>
#include <thread>
#include <mutex>
#include <math.h>
#include <limits.h>
#include <internal/limits_core.h>
#include <unordered_map>
using namespace std;
std::mutex log_mtx;
class Toast
{
int _tnum;
public:
Toast( int tnum) : _tnum(tnum)
{
log_mtx.lock();
std::cout << "Toast " << _tnum << " constructed." << std::endl;
log_mtx.unlock();
}
~Toast()
{
log_mtx.lock();
std::cout << "Toast " << _tnum << " destructed." << std::endl;
log_mtx.unlock();
}
};
void doit( int num )
{
std::thread::id thread_id = std::this_thread::get_id();
log_mtx.lock();
std::cout << "Thread " << num << " with id " << thread_id <<
" began." << std::endl;
log_mtx.unlock();
Toast t(num);
std::this_thread::sleep_for(200ms);
log_mtx.lock();
std::cout << "Thread " << num << " with id " << thread_id <<
" ending." << std::endl;
log_mtx.unlock();
}
int main(int argc, char** argv)
{
auto start = std::chrono::high_resolution_clock::now();
{
log_mtx.lock();
std::cout << "This is a test." << std::endl;
log_mtx.unlock();
std::unique_ptr<Toast> ut = std::make_unique<Toast>(1);
std::shared_ptr<Toast> st = std::make_shared<Toast>(2);
std::thread t1 = std::thread(doit,3);
std::thread t2 = std::thread(doit,4);
t1.join();
t2.join();
}
log_mtx.lock();
std::cout << "Test complete." << std::endl;
log_mtx.unlock();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end-start;
log_mtx.lock();
std::cout << "Took " << elapsed.count() << " ms" << std::endl;
log_mtx.unlock();
float f = 256.0f;
bool isFinite = std::isfinite(f);
log_mtx.lock();
std::cout << "isFinite of " << f << " is " << isFinite << std::endl;
log_mtx.unlock();
float isntf = NAN;
bool isNan = std::isnan(isntf);
log_mtx.lock();
std::cout << "isNan of " << isntf << " is " << isNan << std::endl;
log_mtx.unlock();
long pthreadStackMin = PTHREAD_STACK_MIN;
log_mtx.lock();
std::cout << "Pthread stack min is " << pthreadStackMin << std::endl;
log_mtx.unlock();
std::unordered_map<int,std::string> test_map;
typedef std::unordered_map<int,std::string> test_map_iter_t;
test_map.emplace(2,"banana");
for( auto mStart : test_map )
{
int iv = mStart.first;
std::string & value = mStart.second;
log_mtx.lock();
std::cout << "Found a key " << iv << " and value " << value << std::endl;
log_mtx.unlock();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment