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

Re: thread support



Andy Mortimer <andy.mortimer@poboxes.com> writes:

> Although how well this interacts with dynamically-loaded shared libraries
> is anyone's guess;

What do you mean?

> I suspect I may have to go the global-variable route myself, which
> is why I was asking for examples/docs.

Bruce is right that in general the best thing is to avoid globals and
statics, and pass everything as an argument, but assuming you can't,
let's assume that your global data structure is called the_state.
Here's an example of a function safely modifying the data:

GState the_state;
pthread_pmutex_t the_state_guard;

void
some_function() {
  pthread_mutex_lock(&the_state_guard);
  
  /* do whatever with the_state.  No other thread that respects the mutex
     can touch it while you're in here.  They'll block until you
     unlock the mutex. */

  pthread_mutex_unlock(&the_state_guard);
}

int main(int argc, char **argv) {
  pthread_mutex_init(&the_state_guard, NULL);

  /* other stuff */

}

See "man pthread_mutex_init" and other pthread_* man pages in
libc6-doc.

-- 
Rob


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-devel-request@lists.debian.org . 
Trouble?  e-mail to templin@bucknell.edu .


Reply to: