Skip to content

Instantly share code, notes, and snippets.

@exallium
Created February 19, 2012 16:37
Show Gist options
  • Save exallium/1864566 to your computer and use it in GitHub Desktop.
Save exallium/1864566 to your computer and use it in GitHub Desktop.
Boost Multithread Example
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
using boost::thread;
using boost::mutex;
mutex a;
const int THREAD_COUNT = 10;
void worker(int i) {
a.lock();
cout << "Worker thread " << i << endl;
a.unlock();
}
int main(int argc, char** argv)
{
thread *threads[THREAD_COUNT];
// Creation
for(int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new thread(worker, i);
}
// Cleanup
for(int i = 0; i < THREAD_COUNT; i++) {
threads[i]->join();
delete threads[i];
}
return 0;
}
@vbpc
Copy link

vbpc commented Apr 9, 2019

very very nice!

@marikhu
Copy link

marikhu commented Apr 26, 2021

@sameerthapaevontech Because threads are created with new, you should release with delete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment