About Store Forum Documentation Contact



Post Reply 
Threading in a list
Author Message
Dynad Offline
Member

Post: #1
Threading in a list
Hey,

Im trying to use threading in a list of threads.. but my problem is that the first 1 gets created but the second one i want to create just hangs and doing nothing.

I will show a small snippet cause the class is too large for posting it here:

PHP Code:
class Main
{
Main();
~
Main();
static 
Bool ThreadSession(Thread &threadCaller);

Thread _session;


Then in the cpp class i have:
PHP Code:
std::map<SystemAddressSessionRunnable*>::iterator i _sessions.find(p->systemAddress);
        if(
== _sessions.end()) { //create new session if the session doesnt exist.
            
SessionRunnablesession = new SessionRunnable(this);
            
            
_session.create(ThreadSession200session);
            
_sessions[p->systemAddress] = session;            
        } 

But when it hits the second time this thead just hangs... only my main thread keeps going.. otherwise my entire program will crash/hang in a second.

So who can help me out? smile


Thnx,
~Dynad

There is always evil somewhere, you just have to look for it properly.
11-26-2010 08:04 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Threading in a list
I'd need to see some more sources, but:
-where do you store the Thread objects?
remember that:
struct Thread // Thread !! must be placed in constant memory address !!

-you're using +2 priority for thread creation, you can try 0

-for each thread you need to use different Thread object

where in cpp is the 2nd code snippet?
11-26-2010 09:36 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #3
RE: Threading in a list
Ok well i have a main.cpp where i create 2 threads.
1. The EE Render.
2. My netlayer.

In my netlayer main class i create session for each connected user which is understandable. But i don't know if its correct in EE that you can use a thread inside a thread but ok.

I changed the code a little but still having same probs.

Session header class which is needed to be multithreaded:
PHP Code:
class SessionRunnable
{
public:
    
SessionRunnable(Socket *);
    
void Run(void);
    
void sleep(uint32);
    
inline Session *GetInstance(void) { return _i; }

private:
    
Session *_i;
    
Socket _socket;
}; 
Session cpp:
PHP Code:
SessionRunnable::SessionRunnable(Socket socket)
{
    
_socket socket;
}
/******************************************************************************/
void SessionRunnable::Run(void)
{
    
_i = new Session(_socket);
    
_i->Run();
    
    
delete _i;


Main session handler header:
PHP Code:
class Netlayer
{
public:
Main();
~
Main();
static 
Bool ThreadSession(Thread &threadCaller);

typedef std::map<SystemAddressThread*> SessionMap;

private:
Thread _session;
SessionMap _sessions;

and the cpp:
PHP Code:
std::map<SystemAddressThread*>::iterator i _sessions.find(p->systemAddress);
        if(
== _sessions.end()) { //create new session if the session doesnt exist.
            
SessionRunnablesession = new SessionRunnable(this);
            
            
_session.create(ThreadSession200session);
            
_sessions[p->systemAddress] = &_session;            
        } 

I hope this gives you an idea how it works.


Thnx,
~Dynad

There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 11-27-2010 01:31 PM by Dynad.)
11-26-2010 11:18 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Threading in a list
Hi,

well the codes aren't clear to me, so I don't understand them.

in this line:
_session.create(ThreadSession, 2, 0, 0, session);
aren't you creating different threads using the same Thread object?
11-27-2010 07:59 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #5
RE: Threading in a list
Yes i want to create more threads with the same Thread _session; variable. But i guess thats not possible right? How else can you make a list of threads without pre defining it in the e.g header file? Cause you can't tell before hand how many ppl are gonna connect to the server otherwise that will take alot of thread variables pfft

There is always evil somewhere, you just have to look for it properly.
11-27-2010 08:12 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Threading in a list
well I've written before:
Quote:-for each thread you need to use different Thread object

Quote:How else can you make a list of threads without pre defining it in the e.g header file?
Memx<Thread> threads;
11-27-2010 08:23 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #7
RE: Threading in a list
Ok well i can use Memx for that... but then i need the EE::Map aswell but ive some probs with it.

PHP Code:
class Socket 
{
public:
Socket();
~
Socket();

    static 
Bool Create(Data &dataKey &keyPtr user); // create element from 'key'
    
static Int Compare(Key &aKey &b); // compare 2 keys


    
Map<Key,Datamap(Create,Compare,NULL);



Gives me this as error which i dont really understand why:
error C2061: syntax error : identifier 'Create'

There is always evil somewhere, you just have to look for it properly.
11-27-2010 09:07 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: Threading in a list
try Socket::Create
11-27-2010 09:13 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #9
RE: Threading in a list
No it still gives me the same error.

There is always evil somewhere, you just have to look for it properly.
11-27-2010 09:45 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
RE: Threading in a list
ah, you cant initialize the member inside struct declaration:

Map<Key,Data> map;
};

later in Socket constructor:

Socket::Socket() : map(...)
{
}
11-27-2010 11:29 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #11
RE: Threading in a list
Ah yes that compiles now.. thnx! smile

There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 11-28-2010 10:32 AM by Dynad.)
11-28-2010 10:32 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #12
RE: Threading in a list
Is there a way to delete elements from a map based on the Key? I can find only based on "int i" which i can't use as i only know the key of the element. Any suggestions?

There is always evil somewhere, you just have to look for it properly.
11-28-2010 08:36 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #13
RE: Threading in a list
struct Map
{
void removeKey(C KEY &key) { _Map:: removeKey(&key);} // remove element from container, after making this call you may no longer use the element at all
}

if you don't have it, you need to update SDK
11-28-2010 08:38 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #14
RE: Threading in a list
Ah i guess i have to update my sdk then smile
Ok well updated and i can delete based on key now... but here it comes my next problem pfft

With threading and a thread needs to close/delete.. it deletes the class by itself and after that i delete the pointer again from my map which results in a crash. But i need to clean my map otherwise it will eat memory leaks of course..

Or how can i access my Map from the thread function which is a static function?

PS: In the thread itself ive a main update loop.. and when the session needs to exit it will leave the Run() func and delete thread... but the element still exists in the Thread Map which i dont really need.

There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 11-29-2010 07:20 PM by Dynad.)
11-28-2010 08:39 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #15
RE: Threading in a list
Nvm got it working now... Ive made a request handler which will delete the map after the thread is closed. smile

There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 11-29-2010 06:40 PM by Dynad.)
11-29-2010 06:40 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply