Jun
Jun
Seems like CI always fails, but neither is relevant
You should make your question readable first.
# 0x00 主线程 Python 默认会有一个主线程,其实就是进程本身。 ```c // Python/pylifecycle.c _PyInitError _Py_InitializeCore(const _PyCoreConfig *core_config) { ... tstate = PyThreadState_New(interp); if (tstate == NULL) return _Py_INIT_ERR("can't make first thread"); ``` _Py_InitializeCore 初始化调用`PyThreadState_New`来创建第一个线程状态。 ```c...
# 0x01 线程的创建 ```c static PyObject * thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) { PyObject *func, *args, *keyw = NULL; struct bootstate *boot; unsigned long ident; ... // [1] 创建bootstate结构boot并初始化 boot =...
# 0x02 线程的执行 传给了`pthread_create`函数用来创建线程的func参数是`t_bootstrap`函数,arg参数包装了线程信息的boot对象,也就是说主线程创建的子线程将会执行`t_bootstrap(boot)`。 `t_bootstrap`定义在`Modules/_threadmodule.c`: ```c static void t_bootstrap(void *boot_raw) { struct bootstate *boot = (struct bootstate *) boot_raw; PyThreadState *tstate; PyObject *res; tstate = boot->tstate; tstate->thread_id = PyThread_get_thread_ident(); _PyThreadState_Init(tstate);...
# 0x03 线程的销毁 `PyThreadState_Clear`清除当前线程对应的线程状态对象,所谓清理,实际上比较简单,就是对线程状态对象中维护的东西进行引用计数的维护。 `PyThreadState_DeleteCurrent`释放线程状态对象并释放GIL。 ```c void PyThreadState_DeleteCurrent() { PyThreadState *tstate = GET_TSTATE(); if (tstate == NULL) Py_FatalError( "PyThreadState_DeleteCurrent: no current tstate"); tstate_delete_common(tstate); if (_PyRuntime.gilstate.autoInterpreterState && PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate) {...
# 0x04 线程的调度 ## 时间调度 当然,子线程是不会一直执行`t_bootstrap`到释放GIL,Python中持有GIL的线程会在某个时间释放GIL。 > In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be able to release the GIL on demand by another thread. A volatile boolean...
## 字符串对象 前一篇介绍过,Redis 的字符串对象有三种编码使用不同底层数据结构来实现: `OBJ_ENCODING_INT`、`OBJ_ENCODING_EMBSTR`和`OBJ_ENCODING_RAW`。 ### int 编码 如果存储的是可以用 long 类型来表示整数值,在 Redis 中会用 int 编码来表示字符串对象。 ``` 127.0.0.1:6379> SET a 1 OK 127.0.0.1:6379> TYPE a string 127.0.0.1:6379> OBJECT ENCODING a "int"...
@huosan0123 3.7吧
@yaolipro 是串了代码,已经改过来了,感谢指正