[libdispatch-dev] Regarding dispatch_get_main_queue() and the thread it is actual executing on

Marius Zwicker marius at mlba-team.de
Tue Apr 19 08:07:05 PDT 2011


Hi everyone,

During my work for libDispatch I had to find a way for implementing TSD de-allocators (which are not available on windows). While doing so I did some extensive debugging on OS X / Linux for understanding what is actually happening when calling dispatch_main(). Weird enough it seemed to me as if the main thread would never execute an item from the main queue but simply go to sleep instead.

To verify this behaviour, I wrote a test to compare the pthread_t handles during execution:


static pthread_t main_thread;

void pass(void* dt){
    pthread_t now = pthread_self();
    assert(pthread_equal(now, main_thread) == 1);
    exit(0);
}

void dispatch_api() {
    dispatch_queue_t q = NULL;
    main_thread = pthread_self();
    q = dispatch_get_main_queue();
	assert(q != NULL);
    dispatch_async_f(q, NULL, pass);
    ...
    dispatch_main();
}

This test is approving my suspicions, as the pthread_equal(...) assertion is constantly failing. So the big question is: What am I missing? I thought the main queue is available as a mean to be sure some blocks execute on the main thread (e.g. when updating the GUI is only possible while being on the main thread).

I welcome any hints or help.

- Marius Zwicker

	
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/libdispatch-dev/attachments/20110419/5533ea98/attachment.html>


More information about the libdispatch-dev mailing list