Multithreading / Newbie-problems

Hello all,

the situation is the following: As an non-professional coder, i've a quiet huge application (Windows / OpenGL / wxWidgets) that is working, but it would be good to implement some multithreading.
One of my problems is, that i'm not common with all the compiler-settings so that whenever i change the setup i.e. to replac a library by a newer version, this leads to problems.

I'm working with:
Windows 10
Code::Blocks
Mingw32 (mingw-w32-bin-i686-20180726)
wxWidgets 3.1.2

Concerning mulithreading, i think, c++11 std::thread (that i would probably prefere) or OpenMP seem the best solutions, but with both i get linking-errors. For OpenMP the linker reports a missing file "libgomp.spec" and for std::thread it reoprts: 'thread' is not a member of 'std'.

I tried around a lot with different possible solutions that i found on google (like i.e. trying another compiler-suite), but nothing worked. Only the error-messages changed, like i.e. in one case some missing OpenGL-headers. So obviously i need some help to find the solution...

Thank you,
Frank



std::thread it reoprts: 'thread' is not a member of 'std'.
I don't have experience with OpenMP, but as far as std::thread goes... well it opens up a can of worms.

First, before trying anything else, just make sure you have -std=c++11 as a compiler flag. But if you already did that, and that doesn't magically fix things...

The issue is, some versions of MinGW do not have std::thread implemented.
https://stackoverflow.com/questions/37358856/does-mingw-w64-support-stdthread-out-of-the-box-when-using-the-win32-threading/
(I assume this applies to both mingw32 and mingw64)

