Swift 线程

  • NSThread

  • pthread 现在主要是如何在swift中使用pthread(c + swift) 其他后面更新

  • GCD async (TODO)

  • GCD sync (TODO)

NSThread

pthread

  • Xcode 7.0.1 + Swift 2

  • 好像直接使用**pthread_...**是不行的 编译问题 暂时忽略 ...

1 在c中使用pthread封装要用的接口

pthread.c

#include <pthread.h>


int32_t create_thread (
	uint64_t * out_tid,
	const void * _run,
	void * _arg) {

	int ret;
	long int run = (long int)_run;
	long int arg = (long int)_arg;

	ret = pthread_create((pthread_t *restrict)out_tid, NULL,
		(void *(*)(void *))run, (void *restrict)arg);

	return (int32_t)ret;
}


int32_t cancel_thread (uint64_t tid) {

	int ret = pthread_cancel((pthread_t)tid);

	return (int32_t)ret;
}


int64_t wait_thread_exit (uint64_t tid) {
	int64_t out_ec;

	int _ret = pthread_join((pthread_t)tid, (void **)(&out_ec));


	if (0 == _ret) {
		return out_ec;
	} else {
		return (int64_t)_ret;
	}
}


/*
 * DESC
 * - 此函数应该在线程开始时执行
 *   若线程内部有任何资源申请等操作
 *   应该选择 PTHREAD_CANCEL_DEFERRED 的设定
 *   然后在退出点 (call pthread_testcancel() 用于定义退出点) 进行线程退出
 */
int32_t set_thread_cancel_type (
	int32_t * out_old,
	int32_t t) {

	/*
	 * E.x.
	 * - thread can be cancelled when "pthread_testcancel"
	 *   PTHREAD_CANCEL_DEFERRED
	 * - thread can be cancelled at any time
	 *   PTHREAD_CANCEL_ASYNCHRONOUS
	 */
	int ret = pthread_setcanceltype((int)t, (int *)out_old);

	return (int32_t)ret;
}


int32_t set_thread_cancel_state (
	int32_t * out_old,
	int32_t s) {

	int ret = pthread_setcancelstate((int)s, (int *)out_old);

	return (int32_t)ret;
}


void test_thread_cancel (void) {
	pthread_testcancel();
}

2 使用Swift封装class pthreadclass Pthread

pthread.swift

import Foundation


@asmname("create_thread")
	func _create_thread(
	outTid tid: UnsafePointer<UInt64>,
	routine run: (
		(argument: UnsafePointer <UInt8>?)
		-> Int64
	),
	argument arg: UnsafePointer<UInt8>) -> Int32

@asmname("create_thread")
	func _create_thread2(
	outTid tid: UnsafePointer<UInt64>,
	routine run: (
		(argument: UnsafePointer <UInt8>?) -> Int64
	),
	argument arg: Int64) -> Int32

@asmname("set_thread_cancel_type")
	func _set_thread_cancel_type(
	outOldType olt: UnsafePointer<Int32>,
	type t: Int32) -> Int32

@asmname("set_thread_cancel_state")
	func _set_thread_cancel_state(
	outOldState ols: UnsafePointer<Int32>,
	state s: Int32) -> Int32

@asmname("test_thread_cancel")
	func _test_thread_cancel()

@asmname("cancel_thread")
	func _cancel_thread(tid t: UInt64) -> Int32

@asmname("wait_thread_exit")
	func _wait_thread_exit(tid t: UInt64) -> Int64


public final class pthread {
	public enum Running: Int32 {
	case notSet = -1, running = 1, stopped = 0
	}


	public static func start (
		outTid tid: UnsafePointer<UInt64>,
		routine run: ((UnsafePointer<UInt8>?) -> Int64),
		argument arg: UnsafePointer<UInt8>?) -> Int32 {

		var ret: Int32?

		if let a = arg {
			ret = _create_thread(outTid: tid, routine: run,
				argument: a)
		} else {
			ret = _create_thread2(outTid: tid, routine: run,
				argument: 0)
		}

		return ret!
	}


	public static func setCancelType (
		outOldType olt: UnsafePointer<Int32>,
		type t: Int32) -> Int32 {

		let ret = _set_thread_cancel_type(outOldType: olt, type: t)

		return ret
	}


	public static func setCancelState (
		outOldState ols: UnsafePointer<Int32>,
		state s: Int32) -> Int32 {

		let ret = _set_thread_cancel_state(outOldState: ols, state: s)

		return ret
	}


	public static func testThreadCancel () {
		_test_thread_cancel()
	}


	public static func cancelThread (tid t: UInt64) -> Int32 {
		return _cancel_thread(tid: t)
	}


	public static func waitThreadExit (tid t: UInt64) -> Int64 {
		return _wait_thread_exit(tid: t)
	}


	/* Cancel takes place at next cancellation point */
	public static let PTHREAD_CANCEL_ENABLE: Int32 = 0x01
	/* Cancel postponed */
	public static let PTHREAD_CANCEL_DISABLE: Int32 = 0x00
	/* Cancel waits until cancellation point */
	public static let PTHREAD_CANCEL_DEFERRED: Int32 = 0x02
	/* Cancel occurs immediately */
	public static let PTHREAD_CANCEL_ASYNCHRONOUS: Int32 = 0x00
}


public class Pthread {
	public init () {
	}


	public final func start (
		routine run: (
			(UnsafePointer<UInt8>?) -> Int64
		),
		argument arg: UnsafePointer<UInt8>? = nil)
		-> Int32 {

		return pthread.start(outTid: &self._tid, routine: run,
			argument: arg)
	}


	public final var tid: UInt64 {
		return self._tid.first!
	}

	public var running: pthread.Running = pthread.Running.notSet


	private final var _tid: [UInt64] = BufferUtil.getBuffer(uint64_count: 1)
}

转载于:https://my.oschina.net/yuiwong/blog/513406

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值