Skip to content

Instantly share code, notes, and snippets.

@lega911
Created May 17, 2019 23:29
Show Gist options
  • Save lega911/4f3887c73e6d41ec3ca74dfbc069e421 to your computer and use it in GitHub Desktop.
Save lega911/4f3887c73e6d41ec3ca74dfbc069e421 to your computer and use it in GitHub Desktop.
import threading
from ctypes import cdll
lib = cdll.LoadLibrary('./liblib.so')
class Foo(object):
def __init__(self):
self.obj = lib.Foo_new()
def bar(self):
lib.Foo_bar(self.obj)
def run():
f = Foo()
f.bar()
threading.Thread(target=run).start()
threading.Thread(target=run).start()
threading.Thread(target=run).start()
p = threading.Thread(target=run)
p.start()
p.join()
// g++ -c -fPIC lib.cpp -o lib.o
// g++ -shared -Wl,-soname,liblib.so -o liblib.so lib.o
#include <iostream>
class Foo{
public:
void bar(){
std::cout << "Start" << std::endl;
for(int i=0;i<100000000;i++) {
for(int j=0;j<100000000;j++) {}
}
std::cout << "End" << std::endl;
}
};
extern "C" {
Foo* Foo_new(){ return new Foo(); }
void Foo_bar(Foo* foo){ foo->bar(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment