C++ & Python 多线程笔记
来源:互联网 发布:我没有说话 知乎 编辑:程序博客网 时间:2024/05/21 07:49
C++
Posix多线程
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <vector>using std::vector;void* Proc(void* arg){ pthread_t pthread = *(pthread_t *)arg; printf("this is thread %ld\n", pthread);}int main(int argc, char** argv){ if (argc != 2) { printf("usage:%s thread_num", argv[0]); return 1; } int thread_num = atoi(argv[1]); vector<pthread_t> threads(thread_num); for (int i = 0; i < thread_num; ++i) { pthread_t pthread; pthread_create(&pthread, NULL, &Proc, &threads[i]); threads[i] = pthread; } for (vector<pthread_t>::iterator it = threads.begin(); it != threads.end(); ++it) { pthread_join(*it, NULL); } return 0;}
boost多线程
全局函数作为线程函数
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <vector>#include <string>#include <boost/thread.hpp>using std::vector;using std::string;void Proc(int num, const string& str){ printf("this is thread %d, say %s\n", num, str.c_str());}int main(int argc, char** argv){ if (argc != 2) { printf("usage:%s thread_num", argv[0]); return 1; } int thread_num = atoi(argv[1]); vector<boost::thread*> threads; for (int i = 0; i < thread_num; ++i) { boost::thread* thread = new boost::thread(&Proc, i, "Hello World!"); threads.push_back(thread); } for (vector<boost::thread*>::iterator it = threads.begin(); it != threads.end(); ++it) { (*it)->join(); } return 0;}
类成员函数作为线程函数
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <vector>#include <string>#include <boost/thread.hpp>#include <boost/bind.hpp>using std::vector;using std::string;class Test{public: void Proc(int num, const string& str) { printf("this is thread %d, say %s\n", num, str.c_str()); }};int main(int argc, char** argv){ if (argc != 2) { printf("usage:%s thread_num", argv[0]); return 1; } int thread_num = atoi(argv[1]); vector<boost::thread*> threads; for (int i = 0; i < thread_num; ++i) { Test* test = new Test(); boost::thread* thread = new boost::thread(boost::bind(&Test::Proc, test, i, "Hello World!")); threads.push_back(thread); } for (vector<boost::thread*>::iterator it = threads.begin(); it != threads.end(); ++it) { (*it)->join(); } return 0;}
Python
使用threading模块
import sysimport threadingdef proc(num, str): print 'this is thread %d, say %s' % (num, str)def start_threads(thread_num): threads = [] for i in range(thread_num): thread = threading.Thread(target=proc, args=(i, "Hello World!")) threads.append(thread) thread.start() for thread in threads: thread.join()if __name__ == '__main__': if len(sys.argv) != 2: print "usage:%s thread_num" % sys.argv[0] sys.exit(1) start_threads(int(sys.argv[1]))
使用multiprocessing模块
import sysfrom multiprocessing.dummy import Pool as ThreadPooldef proc(arg): num, str = arg print 'this is thread %d, say %s' % (num, str)def start_threads(thread_num): pool = ThreadPool(thread_num) args = [] for i in range(thread_num): args.append((i, "Hello World!")) pool.map(proc, args) pool.close() pool.join()if __name__ == '__main__': if len(sys.argv) != 2: print "usage:%s thread_num" % sys.argv[0] sys.exit(1) start_threads(int(sys.argv[1]))
0 0
- python 多线程笔记一
- python笔记13--多线程
- C++ & Python 多线程笔记
- 【Python笔记】Python多线程进程如何正确响应Ctrl-C以实现优雅退出
- python多线程学习笔记1
- Python多线程Threading学习笔记
- python学习笔记五(python多线程)
- 《python源码剖析》笔记 python多线程机制
- Python学习笔记18:Python多线程编程
- python多线程ctrl-c退出问题
- 多线程C调用python api的陷阱
- 多线程C调用python api的陷阱
- python多线程文件传输范例(C/S)
- Python 之 多线程编程学习笔记
- python 多线程笔记-模拟生产者与消费者
- Python学习笔记--多线程编程,thread
- python 学习笔记13-----多线程编程
- python学习笔记——多线程编程
- Google Chrome调试js代码
- linux c 编程实战:进程控制总结(四)
- PHP7 微信支付回调失败 解决
- 移动应用开发技术过多,该如何做选择
- HDU 1754 I Hate It
- C++ & Python 多线程笔记
- AndroidStudio 动态修改版本名称
- css布局-两列,左边宽度自适应,右边固定宽度200px
- NSDateFormatter 格式说明
- Storm学习笔记-集群环境安装部署
- Android中实现拨打电话、发送短信、响铃、震动和获取当前时间
- MySql 5.6 版本 2016-07 修改配置文件解决中文乱码问题
- Redis实现微博用户的关注关系
- Apache POI(5):公式(formula)