cpu直接超過80%了, python調用c++的庫可以充分使用多核
附上代碼:
c++調用python的方法
#include <iostream>
#include <python27/Python.h>
#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
using namespace std;
using namespace boost::python;
int main() {
//初始化讀取 py 文件的信息
std::ifstream fin;
fin.open("D:/py_test/test.py");
std::string str;
std::string str_in = "";
while (getline(fin, str)) //一行一行地讀到字符串str_in中
{
str_in = str_in + str + '\n';
}
fin.close();
cout<<str_in<<endl;
Py_Initialize();
// PyRun_SimpleString("from time import time,ctime\n"
// "print 'Today is',ctime(time())\n");
PyRun_SimpleString(str_in.c_str());
Py_Finalize();
cout<<"it is over ...."<<endl;
return 0;
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <iostream>
//頭文件是我自己設置的位置
#include <python27/Python.h>
#include <boost/python.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
void test(){
string sum;
while(1)
{
string ss = "111111112222222";
sum = sum + ss;
}
}
char const* greet()
{
thread_group group;
for(int num=0;num<10;num++)
group.create_thread(bind(test));
group.join_all();
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_world)
{
using namespace boost::python;
def("greet", greet);
}
python測試代碼
#! /usr/bin/env python
# Copyright Joel de Guzman 2002-2007. Distributed under the Boost
# Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)
# Hello World Example from the tutorial
import hello_world
print(hello_world.greet())