General Category > General Discussion about just::thread

Just a few questions

(1/1)

CharlesB:
Hello,

I would like to know where I can find examples of the library? I found that the Thread Library Working Draft can be a good starting point, maybe it is worth mentionning it?

CharlesB

Anthony Williams:
There are some examples in my introduction to the C++0x thread library on DevX: http://www.devx.com/SpecialReports/Article/38883

Here's a simple example program that launches two threads:


--- Code: ---#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>

std::mutex iom;

void thread_func(int id)
{
    for(unsigned i=0;i<1000;++i)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
       
        std::lock_guard<std::mutex> lk(iom);
        std::cout<<"Thread "<<id<<" ";
    }
}


int main()
{
    std::thread t1(thread_func,1);
    std::thread t2(thread_func,2);
    t2.join();
    t1.join();
}

--- End code ---

Navigation

[0] Message Index

Go to full version