Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

f*ing pointer! Why doesn't this work?



This isn't fair... the code looks like it should work but there is a problem 
in it somewhere.  It's seg faulting on a mutex lock in in the function 
funcWrapper but I don't have a mutex in that function :(
I'd greatly appreciate any insight anyone might have.

(and and I know the function pointer is getting garbled somwhere that's why 
it's not being called... that's a second problem)

(don't forget "-lpthread" on the gcc line if you try and compile it)

		-fjr

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int threadSpawn(void (*func)(), int *);
void cleanUpThreads(void);

//---------------------------------------------------------------------

void do_one_thing(int *);
void do_another_thing(int *);

int Flag;

int main(void) {
  int x,y;
  Flag = 0;

  printf("raw   %p - %p\n",&do_one_thing, do_one_thing);
  
  if (threadSpawn(do_one_thing,&x) == -1) {
    printf("ERROR\b");
  }

  do_another_thing(&y);

  cleanUpThreads();
  
  return 0;
}


void do_one_thing(int *pnum_times) {
  int i,j,x;

  for (i = 0; i < 4; i++) {
    while(Flag == 0) {};
    Flag = 0;
    printf("Do One Thing I'm %d\n",pthread_self());
    for (j = 0; j < 10000; j++) x = x ++;
    (*pnum_times)++;
  }
}

void do_another_thing(int *pnum_times) {
  int i,j,x;

  for (i = 0; i < 4; i++) {
    while(Flag == 1) {}
    Flag = 1;
    printf("Do Another Thing. I'm %d\n",pthread_self());
    for (j = 0; j < 20000; j++) x = x ++;
    (*pnum_times)++;

  }
}

//---------------------------------------------------------------------------------------


typedef struct ThreadPool_s {
  pthread_t aThread;
  unsigned char Status;
} ThreadPool_t;

struct spawnData_s {
  void (*func)();
  int *arg;
};

#define MAXTHREADS 128
#define tp_Available 0
#define tp_InUse     1
#define tp_Finished  2

void funcWrapper(void *);

static ThreadPool_t ThreadPool[MAXTHREADS];
static int threadPoolStarted = 0;


void startThreadPool(void);

void cleanUpThreads(void) {
  int idx;
  if (threadPoolStarted == 0) {
    startThreadPool();
    return;
  }

  for (idx = 0; idx < MAXTHREADS; idx++) {
    if (ThreadPool[idx].Status == tp_Finished) {
      pthread_join(ThreadPool[idx].aThread,NULL);
      ThreadPool[idx].Status = tp_Available;
    }
  }
  threadPoolStarted = 1;
}


void startThreadPool(void) {
  int idx;
  for (idx = 0; idx < MAXTHREADS; idx++) {
    ThreadPool[idx].Status = tp_Available;
  }
}


int threadSpawn(void (*func)(), int *aArg) {
  int idx;
  void *arg2;
  struct spawnData_s *data;


  if (threadPoolStarted == 0) {
    startThreadPool();
  }

  data = (struct spawnData_s *)malloc(sizeof(struct spawnData_s));  
  data->func = func;
  data->arg  = aArg;
  arg2 = data;

  printf("data  %p - %p\n",&data->func, data->func); 

  // look for an available slot to put the thread in
  for (idx = 0; idx < MAXTHREADS; idx++) 
    if (ThreadPool[idx].Status == tp_Available) {
      pthread_create(&ThreadPool[idx].aThread, NULL, (void *)funcWrapper, 
&data);
      return 0;
    }

  fflush(stdout);
  // if we get here we didn't find one!
  return -1;
}

void funcWrapper(void *obj) {
  struct spawnData_s *data;
  int idx;
  pthread_t me = pthread_self();

  // find self in the thread pool 
  for (idx = 0; idx < MAXTHREADS; idx++)
    if (pthread_equal(ThreadPool[idx].aThread,me) != 0) {
      ThreadPool[idx].Status = tp_InUse;
    }

  data = (struct spawnData_s *)obj;

  printf("fpntr %p - %p\n",&data->func,data->func);

  //  (data->func)(data->arg);
  free(data);

  ThreadPool[idx].Status = tp_Finished;
}




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org