3

Possible Duplicate:
Simple example of threading in C++

Can someone please give me an example how to create a simple application in C++ that runs two functions simultaneously? I know that this question have connections with thread management and multi-threading, but I'm basically a php programmer and I'm not really familiar with advanced C++ programming.

Community
  • 1
  • 1
Farid Rn
  • 3,167
  • 5
  • 39
  • 66
  • Use `thread` class from boost and C++11! – Naszta Nov 18 '12 at 18:22
  • *Imp* : Boost is a wrapper over pthreads. So you might want to have a look at [pthreads](https://computing.llnl.gov/tutorials/pthreads/). – axiom Nov 18 '12 at 18:25
  • Is my question that much not-useful that I deserve "-1" for it? – Farid Rn Nov 18 '12 at 18:26
  • I didn't downvote, but a simple Google search (e.g. for C++ multiple threads example) would give you a lot of examples, including the duplicate question. – interjay Nov 18 '12 at 18:30
  • @interjay this can't be considered an equivalent question to one posted on November 2008, it's just ridiculous, you just cut out 4 years and the brand new C++11 ... you just can't close this one. – user1824407 Nov 18 '12 at 18:32
  • @interjay I searched and I did put my results in my question here and I said that I have no clue what are they talking about, but thanks to MaxMackie, he removed all of them and left only one paragraph of my question! – Farid Rn Nov 18 '12 at 18:33
  • 2
    @faridv this is a very good book http://www.manning.com/williams/ and there are many samples on the website, it's also really up-to-date to the latest C++ standard – user1824407 Nov 18 '12 at 18:36
  • @MaxMackie my question is not the one you mentioned. Why you do that to me?! – Farid Rn Nov 18 '12 at 18:37
  • 1
    @user1824407: New answers can be posted to old questions. For instance, that 2008 question has an answer with a C++11 example. – interjay Nov 18 '12 at 18:41
  • @faridv, I'm not the one who added that in. I tried retagging your question before seeing the Homework tag was obsolete. –  Nov 18 '12 at 18:44
  • So I wanted to help my friend and asked her question here and what I get here is a closed question and I got downvoted too :D – Farid Rn Nov 18 '12 at 18:47

2 Answers2

11

Here's a simple example:

#include <iostream>
#include <thread>

void f1() { std::cout << "This is function 1.\n"; }
void f2() { std::cout << "This is a different function, let's say 2.\n"; }

int main()
{
    std::thread t1(f1), t2(f2);   // run both functions at once

    // Final synchronisation:
    // All running threads must be either joined or detached
    t1.join();
    t2.join();
}

If your functions need to produce return values, you should combine the above thread objects with std::packaged_task runnable objects, available from <future>, which give you access to the return value of the thread function.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

I'm going to let you do the research yourself but a simple way to achieve this is with std::async:

http://en.cppreference.com/w/cpp/thread/async

Note that it is concurrently, but not necessarily simultaneously.

I believe Boost has this too - it's either in Boost.Thread or Boost.ASIO

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • 2
    `std::async` doesn't really promise any particular order of execution. Also, it's more suitable for *producing a value* asynchronously rather than "running a function simultaneously"... Maybe I could get behind an `std::packaged_task`, but for the purpose of exposition, a flat `std::thread` seems to suffice. – Kerrek SB Nov 18 '12 at 18:26