Author Topic: Just a few questions  (Read 50755 times)

CharlesB

  • Beta Testers
  • Newbie
  • *
  • Posts: 1
    • View Profile
Just a few questions
« 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 can be a good starting point, maybe it is worth mentionning it?

CharlesB

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: Just a few questions
« Reply #1 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();
}