变态的libDispatch结构分析-dispatch_object_s

基础类

dispatch_object_s是一个透明联合体。

透明联合类型削弱了C语言的类型检测机制。或者,换言之,它起到了类似强制类型转换的效果。

在一定程度上打破了类型对我们的束缚,使数据以一种更底层的角度呈现在我们面前。不过这样也弱化了C语言对类型的检测,由此也可能带来一些很严重的错误。

可以参看:http://nanjingabcdefg.is-programmer.com/posts/23951.html

typedef union {
	struct dispatch_object_s *_do;
	struct dispatch_continuation_s *_dc;
	struct dispatch_queue_s *_dq;
	struct dispatch_queue_attr_s *_dqa;
	struct dispatch_group_s *_dg;
	struct dispatch_source_s *_ds;
	struct dispatch_source_attr_s *_dsa;
	struct dispatch_semaphore_s *_dsema;
} dispatch_object_t __attribute__((transparent_union));


通过透明联合体,使得dispatch_object_s就像C++中的基类。因此在函数参数类型的转换上,给C语言的参数传递带来极大的方便。

/*
 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
 *
 * @APPLE_APACHE_LICENSE_HEADER_START@
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @APPLE_APACHE_LICENSE_HEADER_END@
 */

#ifndef __DISPATCH_BASE__
#define __DISPATCH_BASE__

#ifndef __DISPATCH_INDIRECT__
#error "Please #include <dispatch/dispatch.h> instead of this file directly."
#endif

#ifdef __cplusplus
/*
 * Dispatch objects are NOT C++ objects. Nevertheless, we can at least keep C++
 * aware of type compatibility.
 */
typedef struct dispatch_object_s {
private:
	dispatch_object_s();
	~dispatch_object_s();
	dispatch_object_s(const dispatch_object_s &);
	void operator=(const dispatch_object_s &);
} *dispatch_object_t;
#else
typedef union {
	struct dispatch_object_s *_do;
	struct dispatch_continuation_s *_dc;
	struct dispatch_queue_s *_dq;
	struct dispatch_queue_attr_s *_dqa;
	struct dispatch_group_s *_dg;
	struct dispatch_source_s *_ds;
	struct dispatch_source_attr_s *_dsa;
	struct dispatch_semaphore_s *_dsema;
} dispatch_object_t __attribute__((transparent_union));
#endif

typedef void (*dispatch_function_t)(void *);

#ifdef __cplusplus
#define DISPATCH_DECL(name) typedef struct name##_s : public dispatch_object_s {} *name##_t
#else
/*! @parseOnly */
#define DISPATCH_DECL(name) typedef struct name##_s *name##_t
#endif

#ifdef __GNUC__
#define DISPATCH_NORETURN __attribute__((__noreturn__))
#define DISPATCH_NOTHROW __attribute__((__nothrow__))
#define DISPATCH_NONNULL1 __attribute__((__nonnull__(1)))
#define DISPATCH_NONNULL2 __attribute__((__nonnull__(2)))
#define DISPATCH_NONNULL3 __attribute__((__nonnull__(3)))
#define DISPATCH_NONNULL4 __attribute__((__nonnull__(4)))
#define DISPATCH_NONNULL5 __attribute__((__nonnull__(5)))
#define DISPATCH_NONNULL6 __attribute__((__nonnull__(6)))
#define DISPATCH_NONNULL7 __attribute__((__nonnull__(7)))
#if __clang__
// rdar://problem/6857843
#define DISPATCH_NONNULL_ALL
#else
#define DISPATCH_NONNULL_ALL __attribute__((__nonnull__))
#endif
#define DISPATCH_SENTINEL __attribute__((__sentinel__))
#define DISPATCH_PURE __attribute__((__pure__))
#define DISPATCH_WARN_RESULT __attribute__((__warn_unused_result__))
#define DISPATCH_MALLOC __attribute__((__malloc__))
#define DISPATCH_FORMAT(...) __attribute__((__format__(__VA_ARGS__)))
#else
/*! @parseOnly */
#define DISPATCH_NORETURN
/*! @parseOnly */
#define DISPATCH_NOTHROW
/*! @parseOnly */
#define DISPATCH_NONNULL1
/*! @parseOnly */
#define DISPATCH_NONNULL2
/*! @parseOnly */
#define DISPATCH_NONNULL3
/*! @parseOnly */
#define DISPATCH_NONNULL4
/*! @parseOnly */
#define DISPATCH_NONNULL5
/*! @parseOnly */
#define DISPATCH_NONNULL6
/*! @parseOnly */
#define DISPATCH_NONNULL7
/*! @parseOnly */
#define DISPATCH_NONNULL_ALL
/*! @parseOnly */
#define DISPATCH_SENTINEL
/*! @parseOnly */
#define DISPATCH_PURE
/*! @parseOnly */
#define DISPATCH_WARN_RESULT
/*! @parseOnly */
#define DISPATCH_MALLOC
/*! @parseOnly */
#define DISPATCH_FORMAT(...)
#endif

