Why is jthread used? To put it simply, ourProgramWe often need to consider the problem of multithreading and cross-platform compilation. However, the multi-thread Writing Method in Win32 and Linux is quite different, so we have to use a rather ugly one.
# Ifdef Win32
# Else
# Endif
SuchCodeIt doesn't matter if there are just a few words. If there are too many words, we don't know what the main intention is. If we choose jthread, we don't have to worry about it.
In the small aspect, many open-source projects also depend on some other open-source projects. For example, jrtp depends on jthread. So it is quite useful to install this library.
Okay, first download the source code or here http://research.edm.uhasselt.be /~ Jori/page/index. php? N = cs. jthread
Decompress the package
Tar-xvf jrtplib-3.7.1.tar.bz2
I usually like to install these libraries by default, that is, install these libraries in/usr/local, so
./Configure
Make
Make install
Remember to check whether the database is put
Shared library search path
Otherwise, you will receive this error during the execution after compilation.
Error while loading shared libraries: libjthread-1.2.1.so: cannot open shared object file: no such file or directory
Shared library search path
You can copy so to/usr/lib or add it to the/etc/lD. So. conf directory.
Then
Ldconfig-V
I am using the latter
Or execute your program as follows:
LD_LIBRARY_PATH =/usr/local/lib./run
Now you can write a helloworld about jthread.
Myfunc. h
# Ifndef _ myfunc_h _
# Define _ Myfunc_h _
# Include<Jthread. h>
IntPrint_func ();
ClassMythread:PublicJthread {
Public:
Void *Thread ();
};
# Endif
Myfunc. cpp
# Include < Iostream >
# Include < String >
# Include " Myfunc. h "
int print_func () {
sleep ( 1 );
STD: cout " hello! " STD :: endl;
return 0 ;
}
Void *Mythread: thread (){
Jthread: threadstarted ();
Print_func ();
Return 0;
}
Only a few lines of Main Functions
Main. cpp
# Include < Iostream >
# Include " Myfunc. h "
IntMain (){
Sleep (1);
//Run it first.
STD: cout< "Create thread" <STD: Endl;
Mythread arr_t [10 ];
For ( Int I = 0 ; I < 10 ; I ++ ){
Arr_t [I]. Start ();
}
STD: cout < " Over! " < STD: Endl;
Sleep ( 1 );
Return 0 ;
}
Then there is makefile.
Makefile
Objects = Myfunc. O main. o
Cxxflags = - I / USR / Local / Include / Jthread - G
Commlib = - Lstdc ++ - L / USR / Local / Lib - Ljthread - Lpthread
Run: $ (Objects)
G ++ - O run $ (Objects) $ (Commlib)
Main. O: myfunc. h
Myfunc. O: myfunc. cpp myfunc. h
Gcc - C myfunc. cpp $ (Cxxflags)
. Phony: clean
Clean:
Rm run$(Objects)
Make
Run
./Run
Run result
Create thread
Over !
Hello !
Hello !
Hello !
Hello !
Hello !
Hello !
Hello !
Hello !
Hello !
Hello !
Linbc @ Cheng - Ubuntu :~ / Workspace / Example_jthread $