The difference between boost and c++11 creating threads

Source: Internet
Author: User

C++11 introduced line threading, to the vast number of C + + bitter programmer brought the gospel, previously used in the project is boost library, in order to respond to the warm call of C++11, decided to start using the latest project C++11 line threading!

So I was happy to write the following code:

#include <iostream>
#include <thread>
#include <unistd.h>
void haha (int& i) {
	Std::cout << i << std::endl;
}

int main (int argc, char *argv[]) {
	int a = 2;
	Std::thread T2 (Haha, a);
	Sleep (929);
	return 0;
}
However, the compilation has failed. Can not ah, boost is so written Ah, and wrote several times, in addition to the function name in front of the "&", there is no difference ah, do not believe you see
#include <boost/thread/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream >
void haha (int& i) {
	std::cout << i << std::endl;
}

int main (int argc, char* argv[]) {
    int a = 2;
    Boost::thread THRD (&haha, a);
    Sleep (929);
    return 0;
}
This code boost run no problem ah, what the reason. Looking at the error tip I gave, I found that the original c++11 thread function does not support reference passing. I think also ah, the thread's life cycle does not know, the reference to their own variables to the thread, to join themselves to modify or delete this piece of content, so that the thread of the situation, the thread is not run to get the garbage value, or crash.

Therefore, the thread functions of C + + do not support reference passing of variables (support for this practice), and error prompts are given at compile time. Knowing the reason, the first piece of code just needs to delete a reference symbol to get it done.

void haha (int i) {
	std::cout << i << std::endl;
}

int main (int argc, char *argv[]) {
	int a = 2;
	Std::thread T2 (Haha, a);
	Sleep (929);
	return 0;
}
C++11 problem is solved, that boost before the use is not a problem, continue to see boost.

With doubts about the life of the confused mood, wrote down the following code:

void haha (int& i) {while
	(1) {
		std::cout << i << std::endl;
	}
}
int main (int argc, char* argv[]) {
    int a = 2;
    Boost::thread THRD (&haha, a);
    Sleep (1);
    A = 3;
    Sleep (929);
    return 0;
}
Output 222222222222222222222222, all 2 (I feel like this is the number AH), there is no I want to see "3", so the thread in the execution of the object is not a previously created object, not its reference. In other words, I'm writing a reference pass, but boost itself has been transformed into a value transfer, OH, yes, so why should I do this?


The use of this piece was the passing of a smart pointer, so I wrote this test code again:

#include <iostream>
#include <thread>
#include <unistd.h>
class Test {public
:
	Test (int num): num_ (num) {}
	~test () {}
	void print () {
		static int i = 0;
		Std::cout << num_ << "" <<  i++ << std::endl;
	}
Private:
	int num_;
};

void Fun (std::shared_ptr<test>& Test) {while
	(1) {
		std::cout << test.use_count () << Std::endl;
		Test->print ();
	}

void Test () {
	std::shared_ptr<test> shareptr (new test (1));
	Std::thread T (fun, shareptr);
	Sleep (929);
}

int main (int argc, char *argv[]) {
	test ();
	return 0;
}
As expected, still cannot compile pass, change to value pass:
#include <iostream>
#include <thread>
#include <unistd.h>
class Test {public
:
	Test (int num): num_ (num) {}
	~test () {}
	void print () {
		static int i = 0;
		Std::cout << num_ << "" <<  i++ << std::endl;
	}
Private:
	int num_;
};

void Fun (Std::shared_ptr<test> Test) {while
	(1) {
		std::cout << test.use_count () << std:: Endl;
		Test->print ();
	}

void Test () {
	std::shared_ptr<test> shareptr (new test (1));
	Std::thread T (fun, shareptr);
	Sleep (929);
}

int main (int argc, char *argv[]) {
	test ();
	return 0;
}
Compile pass, run no problem, look at the results of the run, the smart pointer above the number of references to 2, a main thread, a created thread, no problem.

What about boost. Write the following code test:

#include <boost/thread/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>

class Test {public
:
	test (int num): num_ (num) {}
	~test () {}
	void print () {
		static int i = 0;
  
   std::cout << num_ << "" <<  i++ << std::endl;
	}
Public:
	int num_;
};

void Hello (boost::shared_ptr<test>& Test) {while
	(1) {
		std::cout << "thread->" << Test.use_count () <<  Std::endl;
		Test->print ();
	}
void Test () {
	boost::shared_ptr<test> sharedptr (new test);
	Boost::thread thrd (&hello, sharedptr);
	Sleep (929);
}

int main (int argc, char* argv[]) {
    test ();
    return 0;
}
  
This code is a reference pass, the compilation is successful, the result is run, and the number of references to the smart pointer is 2. Matches the previous conjecture (boost passes the reference of the thread function, which is changed by default to the value pass).

Write Value pass:

#include <boost/thread/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>

class Test {public
:
	test (int num): num_ (num) {}
	~test () {}
	void print () {
		static int i = 0;
  
   std::cout << num_ << "" <<  i++ << std::endl;
	}
Public:
	int num_;
};

void Hello (boost::shared_ptr<test> Test) {while
	(1) {
		std::cout << "thread->" << Test.use_count () <<  Std::endl;
		Test->print ();
	}
void Test () {
	boost::shared_ptr<test> sharedptr (new test);
	Boost::thread thrd (&hello, sharedptr);
	Sleep (929);
}

int main (int argc, char* argv[]) {
    test ();
    return 0;
}
  
The compilation was successful, the operation was successful, and the running result showed that the reference count of the smart pointer was 3, AH. 3. What's going on. For what reason. I can only guess is that boost did a special deal, based on limited capacity, can only be thrown out, please interested friends advice and discussion.

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.