Sign in to follow this  
3dnewbie

pthreads and main rendering function

Recommended Posts

3dnewbie    100
Hi, I am putting either a call to scan_network() in my GlutIidle function. This scan_network function is supposed to create a thread (using Posix threads), but no more then 1, because a) the idle function gets called very fast and i don't want to implement any locking mechanisms yet. Besides, because i'm only passing one ip address per pass, it would be a bit of a waste. In later implementation, i will pass by netblock and then i will be using more threads, but that's for the future things to do. basically i just use the thread so the render function can go on without blocking. I could have used a fork as well but i don't want to fiddle with shared memory and stuff. My question is, how to go about this ? Basically i need to have a way of checking if a thread already exists. I might even move the scan_network function from the idle function, and just put it somewhere else, but the problem still is: If i have a function which does (upon being called) pthread_create(etc etc) pthread_join (etc) it is blocking which i don't want. i'd rather use nonblocking (so i can get on with the main loop) but that doesn't seem to exist. any examples would be appreciated.

Share this post


Link to post
Share on other sites
pulpfist    528
I dont see why you would call pthread_create more than once, before the render loop starts.
And pthread_join once when the game shuts down.

The way I see it the idle function dont even need to know that the thread exists or what it does.

But first:
Are you making a simple demo to test out some networking/synchronization or is it a serious multiplayer game?
Are you going to have a server program?
What is this thread going to do?

Share this post


Link to post
Share on other sites
Zahlman    1682
Let me see if I have this straight. You have a structure something like this (and forgive me as I don't remember all the names of stuff)?


#include "gl.h"
#include "pthread.h"
// etc.

void do_scan(void* args) {
// scan the network somehow
}

void scan_network() {
pthread_create(do_scan, 0);
}

int main() {
// other OGL stuff
glutSetIdle(scan_network);
}


I think you miss the point of the threading: separate threads of execution share the processor already. GL provides an "idle function" so that you can do things in series with your program, but that's inappropriate for you because of the blocking I/O), so you want to do things in parallel - thus the idle function no longer helps you out. Try something like this instead:


#include "gl.h"
#include "pthread.h"
// etc.

void do_scan(void* args) {
while (true) {
// scan the network somehow
sleep(POLL_DELAY);
}
}

void do_idle() {
sleep(MIN_FRAME_DELAY);
}

int main() {
// other OGL stuff
glutSetIdle(do_idle);
}


Of course, you still have the challenge of communicating information from the network-scanning thread back to the main program. :)

Share this post


Link to post
Share on other sites
3dnewbie    100
Hmm.
W'ell i've fixed it. I moved the scan network function out of idle. Not only does the idle function gets called a gazillion times which even mutexes would have no benefit anymore i'd gather, because it get's called so often i have to manage alot of threads which i don't want to do in the first place.

I have now put it at the mercy of the users request if it is scanning, like so:

for(current=start;current<=end;current++) {
reserved=reverse_int(current);
/* Resolve hostname */
if((he=gethostbyname(reserved))==(struct hostent *)NULL) {
/* Dang. Let's just use ip address then */
pthread_create(&tid,NULL,snmp_walk,reserved);
pthread_join(tid,NULL);}
else {
pthread_create(&tid,NULL,snmp_walk,he->h_name);
pthread_join(tid,NULL);
}
//exit (1);
}
}

fclose(fp);
NETWORKSCAN=!NETWORKSCAN; /* Turn off */
pthread_exit(0);


This code is a snippet of the function. Because this works fine, but serialized, i am going to change this to first fire off creates (bound my maximum of defined threads), then later get them back with joins (with an array of p_thread id's, and in the threads themselves i lock them with mutexes. I hope that speeds things up, i just hope the mutexes are fast enough.

It's not a game, not a demo, just a piece of software i'm working on :-P
thanks for all your replies

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this