Use boost thread Library

Source: Internet
Author: User
How to get started with boost's cross-platform thread Library (Linux)

Boost homepage:Http://www.boost.org/
Click Download on the homepage to download the SourceForge page. The latest version is boost_000033_1, and multiple file formats are available for download (including. Zip, .Tar.gz, etc.) with the same content, all of which are boost_201733_1Source code. Download and decompress the package. (assume the directory is/home/yjguo/boost_000033_1 ).
 
Most of the content in boost can be directly SourceCodeAnd thread needs to compile the corresponding Library first.
 
Linux platform:
Redhat9.0 is installed by default.
1. Compile jam (jam is the basis for compiling other libraries)
Go to the/home/yjguo/boost_1_33_1/tools/build/jam_src directory.
Run./build. Sh.
After running, the bin. linuxx86 directory will appear, and the required bjam directory will be displayed.
 
The build. Sh script automatically detects GCC and calls GCC to compile bjam. (A lot of information is mentioned in the index.html file under the jam_srcdirectory)


2. compile the thread library
To Go To The/home/yjguo/boost_000033_1 directory.
run the program. /tools/build/jam_src/bin. linuxx86/bjam -- With-thread stage (only compile thread Library)
after compilation, the result is in the/home/yjguo/boost_1_33_1/bin/Boost/libs/thread/build directory (including debug/relase ,. a /. in addition, because the stage option is used during compilation, all the results will be copied to the/home/yjguo/boost_1_33_1/stage/lib directory.
3. Prepare to use the thread library
select the compiled thread dynamic library (. So share object ).
copy the libboost_thread-gcc-mt-1_33_1.so.1.33.1 to the/usr/lib/directory
copy the libboost_thread-gcc-mt-d-1_33_1.so.1.33.1 to the/usr/lib/directory
to/usr//lib directory run
ln-s libboost_thread-gcc-mt-1_33_1.so.1.33.1 libboost_thread-gcc-mt-1_33_1.so

Ln-s libboost_thread-gcc-mt-d-1_33_1.so.1.33.1 libboost_thread-gcc-mt-d-1_33_1.so
 
4. Use the thread Library
Create the main. cpp file in the/home/yjguo directory with the following content:
# Include <boost/thread. HPP>
# Include <iostream>
Void Hello ()
{
STD: cout <"Hello world, I'm a thread! "<STD: Endl;
}
Main ()
{
Boost: thread thrd (& Hello );
Thrd. Join ();
}
 
Run g ++-I/home/yjguo/boost_1_33_1-pthread-lboost_thread-gcc-mt-1_33_1 main. cpp to get the. Out file.

./A. Out to run.

 

How to get started with boost's cross-platform thread Library (Windows) Boost homepage: Http://www.boost.org/ Others, .tar.gz, etc.), with the same content, all are boost_1 _ 44 All source code of _ 1. Download and decompress the package. (assume that the decompressed directory is D: \ boost \ boost_1 _ 44 _ 1 ).   Most of the content in boost can be used directly in source code, while thread needs to compile the corresponding Library first.   Windows XP platform: Vs2008 is installed on my machine 1. Compile jam (jam is the basis for compiling other libraries)
Start the command line to enter D: \ boost \ boost_1 _ 44 _ 1 \ tools \ build \ jam \ src directory Run build. bat. After running the script, the new bin.ntx86directory is stored in the directory where bjam.exe is located.     2. Compile the thread Library
Copy bjam.exe D: \ boost \ boost_1 _ 44 _ 1 Directory
Start the command line to enter D: \ boost \ boost_1 _ 44 _ 1 directory Bjam -- toolset = msvc-9.0 -- With-thread stage (only compile thread Library)
Bjam -- Toolset = msvc-9.0 -- with-date_time stage (also requires the date_time Library)   After compilation, the result is in D: \ boost \ boost_1 _ 44 _ 1 \ bin \ boost \ libs \ thread \ build directory (including debug/relase, dll/lib, etc.); in addition, because we used the stage option during compilation, so all the results will be copied to D: \ boost \ boost_1 _   44   _ 1 \ stage \ lib directory.     3. Prepare to use the thread Library Select the compiled thread dynamic library.

The generated files-1-44 and none are actually exactly the same. Change libboost_thread-vc90-mt-gd-1_44.lib
Libboost_thread-vc90-mt-1_44.lib
Libboost_date_time-vc90-mt-gd-1_44.lib
Libboost_date_time-vc90-mt-1_44.lib Copy these four files to the library directory of vs2008. My files are c: \ Program Files \ Microsoft Visual Studio 9.0 \ Vc \ Lib.

required 1. set code geneartion ------ Runtime Library to/MDD or/MD
2. directory settings. Main Menu "Tools"-> "OPTIONS ..." In the displayed window, add the directory where the boost header file is located
D: \ boost \ boost_000044_1

4. Use the thread LibraryCreate an empty Win32 console project in vs2008. Code:# Include <boost/thread. HPP>
# Include <iostream>Void Hello ()
{
STD: cout <"Hello world, I'm a thread! "<STD: Endl;
}Main ()
{
Boost: thread thrd (& Hello );
Thrd. Join ();
}Compile and run the program.

In addition, the following methods are summarized:

Let's take a look at the boost: thread constructor. Boost: thread has two constructor functions:
(1) thread (): constructs a thread object that represents the current execution thread;
(2) Explicit thread (const boost: function0 <void> & threadfunc ):
 Boost: function0 <void>A function with No Response (return void) and no parameters can be simply described as follows. The function here can also be a function consisting of a class overload operator (). The constructor imports a function object instead of a function pointer, such a class with general function features can also be passed in as a parameter, as shown in the following example.
Method 1: the simplest method
# Include <boost/thread. HPP>
# Include <iostream>
 
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
 
Int main (INT argc, char * argv [])
{
Boost: thread thrd (& Hello );
Thrd. Join ();
Return 0;
}
Method 2: Use a complex type object as a parameter to create a thread:
# Include <boost/thread. HPP>
# Include <boost/thread/mutex. HPP>
# Include <iostream>
 
Boost: mutex io_mutex;
 
Struct count
{
Count (int id): ID (ID ){}

Void operator ()()
{
For (INT I = 0; I <10; ++ I)
{
Boost: mutex: scoped_lock
Lock (io_mutex );
STD: cout <id <":"
<I <STD: Endl;
}
}

Int ID;
};
 
Int main (INT argc, char * argv [])
{
Boost: thread thrd1 (count (1 ));
Boost: thread thrd2 (count (2 ));
Thrd1.join ();
Thrd2.join ();
Return 0;
}
Method 3: Create a thread in the class;
(1) Class internal static method to start the thread
# Include <boost/thread. HPP>
# Include <iostream>
Class helloworld
{
Public:
Static void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Static void start ()
{

Boost: thread thrd (Hello );
Thrd. Join ();
}
 
};
Int main (INT argc, char * argv [])
{
Helloworld: Start ();
 
Return 0;
}
Here, both the START () and hello () methods must be static methods.
(2) If the START () and hello () methods are not required to be static, use the following method to create a thread:
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
# Include <iostream>
Class helloworld
{
Public:
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Void start ()
{
Boost: function0 <void> F = boost: BIND (& helloworld: Hello, this );
Boost: thread thrd (f );
Thrd. Join ();
}
 
};
Int main (INT argc, char * argv [])
{
Helloworld hello;
Hello. Start ();
Return 0;
}
(3) create a thread in singleton mode:
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
# Include <iostream>
Class helloworld
{
Public:
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Static void start ()
{
Boost: thread thrd (boost: bind
(& Helloworld: Hello, & helloworld: getinstance ()));
Thrd. Join ();
}
Static helloworld & getinstance ()
{
If (! Instance)
Instance = new helloworld;
Return * instance;
}
PRIVATE:
Helloworld (){}
Static helloworld * instance;
 
};
Helloworld * helloworld: instance = 0;
Int main (INT argc, char * argv [])
{
Helloworld: Start ();

 

Return 0;
}
Method 4: Use class internal functions to create threads outside the class;
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>

# Include <string>
# Include <iostream>
Class helloworld
{
Public:
Void Hello (const STD: string & Str)
{
STD: cout <STR <STD: Endl;
}
};
 
Int main (INT argc, char * argv [])
{
Helloworld OBJ;
Boost: thread thrd (boost: BIND (& helloworld: Hello, & OBJ, "Hello
World, I'm a thread! "));
Thrd. Join ();
Return 0;
}
If the function to be bound by a thread has parameters, use boost: bind. For example, if you want to use boost: thread to create a thread to execute the function: void F (int I), it is wrong to write: boost: thread thrd (f, the thread constructor accepts a type with no parameters and the return type is void. If the parameter I value is not provided, the thread constructor cannot run. In this case, you can write: boost :: thread thrd (boost: BIND (F, 1 )). Basically, the binding issues involving function parameters are boost: thread, boost: function, and boost: bind.

Original article: http://weiwu83.javaeye.com/blog/98388
Http://dev.firnow.com/course/3_program/c++/cppjs/200856/114821.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.