The answer in that link (https://stackoverflow.com/a/37358960) suggests using mingw-std-threads headers: https://github.com/meganz/mingw-std-threads

Here's some other CPP.com links about std::thread, I assume it's the same issue.
http://www.cplusplus.com/forum/beginner/169434/ (See posts by JLBorges and Duthomhas)
http://www.cplusplus.com/forum/general/267237/#msg1149847
Last edited on
but it would be good to implement some multithreading.

Why? Multithreading isn't easy, though std::async makes it easier then ever.
Why do you want to change your app at all if it is working?
Last edited on
First conderning this:

Why? Multithreading isn't easy, though std::async makes it easier then ever.


I never heard of "std::async", but it sounds interesting.

As i said: I'm a non-professional coder. Most common i'm with really basic things that i lerned in early 1990th. I found std::thread via google and what i was reading seemed to fit.

Can you tell me more about the difference between std::async and std::thread?

Kind regard,
Frank



Last edited on
@Ganado

just make sure you have -std=c++11 as a compiler flag


I did. By the way: I have some macros like this:
 
#define EXAMPLE(COSTCHARARRAY)  CallFunction("text: " COSTCHARARRAY); 

...that complain, since i did this. Do you know why?

Concerning:
The issue is, some versions of MinGW do not have std::thread implemented.


This is one of the things that i probably don't understand due to missing basics, like i.e. everything concerning "CMakeLists.txt". As i'm also not a native english-speaker. I need hours to understand the documentations for your links, where often 90% is written for special cases like but not needed in my case. But i have to translate it anyway, to find out if it is relevant.

I'll try to understand it anyway. On a fist view, i saw this:
For example, #include "mingw.thread.h" replaces #include <thread>.
...which seems to be easy. I hope i find out what i need for it.


Thank you,
Frank
#include "mingw.thread.h"
Yes, try downloading that header file from the GitHub link, and seeing if it works. If it doesn't work, we'll have to investigate further.

By the way: I have some macros like this:
Can I see how you're "calling" the macro and what CallFunction is? Probably has something to do with stricter const correctness with c-strings, but I'm guessing.

e.g. if you are calling a function like:
myfunc("hello");
then doing void myfunc(char*); is deprecated.
you need to do: void myfunc(const char*);
Last edited on
then doing void myfunc(char*); is deprecated.


Ah, i did not know. Just set the C++11 some days ago after reading about std::thread.

I'm sometimes not really sure concerning the behaviour of "const", like i.e. in more complex statements "const char const*&". Therefore i avoid to use "const" wherever it works without.

----

#include "mingw.thread.h"
Yes, try downloading that header file from the GitHub link, and seeing if it works. If it doesn't work, we'll have to investigate further.


I'll try tomorrow and with header-only-includes i'll probably get it.

I'll give report or request tomorrow...



Thank you so far,
Frank
In terms of pointers to const, you can figure out its meaning by reading the line backwards:

char*
This is a "pointer to a char" if you read the * first, followed by the char.

const char* .
char const*
This is a "pointer to a char constant" or a "pointer to a constant char" (both are the same). This means that it is a pointer that points to a character whose value cannot be changed. However, the pointer itself is not constant and it can change the value it points to to a different constant char.

char * const
This is a "constant pointer to a char" if you read it backwards, which means the POINTER is constant, but the char is not. This means once you set what the pointer points to, you cannot change what it points to, but you can change the value of the CHAR it points to. This is basically equivalent to a reference except with pointer semantics.
const char* const&
This is a "reference to a constant pointer to a constant char". I think you can deduce what this means from the previous examples.
Last edited on
Can you tell me more about the difference between std::async and std::thread?

std::async is a higher level interface, pretty easy to use.
http://www.cplusplus.com/reference/future/async/

std::thread is low level where you need to know things like mutex, lock_guards, condition variables...

BTW: What's your native language?
@Thomas1965

The only difference between std::thread and std::async is that the latter stores the return value in the std::future variable. Otherwise both are starting threads and hence mutex etc. is required for synchronization in both cases.
Last edited on
@artganseforth

If you want to try OpenMP instead of the C++-standard threads then you can try the following. OpenMP does have the benefit of simplicity.

If you use the g++ compiler from the command line then you can compile and link with
g++ -fopenmp -O3 -o pi.exe pi.cpp

I don't know what you would use in an IDE.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <ctime>
#include "omp.h"                                           // OpenMP
using namespace std;

int main()
{
   const int N = 100000000;

   for ( int nThreads = 1; nThreads <= 8; nThreads++ )
   {
      clock_t start = clock();
      double sum = 0.0, sign = 1;

      omp_set_num_threads( nThreads );                     // OpenMP
      #pragma omp parallel for reduction( + : sum )        // OpenMP
      for ( int i = 0; i < N; i++ )
      {
         sum += sign * 4.0 / ( 1.0 + 2.0 * i );
         sign = -sign;
      }

      clock_t end = clock();

      cout << "Threads: " << nThreads << "     Sum: " << sum << "     Time: " << (double)( end - start ) / CLOCKS_PER_SEC << " s\n";
   }
}
coder777 wrote:
Otherwise both are starting threads and hence mutex etc. is required for synchronization in both cases.

AIUI, std::thread may or may not start a new thread. It's not guaranteed, unless you specify the std::launch::async policy.
Thank you all for your fantasic help!!


@Ganado:
I got it with: #include "mingw.thread.h" . No more linking-errors...

@Thomas1965
I'm german.
std::async is a higher level interface, pretty easy to use.

I red a bit about yesterday but i've to admit, that this high-level-cpp is not easy to understand by theory only. Therefore, trying around a bit with it, will be the best.



It took a while, to figure out that i will have to use std::ref(foo) in most cases, but now i think i have implemented a first working std::asyc-call. (Yeah!!)
Will be very interesting to find out, which parts of my script-interpreter can be executed asynchron without producing conflichts. Mybe i'll implent a "async"-statement into the script-language.

---


@TheToaster
In terms of pointers to const, you can figure out its meaning by reading the line backwards:

This is a good hint. Especially this will make it easier to undestand other-peoples code. In my own code, using const sometimes lead to type-problems - probably due to somehow 'dirty' programming. But if i would really clean up my code, this would take years....



So, thanks again to all of you!


Best,
Frank



Oh, a lot of new posts ;-)


coder777 wrote:
Otherwise both are starting threads and hence mutex etc. is required for synchronization in both cases.

AIUI, std::thread may or may not start a new thread. It's not guaranteed, unless you specify the std::launch::async policy.


Good hint. Thank you. I'll read about std::launch::async policy...


@lastchance
I tried OpenMP, but i also had problems with the libraries. I'm not sure, but the reason was probably, that i link static.
But as i now have a working std::async-call i'll try around with this for a while. Thank you anyway!!


Best,
Frank


Otherwise both are starting threads

@coder777,
std::async will throw only an exception if the launch_policy is std::async, otherwise it will execute the code in the same thread.
std::thread will throw an exception if it can't create a new thread.

@artganseforth, if you want to discuss sth. in German feel free to send me a PM.
Last edited on
@Thomas1965

Thank you! That's very nice. I'll keep it in my mind...

Best,
Frank
Topic archived. No new replies allowed.