Threading problem.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 739 Views - Last Post: 19 May 2014 - 03:24 PM Rate Topic: -----

#1 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Threading problem.

Posted 13 May 2014 - 06:08 AM

Hello. I've written some code that I am currently threading but I am unsure how to pass by reference, or rather why my pass by reference is failing.

I am passing an array of floats by reference, this works fine when not threaded but I am given the error that float*&field does not match std::reference_wrapper<float*>.

Anyways. Here's the code.

The method:
diffuse(int b, float*& field, float*& pField, float diffFactor, float dt, int iteration)


The thread:
std::thread diffuseThread(diffuse, 1, std::ref(u), std::ref(prevU), visc, dt, iteration);


Is This A Good Question/Topic? 0
  • +

Replies To: Threading problem.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Threading problem.

Posted 13 May 2014 - 12:24 PM

You're going to need to show more content. For example how are all those variables defined? But really a small complete program that illustrates the problem would probably be best.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Re: Threading problem.

Posted 13 May 2014 - 01:18 PM

Is that really necessary? The problem is a compile error for creating a thread for this specific method, I'm not sure how the rest of the code would help.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Threading problem.

Posted 13 May 2014 - 01:58 PM

As long as you can deal with answers based on guesswork then I guess no, otherwise yes you need to provide more content.

Jim

This post has been edited by jimblumberg: 13 May 2014 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Threading problem.

Posted 13 May 2014 - 08:47 PM

If you don't want to post all your code, then post a minimal example that demonstrates your problem.
Was This Post Helpful? 0
  • +
  • -

#6 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Re: Threading problem.

Posted 14 May 2014 - 03:17 AM

As the problem is with creating the thread I thought this would be enough, but no problem.

Here is the code.

#include <Windows.h>
#include <iostream>
#include <thread>

void diffuse(int b, float*& field, float*& pField, float diffFactor, float dt, int iteration)
{
	std::cout << "Things happen" << std::endl;
}

int main()
{
	float* arrayOne;
	float* arrayTwo;

	int size = 32 * 32;

	arrayOne = (float*)calloc(size, sizeof(float));
	arrayTwo = (float*)calloc(size, sizeof(float));

	std::thread diffuseThread(diffuse, 1, std::ref(arrayOne), std::ref(arrayTwo), 0.001f, 0.001f, 2);

	system("PAUSE");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Threading problem.

Posted 14 May 2014 - 06:25 AM

Are you using VS2010, VS2012 or VS2013? For me, it builds using VS2013.

I tried compiling with gcc on Windows, but I stumbled on the "'thread' is not a member of 'std'" error even though I'm using the 64-bit version of MinGW. Apparently I need to rebuild my MinGW.
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1629
  • View blog
  • Posts: 3,092
  • Joined: 30-May 10

Re: Threading problem.

Posted 14 May 2014 - 09:49 AM

You need a C++11 compiler.
$ g++ bar.cpp
In file included from /usr/include/c++/4.6/thread:35:0,
                 from bar.cpp:2:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
bar.cpp: In function ‘int main()’:
bar.cpp:20:3: error: ‘thread’ is not a member of ‘std’
bar.cpp:20:15: error: expected ‘;’ before ‘diffuseThread’



But when compiled with C++11 flags, it compiles OK.
$ g++ -std=c++0x bar.cpp
$ ./a.out 
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted


I guess it is still experimental then ;)
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1314
  • View blog
  • Posts: 4,513
  • Joined: 19-February 09

Re: Threading problem.

Posted 14 May 2014 - 10:21 AM

Hi, are you just changing the data in the arrays or are changing the pointers.

So do you need the C++ references?

#include <windows.h>
#include <iostream>
#include <thread>

void diffuse(int b, float* field, float* pField, float diffFactor, float dt, int iteration)
{
	std::cout << "Things happen" << std::endl;
}


Was This Post Helpful? 0
  • +
  • -

#10 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Re: Threading problem.

Posted 14 May 2014 - 10:38 AM

I am using VS2012. I'll have to test it tomorrow as I do not have the source with me but there was definitely an error. Maybe it is not using the latest compiler.

And I am changing the data in the arrays, yes.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Threading problem.

Posted 14 May 2014 - 08:30 PM

With VS2012, this is the error I'm getting:
error C2661: 'std::thread::thread' : no overloaded function takes 7 arguments



The error that you posted in post #1 is an Intellisense error. I usually ignore those until after I get a successful build. For reference, here is the complete Intellisense error:
IntelliSense: no instance of constructor "std::thread::thread" matches the argument list
            argument types are: (void (int b, float *&field, float *&pField, float diffFactor, float dt, int iteration), int, std::reference_wrapper<float *>, std::reference_wrapper<float *>, float, float, int)



Personally, I would completely forget about using VS2012 because it is not C++11 compliant, and I would use VS2013 with a huge grain of salt because the C++11 compliance for that version is very spotty. I would use GCC, but as I noted above, I was running into issues compiling code with threads...
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Threading problem.

Posted 14 May 2014 - 08:42 PM

View PostSalem_c, on 14 May 2014 - 12:49 PM, said:

You need a C++11 compiler.
Spoiler


I was using C++11. Here's my build output to show that:
- I'm using the "-std=c++11" flag to force C++11 compilation;
- I'm using the "-v" turned on to show that:
- I'm using version 4.8.2 which supposedly is fully C++11 compliant.
- I'm using the 64-bit version of MinGW GCC (because apparently the 32-bit version doesn't support threads)

-------------- Build: Debug in TestThreads (compiler: GNU GCC Compiler (MinGW))---------------

g++.exe -Wall -fexceptions -std=c++11 -Wall -g -v  -c C:\z\Test\TestThreads\main.cpp -o obj\Debug\main.o
Using built-in specs.
COLLECT_GCC=g++.exe
Target: x86_64-w64-mingw32
Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-lto --disable-nls --disable-shared --disable-win32-registry --enable-checking=release --with-tune=core-avx-i
Thread model: win32
gcc version 4.8.2 (GCC) 
COLLECT_GCC_OPTIONS='-fexceptions' '-std=c++11' '-Wall' '-g' '-v' '-c' '-o' 'obj\Debug\main.o' '-mtune=core-avx-i' '-march=x86-64'
 c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.8.2/cc1plus.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/ -U_REENTRANT C:\z\Test\TestThreads\main.cpp -quiet -dumpbase main.cpp -mtune=core-avx-i -march=x86-64 -auxbase-strip obj\Debug\main.o -g -Wall -std=c++11 -version -fexceptions -o C:\Users\Ants\AppData\Local\Temp\cc0iheRN.s
GNU C++ (GCC) version 4.8.2 (x86_64-w64-mingw32)
	compiled by GNU C version 4.8.2, GMP version 5.1.3, MPFR version 3.1.2-p5, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2/x86_64-w64-mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/include"
ignoring nonexistent directory "c:/temp/gcc/destc:/temp/gcc/dest/lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "c:/temp/gcc/dest/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2/x86_64-w64-mingw32
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../include/c++/4.8.2/backward
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/include
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/include-fixed
 c:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../x86_64-w64-mingw32/include
End of search list.
GNU C++ (GCC) version 4.8.2 (x86_64-w64-mingw32)
	compiled by GNU C version 4.8.2, GMP version 5.1.3, MPFR version 3.1.2-p5, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 583a00493e9de87dd9a1bcbf075d3e74
C:\z\Test\TestThreads\main.cpp: In function 'int main()':
C:\z\Test\TestThreads\main.cpp:20:2: error: 'thread' is not a member of 'std'
  std::thread diffuseThread(diffuse, 1, std::ref(arrayOne), std::ref(arrayTwo), 0.001f, 0.001f, 2);
  ^
C:\z\Test\TestThreads\main.cpp:20:14: error: expected ';' before 'diffuseThread'
  std::thread diffuseThread(diffuse, 1, std::ref(arrayOne), std::ref(arrayTwo), 0.001f, 0.001f, 2);
              ^
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Threading problem.

Posted 14 May 2014 - 08:54 PM

And there's the smoking gun on line 8: Thread model: win32. I guess I'll have to abandon using Nuwen's MinGW distro and go for TDM-GCC or the official MinGW-W64 builds.
Was This Post Helpful? 0
  • +
  • -

#14 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Re: Threading problem.

Posted 19 May 2014 - 03:53 AM

So I tested my code again today, checked the compiler versions (it was set to VS2012 v110).

The compile error I am given is
error C2661: 'std::thread::thread' : no overloaded function takes 7 arguments


I have had this error before but I don't recall how I fixed it, I am not sure if my syntax is incorrect?

I will get VS2013 tonight, or set up an alternate compiling environment to compile it.
Was This Post Helpful? 0
  • +
  • -

#15 Haegr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-May 14

Re: Threading problem.

Posted 19 May 2014 - 04:32 AM

I just realised...I can't actually change the compiler version because I do not think one of the libraries I am using is updated for VS2013
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2