Created
February 19, 2012 16:37
-
-
Save exallium/1864566 to your computer and use it in GitHub Desktop.
Boost Multithread Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sameerthapaevontech Because threads are created with new, you should release with delete.