#if __GNUC__
#define DISPATCH_EXPORT extern __attribute__((visibility("default")))
#else
#define DISPATCH_EXPORT extern
#endif

#endif




您可以使用线程池来实现将文件分段读取并拷贝到新文件功能。以下是一个简单的示例代码,使用C语言的线程池库 `pthreadpool`: ```c #include <stdio.h> #include <stdlib.h> #include <pthreadpool.h> // 定义线程池的大小 #define THREAD_POOL_SIZE 4 // 定义每个线程处理的文件块的大小 #define CHUNK_SIZE 1024 // 读取并拷贝文件的线程函数 void* copy_chunk(void* arg) { FILE* source_file = ((FILE**)arg)[0]; FILE* dest_file = ((FILE**)arg)[1]; size_t chunk_size = (size_t)arg[2]; // 分配内存缓冲区 char* buffer = (char*)malloc(chunk_size); // 读取文件块并拷贝到新文件 size_t bytes_read = fread(buffer, 1, chunk_size, source_file); fwrite(buffer, 1, bytes_read, dest_file); // 释放内存缓冲区 free(buffer); return NULL; } int main() { FILE* source_file = fopen("source.txt", "rb"); FILE* dest_file = fopen("destination.txt", "wb"); if (source_file == NULL || dest_file == NULL) { printf("无法打开文件\n"); return 1; } // 获取源文件的大小 fseek(source_file, 0, SEEK_END); size_t file_size = ftell(source_file); fseek(source_file, 0, SEEK_SET); // 计算需要分割的块数 size_t num_chunks = (file_size + CHUNK_SIZE - 1) / CHUNK_SIZE; // 初始化线程池 pthreadpool_t thread_pool = pthreadpool_create(THREAD_POOL_SIZE); // 分配线程参数数组 FILE** args = (FILE**)malloc(sizeof(FILE*) * 2); args[0] = source_file; args[1] = dest_file; args[2] = CHUNK_SIZE; // 提交任务给线程池 pthreadpool_parallelize(thread_pool, copy_chunk, args, num_chunks); // 销毁线程池 pthreadpool_destroy(thread_pool); // 关闭文件 fclose(source_file); fclose(dest_file); return 0; } ``` 此示例代码使用了 `pthreadpool` 库来创建线程池。它首先打开了源文件和目标文件。然后,通过获取源文件的大小,计算需要分割的块数。接下来,初始化线程池,并为每个线程分配参数数组,其中包括源文件、目标文件和块大小。最后,通过调用 `pthreadpool_parallelize` 函数提交任务给线程池,完成文件的分段读取和拷贝操作。 请注意,在实际使用时,您可能需要根据自己的需求进行适当的修改和错误处理。此外,您还可以使用其他线程池库来实现类似的功能,如 `libuv`、`libdispatch` 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值