just::thread Support Forum

General Category => General Discussion about just::thread => Topic started by: CharlesB on October 16, 2008, 10:25:35 AM

Title: Just a few questions
Post by: CharlesB on October 16, 2008, 10:25:35 AM
Hello,

I would like to know where I can find examples of the library? I found that the Thread Library Working Draft (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html) can be a good starting point, maybe it is worth mentionning it?

CharlesB
Title: Re: Just a few questions
Post by: Anthony Williams on October 16, 2008, 01:42:05 PM
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: [Select]
#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();
}