It is currently Wed Aug 23, 2017 6:55 am

All times are UTC - 5 hours




 Page 1 of 1 [ 10 posts ] 
Author Message
 Post subject: C++ VMK 15 - Threads
PostPosted: Mon Apr 20, 2009 7:23 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
There are a lot of things covered in this VMK, all with relation to making multi-threaded applications.

I start by showing how to create a second thread. I then pass data between the two threads. I show how to use a Mutex to monitor when a thread has completed and also how to use critical sections to protect data shared between threads. Finally I end by showing how to use threads inside of a class.


Offline
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 8:52 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I have mistakenly placed the "volatile" keyword in the wrong place in the VMK.

The volatile word must be used at the location where you declare your variables, not where you define the structure. The proper place would be:

struct myVariables {
   bool  bDone;
   int   iValue;
   float fValue;
}; //myVariables


int _tmain(int argc, _TCHAR* argv[]) {

   volatile myVariables var;


Thanks for catching this typo bughunter!


Offline
 Profile  
 
 Post subject:
PostPosted: Sat May 02, 2009 12:22 pm 

Joined: Fri Nov 23, 2007 1:00 am
Posts: 52
Location: Your PC
I have a few questions for your tutorial.

What is the difference of cons and pros betwen CreateThread and beginThread func?

Also you didn't say anything about _endThread function. Would it be required to safely end a thread in a certain situation? I doubt they would just add it for no reason.

And how many threads is too many and also is multithreading performance boost that much or not required in some cases just because of bad algorithm? I.E Particles and using for loops for 10 emitters?

I would like to see a the game engine using threading for rendering particles as a example and show how to work with threads in while loops and controlling the particles.

So there position isn't lagged or jumpy just by using a if statement in the while loop. If game Delta time is more then the last Delta time received update the positions in the particles.

I'm still itching my head if it's algorithm or just bad sorting of C++ code, because of the lag I'm still getting.

Example of slow "execution" C++ put together.

Slow C++ struct:
struct Client {
char name[7];
long Keys;
bool Happy;
};


Faster C++ Struct
struct Client {
long Keys
char name[7];
bool Happy;
}


Offline
 Profile  
 
 Post subject:
PostPosted: Wed May 06, 2009 8:03 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
ajm113 wrote:
What is the difference of cons and pros betwen CreateThread and beginThread func?


Have you tried this: http://www.lmgtfy.com/?q=What+is+the+di ... inThreadex

ajm113 wrote:
Also you didn't say anything about _endThread function. Would it be required to safely end a thread in a certain situation?


If you return out of your main thead function, then the thread will be closed automatically. You don't need to call _endThread. However if you want to force the thread closed then that is when you'd use it.

ajm113 wrote:
...how many threads is too many and also is multithreading performance boost that much or not required in some cases just because of bad algorithm? I.E Particles and using for loops for 10 emitters?


I generally don't use very many threads in my applications unless I'm making a UI. You don't really need many theads now that you can offload work onto the graphics card when making games. I'll use a second thread when I want to stream audio during game play and a third one to handle Physics but usually nothing beyond that. If you want to put your particle engine into a separate thread that's fine, but put the whole engine into one thread rather than spawning individual threads for each particle.

I generally avoid using threads unless it is absolutely necessary because they are such a pain to debug and to synchronize properly. If you put your particle engine into the physics loop then everything should run fine unless you're trying to allocate and deallocate memory in runtime for your particles. This will kill your performance pretty quickly.


Offline
 Profile  
 
 Post subject: Re:
PostPosted: Tue Jun 14, 2011 2:38 pm 

Joined: Tue Jun 14, 2011 11:51 am
Posts: 4
Marek wrote:
...


Hello, I've watched this tutorial some time now, I've copied the whole code.
just some variable name and such that are different.
I'm using Code::blocks.

The errors I'm receiving are :

C:\Program Files (x86)\CodeBlocks\projekt\class\main.cpp||In function 'int main()':|
C:\Program Files (x86)\CodeBlocks\projekt\class\main.cpp|28|error: invalid conversion from 'volatile void*' to 'void*'|
C:\Program Files (x86)\CodeBlocks\projekt\class\main.cpp|28|error: initializing argument 3 of 'long unsigned int _beginthread(void (*)(void*), unsigned int, void*)'|
||=== Build finished: 2 errors, 0 warnings ===|

The code are down below.
I saw in the old post that volatile where in the wrong place so I placed it right before uhm... Making an instance(?) with it.

#include <iostream>
#include <process.h>
#include "windows.h"

using namespace std;

struct mitt
{
    bool done;
    int antal;
};

void main2(void *pParam)
{
    mitt *pmitt = (mitt*)pParam;
    for(int j=0; j<pmitt->antal; j++)
    {
        cout << "Thread #2: " << j << "n";
    }
    pmitt->done = true;
}

int main()
{
   volatile mitt var;
    var.done = false;
    var.antal = 10;
    _beginthread(main2, 0 , &var);

    for(int i=0; i<9; i++)
    {
        cout << i << "\n";
    }

    if(!var.done)
    {
        cout << "Thread #2 is done!";
    }
    while(!var.done)
    {
        Sleep(10);
    }
}



I would also like to thank you for those tutorials.
I have been struggling with inheritance for a long time but seeing your video tutorial made everything crystal.
Have a nice evening!


Offline
 Profile  
 
 Post subject: Re: C++ VMK 15 - Threads
PostPosted: Tue Jun 14, 2011 8:41 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I'm not familiar with Code::blocks so I don't know how much help I will be. However as the error suggests, the third parameter of the _beginthread() function call must be a void* not a volatile void*. You could try to cast your pointer using the following to see if that works:

_beginthread(main2, 0 , (void*)&var);


Offline
 Profile  
 
 Post subject: Re: C++ VMK 15 - Threads
PostPosted: Wed Jun 15, 2011 2:15 am 

Joined: Tue Jun 14, 2011 11:51 am
Posts: 4
Marek wrote:
I'm not familiar with Code::blocks so I don't know how much help I will be. However as the error suggests, the third parameter of the _beginthread() function call must be a void* not a volatile void*. You could try to cast your pointer using the following to see if that works:

_beginthread(main2, 0 , (void*)&var);


It worked thanks!


Offline
 Profile  
 
 Post subject: Re: C++ VMK 15 - Threads
PostPosted: Mon Aug 05, 2013 10:25 pm 

Joined: Sat Jul 27, 2013 9:23 am
Posts: 6
In "Create 2nd Thread", at the 1:56 mark, you increase the variable 'j' from 10 to 100. Yet when you run the program, it only still displays 0 thru 9 instead of thru 99. Did something not compile before running or did I not understand something?


Offline
 Profile  
 
 Post subject: Re: C++ VMK 15 - Threads
PostPosted: Tue Aug 06, 2013 5:17 am 

Joined: Thu Sep 27, 2007 5:51 pm
Posts: 48
The second thread's counter he change from 10 to 100, but since the first thread loops 10 times,
and then encounter return 0(in the main function/thread), the program is terminated before the second thread finish.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 15 - Threads
PostPosted: Tue Aug 06, 2013 11:26 am 

Joined: Sat Jul 27, 2013 9:23 am
Posts: 6
Ah, That makes sense. Marek goes pretty fast and I had to pause and re watch a few times to absorb all that he's saying.

Thanks.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 10 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron