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?

3
  • Threading is operating-system dependent. Without knowing your operating system, we can only suggest that you look at some platform-independent libraries, like boost and Qt. Commented Oct 30, 2011 at 20:21
  • Need a library, or C++11 Commented Oct 30, 2011 at 20:21
  • I'm sorry my mistake, Windows 7 Commented Oct 30, 2011 at 20:24

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

although, maybe using #pragma omp parallel to start several functions running in parallel would be a better example than a for loop.
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

Comments

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.

3 Comments

@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.
Is this what they call Futures? I haven't looked into this stuff yet.
@TomalakGeret'kal: yes, ftr will be of type std::future<T> 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<T> to the thread and retrieve a std::future<T> 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.
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.