Storing Custom Context Information with a Queue

Storing Custom Context Information with a Queue

All dispatch objects (including dispatch queues) allow you to associate custom context data with the object.To set and get this data on a given object, you use the dispatch_set_context and dispatch_get_context functions. The system does not use your custom data in any way, and it is up to you to both allocate anddeallocate the data at the appropriate times. 

<div class="page" title="Page 46"><div class="layoutArea"><div class="column"><p><span style="font-size: 10pt; font-family: MyriadSet;">// Installing a queue clean up function </span></p></div></div></div>
void myFinalizerFunction(void *context)
  {

      MyDataContext* theData = (MyDataContext*)context;
      // Clean up the contents of the structure
      myCleanUpDataContextFunction(theData);
      // Now release the structure itself.
      free(theData);
  }
  dispatch_queue_t createMyQueue()
  {
      MyDataContext*  data = (MyDataContext*) malloc(sizeof(MyDataContext));
      myInitializeDataContextFunction(data);
      // Create the queue and set the context data.
      dispatch_queue_t serialQueue =
  dispatch_queue_create("com.example.CriticalTaskQueue", NULL);
      if (serialQueue)
      {
          dispatch_set_context(serialQueue, data);
          dispatch_set_finalizer_f(serialQueue, &myFinalizerFunction);
      }
      return serialQueue;
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值