3

I would like to perform the operation stated above.

void myFunc()
{
   ... // several stuff
}

...

int main()
{
   ...
   // I would like to run myFunc in a thread so that the code below could execute
   // while it is being completed.
   ...
}

What do you suggest I should do? Which function call from which library would enable me to complete my goal?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Pumpkin
  • 1,993
  • 3
  • 26
  • 32

6 Answers6

4

For Win32 programming, you can use beginthread. There is also CreateThread, but if I remember correctly, that doesn't initialize the C/C++ environment, which leads to problems.

Edit: Just checked - MSDN states "A thread in an executable that calls the C run-time library (CRT) should use the _beginthread and _endthread functions for thread management rather than CreateThread and ExitThread".

Dabbler
  • 9,733
  • 5
  • 41
  • 64
3

Boost.Thread.

void myFunc() {
   // ... stuff ...
}

int main() {
   boost::thread<void()> t(&myFunc);

   // ... stuff ...

   t.join();
}

Or the standard library equivalents if you're using C++11.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

Also you may check out Open Multi-Processing (OMP) protocol. It is the easiest way to write multi threding programs (but for multi CPU systems only).

For example, parallel for, when all accessible CPUs will work together may be implemented in that way:

#pragma omp parallel for
for(int i = 0; i < ARRAY_LENGH; ++i)
{
    array[i] = i;
}
Borg8
  • 1,562
  • 11
  • 19
  • although, maybe using `#pragma omp parallel` to start several functions running in parallel would be a better example than a for loop. – Ben Voigt Oct 31 '11 at 00:42
2

For Windows _beginthread or _beginthreadex MSDN Documentation of the two.

Also take a look at this library about threading: The Code Project article on Multi-threading

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
2

requires c++11 (formerly known as c++0x) support:

#include <future>

int main(int argc, char* argv[])
{
    auto ftr = std::async( std::launch::async, [](){
        //your code which you want to run in a thread goes here.
    });
}

the launch policy can be std::launch::async or std::launch::deferred. `std::launch::async causes the thread to start immediatly, std::launch::deferred will start the thread when the result is needed, which means when ftr.get() is called, or when ftr goes out of scope.

smerlin
  • 6,446
  • 3
  • 35
  • 58
  • @Tomalak: Of course you are right. I fixed that and changed the code to use `std::async` instead of `std::thread`, because thats the easiest way to run a function (which might return a value) in a thread. – smerlin Nov 01 '11 at 10:25
  • Is this what they call Futures? I haven't looked into this stuff yet. – Lightness Races in Orbit Nov 01 '11 at 21:37
  • @TomalakGeret'kal: yes, `ftr` will be of type `std::future` where T is the return type of the function passed to `std::async`. If you use raw threads instead of `std::async`, you will have to pass a `std::promise` to the thread and retrieve a `std::future` object from it. The main and pretty useful feature is, that the `std::future` objects will rethrow exceptions which remained unhandled in the thread, if another thread tries to retrieve the result from it. Bartosz Milewski made (and still makes) some nice video tutorials about threading in C++11, just google his blog. – smerlin Nov 02 '11 at 01:52
1

Using boost/thread:

#include <boost/thread.hpp>
void myFunc()
{
    // several stuff
}

int main()
{
    boost::thread  thread(myFunc);

    // thread created


    //...
    // I would like to run myFunc in a thread so that the code below could execute
    // while it is being completed.
    //...

    // must wait for thread to finish before exiting
    thread.join();
}

> g++ -lboost_thread test.cpp

You will need to make sure the boost thread library has been built.

Using pthreads:

void* myFunc(void* arg)
{
    // several stuff

    return NULL;
}


int main()
{
    pthread_t   thread;
    int result = pthread_create(&thread, NULL, myFunc, NULL);
    if (result == 0)
    {
        // thread created
        //...
        // I would like to run myFunc in a thread so that the code below could execute
        // while it is being completed.
        //...

        // must wait for thread to finish before exiting
        void*   result;
        pthread_join(thread, &result);
    }
}

> g++ -lpthread test.cpp
Martin York
  • 257,169
  • 86
  • 333
  • 562