multi threading

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Howker
Posts: 51
Joined: Thu Jan 22, 2009 4:40 pm

multi threading

Post by Howker »

Hello everyone!

Can anyone tell me how to convert this piece of code into a multi threading application.
I want a new (windows) thread for each client object. (each client objects also creates a player object, this player object belongs to the same thread)

Code: Select all

#include "windows.h"
#include <iostream>
#include <process.h>
 
using namespace std;
 
//#####################CLASSES##################
class Player
{
public:
        Player();
};
 
Player::Player()
{
        cout << "Player object created." << endl;
}
 
class Network
{
public:
        Network();
        void Update();
};
 
Network::Network()
{
        cout << "Network object created." << endl;
}
 
void Network::Update()
{
        cout << "Network update";
}
 
 
//##############################################
 
 
class Client
{
public:
        Client(int id, Network* net);
        void Update(int &c);
private:
        int ID;
        int counter;
        Network* network;
};
 
Client::Client(int id, Network* net) : ID(id), network(net)
{
        counter = 0;
        Player p;
}
 
void Client::Update(int &c)
{
        Sleep(500);
        counter = c;
        cout << "Client Update - ";
        network->Update();
        cout << endl;
}
//##############################################
//##############################################
 
int main()
{
        Network *network = new Network();
        Client *client[4];
        for(int i = 0; i < 4; i++)
        {
                client[i] = new Client(i, network);
        }
        int counter = 0;
        while(true)
        {
                counter++;
                for(int i = 0; i < 4; i++)
                        client[i]->Update(counter);
        }
 
        for(int i = 0; i < 4; i++)
                delete client[i];
 
        delete network;
 
        return 0;
}
Thanks a lot!
Last edited by Howker on Sat Nov 19, 2011 12:58 pm, edited 2 times in total.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: multi threading

Post by Radikalizm »

Why don't you study up on multithreaded programming instead of asking others to write the code for you?
This is at least as bad as "Hi, here's my code, fix it please!"

Even if someone were to write this for you, if you don't understand the principles of multithreading and the consequences behind a multithreaded approach you won't be able to maintain or even understand the code provided

EDIT:

I may be a bit harsh here, but my point stands. Try something by yourself before going to the forums, there's a wealth of information which you could've studied online. You could've at least tried to use the threading library provided by windows, or boost, or whatever to make your app multithreaded by yourself
Howker
Posts: 51
Joined: Thu Jan 22, 2009 4:40 pm

Re: multi threading

Post by Howker »

Radikalizm wrote:Why don't you study up on multithreaded programming instead of asking others to write the code for you?
This is at least as bad as "Hi, here's my code, fix it please!"

Even if someone were to write this for you, if you don't understand the principles of multithreading and the consequences behind a multithreaded approach you won't be able to maintain or even understand the code provided

EDIT:

I may be a bit harsh here, but my point stands. Try something by yourself before going to the forums, there's a wealth of information which you could've studied online. You could've at least tried to use the threading library provided by windows, or boost, or whatever to make your app multithreaded by yourself
You could atleast give me some good links... I understand the basics of multithreading, I just can't figure how to convert this code.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: multi threading

Post by mongoose7 »

Before 'while (true)' you create four threads and you send them to a function like

threadfn(int tid)
{
while (true)
Client[tid]->update(counter++);
}

But this has nothing to do with Irrlicht. And if you intend to use Irrlicht you have to appreciate that Irrlicht is a rendering engine and (so) it is single-threaded.
Howker
Posts: 51
Joined: Thu Jan 22, 2009 4:40 pm

Re: multi threading

Post by Howker »

Thanks, that helped a little I guess...
mongoose7 wrote:Before 'while (true)' you create four threads and you send them to a function like
But this has nothing to do with Irrlicht. And if you intend to use Irrlicht you have to appreciate that Irrlicht is a rendering engine and (so) it is single-threaded.
I am allowed to post general programming questions here. ;-)
Post Reply