Pumpkin
Pumpkin

Reputation: 2043

Running a function in a thread

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?

Upvotes: 3

Views: 2704

Answers (6)

smerlin
smerlin

Reputation: 6566

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.

Upvotes: 2

Borg8
Borg8

Reputation: 1662

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;
}

Upvotes: 3

Loki Astari
Loki Astari

Reputation: 264729

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

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

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.

Upvotes: 3

Tommy Andersen
Tommy Andersen

Reputation: 7230

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

Upvotes: 2

Dabbler
Dabbler

Reputation: 9873

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".

Upvotes: 3

Related Questions