Created
April 22, 2013 12:49
-
-
Save wusuopu/5434651 to your computer and use it in GitHub Desktop.
在python中使用C的多线程。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Python.h> | |
#include <pythread.h> | |
#include <pthread.h> | |
void* test(void *ptr) | |
{ | |
while (1) | |
{ | |
printf("in C thread\n"); | |
sleep(1); | |
} | |
} | |
static PyObject * | |
thread_test(PyObject *self) | |
{ | |
pthread_t pid; | |
pthread_create(&pid, NULL, test, NULL); | |
printf("thread id: %ld\n", pid); | |
Py_RETURN_TRUE; | |
} | |
static PyMethodDef lc_thread_test_methods[] = { | |
{"test", (PyCFunction)thread_test, METH_NOARGS, ""}, | |
{NULL, NULL, 0, NULL} | |
}; | |
PyMODINIT_FUNC initlc_thread_test(void) | |
{ | |
Py_InitModule("lc_thread_test", lc_thread_test_methods); | |
PyEval_InitThreads(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment