Cenron's Profile
Reputation: 6
Worker
- Group:
- New Members
- Active Posts:
- 10 (0.03 per day)
- Joined:
- 23-May 12
- Profile Views:
- 758
- Last Active:
May 17 2013 11:36 AM- Currently:
- Offline
Previous Fields
- Country:
- Who Cares
- OS Preference:
- Windows
- Favorite Browser:
- Chrome
- Favorite Processor:
- AMD
- Favorite Gaming Platform:
- XBox
- Your Car:
- Honda
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: How to trigger Main function through a Thread?
Posted 29 Jul 2012
Skydiver, on 06 June 2012 - 01:56 AM, said:The typical approach is to use a mutex, event, or semaphore. The thread that is to be "triggered" will wait on the WaitHandle object, and the thread that is doing the work that says it is done will signal the WaitHandle.
In your case, though, since all you are doing is waiting for the other thread to be done, you could simply use Join(). See the sample code: http://msdn.microsof...y/95hbf2ta#Y500
Not to knock your approach man but why use a thread to maintain a timer? Seems like a waste of resources. You can make a simple timer using 2 variables.
// NOT CLI int main() { const u_long interval = 10; // 10 seconds for example. u_long startTimer = GetTickCount(); // This is in mili secs. // The thread that checks for the timer. while( true ) { // DO whatever you want here and when 10 seconds goes by the loop exists. if( (startTimer + (interval * 1000)) >= GetTickCount() ) break; } } -
In Topic: set variables from inside a thread
Posted 29 Jul 2012
JohnBoyMan, on 29 March 2012 - 02:30 AM, said:Hello all. I am working with threads at the moment in my program. The thread is simple and looks like this.
void ThreadProc(void){ }; Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) ); oThread->Start();
I have another simplar thread like this but its more complex. I try to set variables in it like this, for example.
int y; void ThreadProc(void){ y=9; }; Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) ); oThread->Start(); cout<<y;
y will be 0!
This is veryyy simple stuff, but if i cout<<y; from outside of the ThreadProc it will be 0 always. I have tried a few other things, i dont ever remeber having this problem, i have been coding for 5 years,so am i nuts? Or should i use a delegate or something? thanks
Use the static keyword to declare the int as a global variable,
static int y = 0;
The other way you can do it is declare it as a pointer.
so it can survive the scope of the function, I like this method better because then your not polluting the global namespace.
unsigned int __stdcall ThreadProc( void *param ) { int *y = reinterpret_cast<int *>( param ); if( y != NULL ) (*y) = 9; } int main() { int y = 0; // Or int *y = new int; // Pass the address of the variable as a reference. CreateThread( &ThreadProc, reinterpret_cast<void *>(&y) ); std::cout << y << std::endl; // Should be 9. } -
In Topic: Questions about DLLs in C/C++
Posted 1 Jul 2012
machoolah, on 20 May 2012 - 03:18 PM, said:I have a few questions about DLLs I hope somebody could help me with:
1. What are DLLs good for?
2. Can you make DLLs using classic C++ (as opposed to C++/CLI)?
3. Can you make DLLs using C?
4. Is there any tutorial out there suitable for someone who has made command-line applications to learn creating DLLs?
DLL's can be used to extend the functionality of a program by allowing an external library of functions that can be called.
You can create an interface into the DLL and as long as you maintain that interface, you can update the functionality of DLL without recompiling the main code.
So lets say for example your writing a game, you create a DLL that handles all the drawing of stuff to the screen ( for example take the following functions. )
DrawEngine.DLL
InitDrawEngine() DrawSprite() MoveSprite() DestroyDrawEngine()
So then in your main program you import the DLL then create access to the functions
MyGame.exe
// This is pseudo code import DrawEngine.DLL // Dynamically Link the functions FUNC_TEMPLATE InitDrawEngine = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(InitDrawEngine)); FUNC_TEMPLATE DrawSprite = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(DrawSprite)); FUNC_TEMPLATE MoveSprit) = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(MoveSprite)); FUNC_TEMPLATE DestroyDrawEngine = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(DestroyDrawEngine)); // Now call our functions since we linked them. mainDisplay = InitDrawEngine(); DrawSprite( mainDisplay, drawCopper ); MoveSprite( mainDisplay, drawCopper, 20px, 100px ); DestroyDrawEngine( mainDisplay );
Now you can go back into your DLL project and modify the functionality of these function as much as you want, but as long as you dont change the function prototype then you wont have to modify and recompile your main program.
The other feature is you can create a DLL as if it was an EXE and when that DLL is loaded it will execute the code from start to finish. This option is usually used for code injection.
Here is a little guide with more detail.
http://www.flipcode....sing_DLLs.shtml
DLL's are very usful for big project like a game, because you can break up your code and only update the parts you want instead of having to recompile your entire project. -
In Topic: Developing a custom network protocol
Posted 10 Jun 2012
tlhIn`toq, on 09 June 2012 - 06:27 AM, said:I do something similar but with two more fields
- packetNumber
- totalPackets
saying this would be packet 14 of 250 for example.
Some of my data are quite large and have to be broken down and reassembled.
This way if one packet is lost in transmission the receiver then requests the missing packet specifically.
Thats def a good idea and so simple. Great for large amounts of data or UDP packets. I am gonna have to remember that next time. - packetNumber
-
In Topic: Developing a custom network protocol
Posted 9 Jun 2012
I can't say this is the best way to do this or even its efficient but it works for me.
If you use TCP, you can create structures fill them with data and then send the structure. For example
struct PACKET { byte sig; // The id that this is a packet from your protocol u_long packetType; // If this is a request or response packet (optional). u_long packetID; // A uniqe 32bit ID that identifies this packet. __int64 timeStamp; // A 64 bit representation of when the packet was sent. byte data[ 1024 ]; // The data you want to send. }; // Create a packet. PACKET pak; pak.sig = MY_PACKET_SIG; pak.packetType = PT_REQUEST; etc.. // Send the packet to the person. send( sock, pak, sizeof(pak) );
This will send the full data as one long value to the server the server recv the packet back into a PACKET type. This method works very well for me so I use it all the time.
Another way you can do it is plain text packets, look at the FTP or HTTP protocol. You can also construct the plaintext packet such as
std::string myPak = "PAK_SIG;PAK_TYPE;PAK_ID;PAK_TS;RAW_DATA"; send( myPak );
For UDP you use this same method but will also have to include packet verification, so that you know that you got the whole thing.
If anyone else has a better way of doing this, please share because I would love to know.
My Information
- Member Title:
- New D.I.C Head
- Age:
- 27 years old
- Birthday:
- October 20, 1985
- Gender:
-
- Location:
- 127.0.0.1
- Years Programming:
- 10
- Programming Languages:
- C/C++, PHP, Python, Java, ASM, MySQL, HTML, CSS, Java Script
Contact Information
- E-mail:
- Private
Friends
Cenron hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
Cenron has no profile comments yet. Why not say hello?