Skip to content

Instantly share code, notes, and snippets.

@autosquid
Forked from kumagi/tls_samp.cpp
Last active August 29, 2015 14:07
Show Gist options
  • Save autosquid/1d78de4498aa940def37 to your computer and use it in GitHub Desktop.
Save autosquid/1d78de4498aa940def37 to your computer and use it in GitHub Desktop.
#include <boost/thread.hpp>
#include <boost/thread/tss.hpp>
#include <boost/bind.hpp>
#include <iostream>
class del{
public:
int dat;
del(int i):dat(i){}
~del(){std::cout << "deleted " << dat<< std::endl;}
};
boost::thread_specific_ptr<del> tls;
void thread(int num){
tls.reset(new del(num));
}
int main(void){
tls.reset(new del(0));
{
boost::thread a1(boost::bind(&thread, 1));
boost::thread a2(boost::bind(&thread, 2));
boost::thread a3(boost::bind(&thread, 3));
a1.join();a2.join();a3.join();
}
std::cout << "end" << std::endl;
}
/*
---outputs--
deleted 3
deleted 1
deleted 2
end
deleted 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment