• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

pthreads, HALP

Status
Not open for further replies.

Doytch

Member
So I'm writing a basic web server for class, and I'm trying to get the multithreading to work. Each thread would run a function in my HTTPserver class, which services one connection. I want to start off by running 5 threads, and then whenever someone connects, start a new thread, until I hit a max_threads ceiling.

Now, our prof gave us some code that's supposed to make it simpler to pass the correct function into pthread_create. When my problems started acting up, I gave up and tried to go to a lower and lower level, until I get to this:

Code:
int main() {
    mutex = new Mutex();
    int i=0;
    pthread_t thread[6];
    pthread_attr_t DetachedAttr;
    pthread_attr_init(&DetachedAttr);
    pthread_attr_setdetachstate(&DetachedAttr,PTHREAD_CREATE_DETACHED);  
  
    while(1){
      if(i<6){
        int res = pthread_create(&(thread[i]), &DetachedAttr, trix, NULL); 
	i++;
      }
    }
    return 0;
}

The problem here is that trix is never executed. pthread_create returns a 0, so no errors there.

Trix just increments a global sum inside a mutex. It's defined as void *trix(void *), and is located in the same file.

The detached bits I threw in after it didn't run trix when I passed in NULL as an attribute.

The only way I can get trix to execute is if I call a join() somewhere(when it's not detached of course). Obviously I'm new to writing threaded programs, so is this working as intended? Do the functions not execute until the main program reaches a join() or something? Google didn't seem to verify that.

Help is very much appreciated!
 

Doytch

Member
OK, this is a problem with the shittacular Solaris labs we have to use for this. I've always tested it remotely, but when I compile/run it at home on my Linux box, it works.

Now if I could find a way around this...
 
Status
Not open for further replies.
Top Bottom