Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: starting a pthread in suspended mode



I am looking for a way to create a pthread that is initially suspended.

The portable way is to use a condition variable with a predicate. Something like this (but more error checking):


static int ready_to_go = 0;
static pthread_mutex_t suspend_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;

static void *func(void *arg) {
	pthread_mutex_lock(&suspend_mutex);
	while (ready_to_go == 0) {
		pthread_cond_wait(&suspend_cond, &suspend_mutex);
	}
	pthread_mutex_unlock(&suspend_mutex);
	/* Now do our thing */
	...
	return NULL;
}

void spawn_thread(void) {
	pthread_attr_t attr;
	pthread_t thread;

	pthread_attr_init(&attr);
	/* We don't need to join this thread */
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_create(&thread, &attr, func, NULL);
	pthread_attr_destroy(&attr);

	/* Do some stuff */
	...
	/* Now tell thread to continue */
	pthread_mutex_lock(&suspend_mutex);
	ready_to_go = 1;
	pthread_mutex_unlock(&suspend_mutex);
	pthread_cond_signal(&suspend_cond);
}


matt. _______________________________________________ unix-porting mailing list | email@hidden Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/unix-porting Do not post admin requests to the list. They will be ignored.



References: 
 >starting a pthread in suspended mode (From: Roel de Jong <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.