Multithreading and Compilation errors
by dragonlady · in Torque Game Builder · 10/01/2010 (3:04 pm) · 3 replies
Good morning everyone :)
I am investigating ways to do some multithreading in Torque 2D. I am doing this because for one of my games, there's multiple AI players who are all making decisions simultaneously. I want to use a C++ toolkit to see how many cores/processors the player's computer has and then divide some of the operations into threads accordingly.
I have looked at the multithreading resource, and have also been using tutorials on how to extend the engine in C++. I do have some multithreading experience and am intermediate level at C++ programming. I figured this would be a cool challenge and hopefully if I'm successful I can do a writeup that might help others later. I am still getting a hold on the engine itself though.
So my basic setup is that I'm extending the Thread class to create a ThreadObject class. I want the ThreadObject class to be able to take in any function written in Torquescript, along with the number of arguments, and the arguments, and then create and run separate thread for that function. ThreadObject class declares a console method to allow the programmer to pass a TorqueScript function.
I get compilation errors in Visual Studio. I'm guessing that it's because the Thread class cannot use ConsoleMethods? Is there a workaround or can somebody please suggest where I can go from here?
Here's the error log:
c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(12) : error C2039: 'getStaticClassRep' : is not a member of 'Thread'
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platform\threads\thread.h(20) : see declaration of 'Thread'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(14) : error C2062: type 'void' unexpected
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(14) : error C2059: syntax error : 'constant'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(15) : error C2143: syntax error : missing ';' before '{'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(15) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(24) : error C2572: 'ThreadObject::run' : redefinition of default parameter : parameter 1
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platform\threads\threadobject.h(18) : see declaration of 'ThreadObject::run'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(27) : error C2036: 'void *' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(27) : error C2440: 'initializing' : cannot convert from 'void' to 'S32'
1> Expressions of type void cannot be converted to other types
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2057: expected constant expression
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2466: cannot allocate an array of constant size 0
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2133: 'args' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2062: type 'int' unexpected
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2065: 'i' : undeclared identifier
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2065: 'j' : undeclared identifier
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2143: syntax error : missing ';' before ')'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2143: syntax error : missing ';' before ')'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(32) : error C2143: syntax error : missing ';' before '{'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2143: syntax error : missing ';' before '['
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2337: 'j' : attribute not found
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2143: syntax error : missing ';' before '='
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(36) : error C2665: 'Con::execute' : none of the 2 overloads could convert all the argument types
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\console\console.h(509): could be 'const char *Con::execute(S32,const char *[])'
1> while trying to match the argument list '(S32, char *[])'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(39) : error C2440: 'static_cast' : cannot convert from 'SimObject *' to 'ThreadObject *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(46) : error C2440: 'initializing' : cannot convert from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2057: expected constant expression
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2466: cannot allocate an array of constant size 0
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2133: 'arg' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(48) : error C2440: '=' : cannot convert from 'const char *' to 'void *'
1> Conversion loses qualifiers
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(52) : error C2440: '=' : cannot convert from 'const char *' to 'void *'
1> Conversion loses qualifiers
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(54) : error C2661: 'ThreadObject::ThreadObject' : no overloaded function takes 3 arguments
1>winGL.cc
1>Generating Code...
1>Creating browse information file...
1>Microsoft Browse Information Maintenance Utility Version 8.00.50727
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>BSCMAKE: error BK1506 : cannot open file '..\..\Link\Debug.Win32\TorqueGameBuilder\bitwise.sbr': No such file or directory
I am investigating ways to do some multithreading in Torque 2D. I am doing this because for one of my games, there's multiple AI players who are all making decisions simultaneously. I want to use a C++ toolkit to see how many cores/processors the player's computer has and then divide some of the operations into threads accordingly.
I have looked at the multithreading resource, and have also been using tutorials on how to extend the engine in C++. I do have some multithreading experience and am intermediate level at C++ programming. I figured this would be a cool challenge and hopefully if I'm successful I can do a writeup that might help others later. I am still getting a hold on the engine itself though.
So my basic setup is that I'm extending the Thread class to create a ThreadObject class. I want the ThreadObject class to be able to take in any function written in Torquescript, along with the number of arguments, and the arguments, and then create and run separate thread for that function. ThreadObject class declares a console method to allow the programmer to pass a TorqueScript function.
I get compilation errors in Visual Studio. I'm guessing that it's because the Thread class cannot use ConsoleMethods? Is there a workaround or can somebody please suggest where I can go from here?
Here's the error log:
c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(12) : error C2039: 'getStaticClassRep' : is not a member of 'Thread'
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platform\threads\thread.h(20) : see declaration of 'Thread'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(14) : error C2062: type 'void' unexpected
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(14) : error C2059: syntax error : 'constant'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(15) : error C2143: syntax error : missing ';' before '{'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(15) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(24) : error C2572: 'ThreadObject::run' : redefinition of default parameter : parameter 1
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platform\threads\threadobject.h(18) : see declaration of 'ThreadObject::run'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(27) : error C2036: 'void *' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(27) : error C2440: 'initializing' : cannot convert from 'void' to 'S32'
1> Expressions of type void cannot be converted to other types
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2057: expected constant expression
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2466: cannot allocate an array of constant size 0
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(29) : error C2133: 'args' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2062: type 'int' unexpected
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2065: 'i' : undeclared identifier
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2065: 'j' : undeclared identifier
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2143: syntax error : missing ';' before ')'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(31) : error C2143: syntax error : missing ';' before ')'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(32) : error C2143: syntax error : missing ';' before '{'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2143: syntax error : missing ';' before '['
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2337: 'j' : attribute not found
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(33) : error C2143: syntax error : missing ';' before '='
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(36) : error C2665: 'Con::execute' : none of the 2 overloads could convert all the argument types
1> c:\program files\torque\torque game builder 1.7.5 pro\engine\source\console\console.h(509): could be 'const char *Con::execute(S32,const char *[])'
1> while trying to match the argument list '(S32, char *[])'
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(39) : error C2440: 'static_cast' : cannot convert from 'SimObject *' to 'ThreadObject *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(46) : error C2440: 'initializing' : cannot convert from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2057: expected constant expression
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2466: cannot allocate an array of constant size 0
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(47) : error C2133: 'arg' : unknown size
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(48) : error C2440: '=' : cannot convert from 'const char *' to 'void *'
1> Conversion loses qualifiers
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(52) : error C2440: '=' : cannot convert from 'const char *' to 'void *'
1> Conversion loses qualifiers
1>c:\program files\torque\torque game builder 1.7.5 pro\engine\source\platformwin32\threads\threadobject.cc(54) : error C2661: 'ThreadObject::ThreadObject' : no overloaded function takes 3 arguments
1>winGL.cc
1>Generating Code...
1>Creating browse information file...
1>Microsoft Browse Information Maintenance Utility Version 8.00.50727
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>BSCMAKE: error BK1506 : cannot open file '..\..\Link\Debug.Win32\TorqueGameBuilder\bitwise.sbr': No such file or directory
About the author
#2
Thanks all, and have a great day :)
10/01/2010 (3:08 pm)
Also, is there a tag or something for code? To make it look better in the post....sorry if it's hard to read.Thanks all, and have a great day :)
#3
I think I have a plan...if it works, I'll post back here with a solution.
10/03/2010 (1:10 pm)
I believe I see my error...other than the syntax errors, which are a little more obvious.I think I have a plan...if it works, I'll post back here with a solution.
Torque Owner dragonlady
Default Studio Name
ThreadObject.cc:
#include "platform/threads/ThreadObject.h"
#include "platform/threads/semaphore.h"
#include "platformWin32/platformWin32.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"
#include "util/safeDelete.h"
#include <process.h>
IMPLEMENT_CONOBJECT(ThreadObject);
ThreadObject::ThreadObject(0, void *arg, int numArgs) : Thread(0, arg, false)
{
this->numArgs = numArgs;
start();
}
void ThreadObject::run(void *arg = 0)
{
S32 numArgs = arg[1];
char* args[numArgs];
for(int i = 2, int j = 0; i < numArgs + 2; i++, j++)
{
char[j] = arg[i];
}
Con::execute(numArgs, args);
}
ConsoleMethod(ThreadObject, createThread, void, 4, 0, "create a thread for this function")
{
//three arguments are class object, name, and at least a function name and number of arguments
//access individual arguments using argv[n]
//arg is a pointer
int numArgs = argv[3];
void* arg[numArgs];
arg[0] = argv[2];
for(int i = 0; i < numArgs; i++)
{
arg[i + 1] = argv[i+4];
}
ThreadObject *t = new ThreadObject(0, arg, numArgs);
}
ThreadObject.h:
#include "platform/threads/thread.h"
#include "console/simBase.h"
#ifndef THREADOBJECT_H
#define THREADOBJECT_H
class ThreadObject : public Thread
{
public:
typedef Thread Parent;
ThreadObject(ThreadRunFunction func, void *arg); //: Thread(0);
virtual void run(void *arg = 0);
DECLARE_CONOBJECT(ThreadObject);
};
#endif /* THREADOBJECT_H */