|
jobin007007 wrote: But do you know of anything in Visual Studio 2008 or any other widely used library that can do the above?
No extra library is required. Your code will work by using CreateThread() .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
If you want use CreateThread() Win32 API then must write additional static function to satisfy CreateThread()'s calling convention.
Like;
#include <windows.h>
class ABC {
HANDLE m_child;
public:
static DWORD __stdcall DO(void* p) {
((ABC*)p)->DoReally();
};
void DoReally() {};
};
void main()
{
ABC Object[20];
HANDLE childHandles[20];
DOWRD childIds[20];
for (int i = 0; i < 20; i++) {
childHandles[i] = CreateThread(NULL, 0, Object[i].DO, &Object[i], 0, &childIds[i]);
}
}
But CreateThread() do not mention C runtime, so I recommend _beginthreadex() runtime function to call.
In general, if you want to make thread with some windows api, you should make some entry point matching its api function interface.
|
|
|
|
|
oh, no need m_child member for class ABC.
|
|
|
|
|
jobin007007 wrote: unfortunately Visual C++ 2008 has some issues with Boost::threads package
Really? Like what, pray tell? I've used VS2008 with Boost.Threads and I'd like to know what issues I might encounter...
Here's how I've implemented the pattern you want in the past:
class Updater
{
public:
static Updater* StartUpdate()
{
Updater* u = new Updater();
HANDLE hThread = (HANDLE)_beginthread(Updater::ThreadProc, 0, (LPVOID)u);
return u;
}
static void ThreadProc(void* lpvUpdater)
{
Updater* updater = (Updater*)lpvUpdater;
updater->DoUpdate();
}
void DoUpdate()
{
}
};
The thread function is Updater::ThreadProc. This is passed an Updater instance pointer, which it uses to call the member function you actually want to call.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
Ah - so it's a C++/CLI issue - well, that ain't going to worry me - I use vanilla C++
But anyway - it only affects you (IIUC) when you statically link against Boost.Threads. If you put this line before any Boost #includes, then Boost will automatically link against the DLL.
#define BOOST_THREAD_DYN_DLL
I've just tested this with a little sample program (built with VC2008) which had this code compiled with /clr:
#include "stdafx.h"
using namespace System;
extern void flibble();
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
flibble();
return 0;
}
and this code compiled as native (against Boost 1.37.0)
#define BOOST_THREAD_DYN_DLL
#include <boost/thread.hpp>
void ThreadFunc()
{
for(int i=0;i<INT_MAX;++i)
;
}
void flibble()
{
boost::thread t(&ThreadFunc);
t.join();
}
It ran successfully, but if I removed or commented out the #define , it crashed on start-up. So - there was probably no need to dump Boost
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Oh great. I am gonna test this out tommorow.
But the CLI portion of my project is Dynamic Library type with MFC in a Shared DLL.
I am not sure what my C# Project type is.
When you say But anyway - it only affects you (IIUC) when you statically link against Boost.Threads. If you put this line before any Boost #includes, then Boost will automatically link against the DLL.
1) what do you actually mean? Would you be able to make the meaning clearer?
2) Why is Boost statically linking with my library? or is vice versa? what does that actually mean?
3) What is difference between static and dynamic linkage.
It would be really great if you could answer these questions.
|
|
|
|
|
jobin007007 wrote: 3) What is difference between static and dynamic linkage.
Static linking = link against a .lib containing object file - the object files are incorporated into your .exe or .dll
Dynamic linking = link against a .lib that references a DLL - your .exe or .dll will call the DLL when it calls the functions from the library.
jobin007007 wrote: 1) what do you actually mean? Would you be able to make the meaning clearer?
Boost (on Windows) has this really cool mechanism which automatically specifies which Boost libraries the linker should use when making your .exe or .dll. This takes into account various factors like a) what compiler you're using, b) (for Visual C++) what C run-time you're using, c) whether you're building a Debug or Release build. The one thing you have control over is whether a dynamic or static version of the Boost library is used. By using the line I gave you, you are telling Boost that you want to use the DLL version of Boost.Threads, not the other one. Boost will then tell the linker to use the DLL version of Boost.Threads.
jobin007007 wrote: 2) Why is Boost statically linking with my library? or is vice versa? what does that actually mean?
Your library is statically linking the Boost library. This happens because by default, Boost libraries automatically get statically linked.
HTH!!!
jobin007007 wrote: I am not sure what my C# Project type is.
That doesn't matter.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Would you able to tell me how to adapt my code to the code you wrote earlier?
Class ABC
{
int a;
int b;
void DO()
{
this.a="";
this.b="";
this.capture();
}
void capture()
{
}
};
void main()
{
ABC Object[20];
for(int i=0;i<20;i++)
createthread(Object[i].DO); //So 20 Threads are created here.
}
I am unsure as of how to use your example in my code.
|
|
|
|
|
There are so many ways...here's one
void MyThreadProc(void* objectAsVoidPtr)
{
ABC* object = (ABC*)objectAsVoidPtr;
object->DO();
}
void main()
{
ABC Object[20];
for(int i=0;i<20;i++)
_beginthread(MyThreadProc, 0, (LPVOID)Object+i);
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
#include iostream; //can't paste the << >> things here
#include string;
int main()
{
//works, from what i can see, because you cant compile "int someArray[] = {}" or "int arr[0];" because you cant allocate a 0-length array according to vs.net errors. So your guarenteed atleast 1 item in the array for "sizeof(_array[0])" to always find an item. this is the alternative to doing sizeof(int) or passing the type of the array to the macro.
#define ARRAY_LENGTH(_array) (sizeof(_array)/sizeof(_array[0]))
int someArray[] = {1, 2, 3, 4};
for(int i = 0; i < ARRAY_LENGTH(someArray); i++)
{
std::cout<<*(someArray + i)<
|
|
|
|
|
ARRAY_LENGTH macro is always ok for real array but not for pointer.
By the way, ARRAYSIZE() macro is predefined in windows.h of some newer sdk and it looks the same form as above.
|
|
|
|
|
wow norish i had no idea about that macro :P when i read that sizeof returned the number of bytes used by an array, i figured if i just divided by the size of the datatype used that i could get the length.
|
|
|
|
|
If you want a type-safe mechanism for getting the number of elements in an array, you might consider this:
#include <iostream>
template<class A, size_t N>
inline size_t ArraySize(const A ( & )[N]) { return N; }
template <typename T>
struct array_info
{
};
template <typename T, size_t N>
struct array_info<T[N]>
{
typedef T type;
enum { size = N };
};
int main()
{
typedef int (MyArray)[120];
int a[120];
float b[120];
std::cout << ArraySize(a) << std::endl;
std::cout << ArraySize(b) << std::endl;
std::cout << array_info<MyArray>::size << std::endl;
}
Here you have mechanisms for getting the number of elements in an array variable (function ArraySize ) or an array type (struct array_info ). And (unlike the macro version) these two won't compile if given a pointer variable/type - the power of pattern matching sees to that
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
hmm vectors? i suppose. Is it possible that arrays are more efficient?
Now this part of the code is really sexy:
<br />
template<class A, size_t N><br />
inline size_t ArraySize(const A ( & )[N]) <br />
{ <br />
return N; <br />
}<br />
<br />
int main()<br />
{<br />
int a[120];<br />
<br />
std::cout << ArraySize(a) << std::endl;<br />
}<br />
i'm not sure how it works but my guess is that somehow it's magically reading the size from the declaration of the array.
|
|
|
|
|
FocusedWolf wrote: possible that arrays are more efficient
Nope - in VS2008, vectors are guaranteed to use contiguous storage (i.e. same as arrays).
FocusedWolf wrote: i'm not sure how it works but my guess is that somehow it's magically reading the size from the declaration of the array.
That's the one. You pass an array and the compiler instantiates the function with the template parameters appropriate for that array.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
Not always necessarily better - not until C++ 0x when vectors get array style initialisers (IIU&RC).
Say I have an encryption key that (foolishly) I'm hardcoding in the software - which is simpler:
[edit]Or a better example - say I'm implementing a CRC32, which can be highly optimised by using a static array of byte values[/edit]
const BYTE key[] = { 12, 34, 4, 5, 36, 23, 765, 34 };
std::vector<BYTE> vKey;
vKey.push_back(12);
: :
vKey.push_back(34);
Yes, I know Boost has a library[^] that almost gives vectors initialiser syntax...but sometimes you just can't be bothered
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
FocusedWolf wrote: #include iostream; //can't paste the << >> things here
#include string;
Why not?
#include <iostream>
#include <string> Did you use the < button above the smileys?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hmm:
#include <iostream>
#include <string>
O i see... if this works then the lt,gt things are auto replacing my < >. Ya didn't realize that yesterday lol
|
|
|
|
|
see the _countof macro in the Run-Time Library Reference
|
|
|
|
|
Hi
How can I disable MDI to send a "File New" message at start up? So there will be no child window to be opened.
Best regards,
|
|
|
|
|
See here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
i am devloping an gui i want to use the costum controls(like styleish buttons) available in net but don't know how to use the code in my project ..have tried a lot but failed pls help me out..
|
|
|
|