I want to write a program (any one performing any arithmetic operation), but I need it to be executed in several threads. Please tell me what are the standard libraries in C ++ (is there any). And a small example of their use.

  • one
    Qt? - Sioshka
  • If there is an opportunity on Qt, then this option is also suitable, but first I wanted to accomplish the task in C ++ - Egor Sokolov

4 answers 4

In boost there is a library for working with threads. The big plus is that it is cross-platform. The documentation is here . An example of a simple program that starts one thread:

#include <iostream> #include <boost/thread.hpp> #include <boost/date_time.hpp> void workerFunc() { boost::posix_time::seconds workTime(3); std::cout << "Worker: running" << std::endl; // Pretend to do something useful... boost::this_thread::sleep(workTime); std::cout << "Worker: finished" << std::endl; } int main(int argc, char* argv[]) { std::cout << "main: startup" << std::endl; boost::thread workerThread(workerFunc); std::cout << "main: waiting for thread" << std::endl; workerThread.join(); std::cout << "main: done" << std::endl; return 0; } 

An example using a group of threads and capturing mutex:

 #include <iostream> #include <boost/thread.hpp> #include <boost/bind.hpp> void print(int* arr, int from, int to) { static boost::mutex ioMutex; for (int i = from; i < to; ++i) { boost::mutex::scoped_lock l(ioMutex); std::cout << arr[i] << std::endl; } } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; boost::thread_group g; g.create_thread(boost::bind(print, arr, 0, 4)); g.create_thread(boost::bind(print, arr, 4, 8)); g.join_all(); return 0; } 
  • A good example, thank you, I will try to figure it out - Egor Sokolov
  • Another question: boost is not included in the standard Linux libraries? Is it necessary to add it manually or is it called something different? - Egor Sokolov
  • Well, in ubuntu, for example, it is enough to install libbost-thread-dev (sudo apt-get install libbost-thread-dev), and the rest of the packages will catch up with dependencies. In other distros probably something like that. - dzhioev

openmp here on Habré is an example:

http://habrahabr.ru/blogs/cpp/71296/

UPD:

 #include <omp.h> #include <stdio.h> int main(int argc, char **argv){ int i; #pragma omp parallel for for (i = 0; i < 10; i++) printf("t#: %ii: %i\n", omp_get_thread_num(), i); } 

at the exit:

 t#: 2 i: 6 t#: 2 i: 7 t#: 2 i: 8 t#: 3 i: 9 t#: 0 i: 0 t#: 0 i: 1 t#: 0 i: 2 t#: 1 i: 3 t#: 1 i: 4 t#: 1 i: 5 
  • The compiler can not understand omp_get_thread_num (), if you do #pragma omp parallel omp_get_thread_num (3) the message is displayed 1 time. - Yegor Sokolov
  • what message? omp_get_thread_num (), as far as I know, displays the number of threads. - omp_get_thread_num (3) what's this? - Vitaliy
  • 2
    Wrong, not omp_get_thread_num (3), but omp_set_num_threads (3). I realized my mistake, I am not okay with the libraries, so the compiler does not see the functions - Egor Sokolov

Using boost :: thread:

 #include <boost/thread.hpp> using namespace boost; void calc0() { } void calc1() { } int main () { thread t0 = thread(calc0); thread t1 = thread(calc1); t0.join(); t1.join(); return 0; } 

Calculations are carried out in the body of the functions calc0 and calc1.

    Usually do so

     /*переменные*/ #ifdef _WIN32 HANDLE thr_handle; #else pthread_t thr_handle; pthread_attr_t attr; #endif /*переменные конец*/ 

    Start stream

     /*запуск*/ if (thr_handle != NULL) { #ifndef _WIN32 pthread_join(thr_handle,NULL); pthread_attr_destroy(&attr); #endif } #ifdef _WIN32 thr_handle = AfxBeginThread(Run, (LPVOID)this, pr, 15000000); if (thr_handle == NULL) {сообщаю о жуткой ошибке, типа все плохо} else { return 0; } #else pthread_attr_setstacksize(&attr, 15000000); if (pthread_create(&thr_handle, &attr, Run, (LPVOID)this) != 0) {сообщаю о жуткой ошибке, типа все плохо} else { return 0; } #endif /*запуск конец*/ 

    As a result, a thread of the type UINT Run (LPVOID pParam) is executed once

    If I remember correctly, then for building: under MFC Windows, under linux -lpthread

    The rest is a manual to help :)