linux simple multi-threaded application

1. Environment Configuration

When include the header file #include, compile under linux multithreading, always will be reported this wrong

:对‘pthread_create’未定义的引用

You can use the link to the relevant library in the CMakeLists.txt

find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
  set_property(TARGET my_app PROPERTY COMPILE_OPTIONS "-pthread")
  set_property(TARGET my_app PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
  target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}")
endif()

Reference http://www.itkeyword.com/doc/1866142651042956x118/cmake-linux
2. practical exercises , learning to find a multithreaded very good blog

c ++ 11 multi-threaded programming (a): three ways to create threads
https://blog.csdn.net/lijinqi1987/article/details/78396512

multi-threaded programming c ++ 11 (b): joining and detaching the thread
https://blog.csdn.net/lijinqi1987/article/details/78396758

Multithreaded Programming c ++ 11 (c): passing parameters to carefully thread
https://blog.csdn.net/lijinqi1987/article/details/78404092
the third part, the pointer specifies a class member function as a thread orbslam function is frequently used in
a specific use for the

#include<iostream>
#include<thread>

class DummyClass{
public:
	DummyClass(){}
	DummyClass(const DummyClass& obj){ }
	void sampleMemberfunction(int x)
	{
		std::cout<<"Inside sampleMemberfunction "<<x<<std::endl;
	}
};

int main()
{
	DummyClass dummyObj;
	int x = 10;
	std::thread threadObj(&DummyClass::sampleMemberfunction,&dummyObj,x);
	threadObj.join();

	return 1;
}

c ++ 11 multi-threaded programming (IV): data sharing and competitive conditions
https://blog.csdn.net/lijinqi1987/article/details/78405623
line 34 of the program there's a small mistake, should be "= = "to" = "

if ((val = testMultithreadWallet()) != 5000)

The results of running this program are as follows inside and found the results for each run are different, with the title Lord said phenomenon consistent, but do not know why, my results are out of the 4000 yuan

jlm@jlm-X450VE:~/桌面/build$ ./Test 
Error at count = 945 Money in Wallet = 4000
Error at count = 961 Money in Wallet = 4000
jlm@jlm-X450VE:~/桌面/build$ ./Test 
Error at count = 983 Money in Wallet = 4000
jlm@jlm-X450VE:~/桌面/build$ ./Test 
jlm@jlm-X450VE:~/桌面/build$ ./Test 
jlm@jlm-X450VE:~/桌面/build$ ./Test 
Error at count = 119 Money in Wallet = 4000
Error at count = 448 Money in Wallet = 4000

c ++ 11 multi-threaded programming (E): use mutex repair competition
https://blog.csdn.net/lijinqi1987/article/details/78412183
mMoney the last paragraph of the first 10 lines of code inside of this money should be changed to
article content brief introduction

//第一种修复竞争方法
  std::mutex mutex;
  {
  	mutex.lock();
  	fun();
  	mutex.unlock();
  }
  //第二种修复方法
  {
  	std::lock_guard<std::mutex> lockGuard(mutex)
  	fun();
  }

The second way a little better than the first, because not only can run at the end of the braces is automatically unlocked, unlocking can occur when abnormal internal braces, here's a way better elsewhere learned from std :: unique_lock, but still did not understand, here today to see the

Guess you like

Origin blog.csdn.net/qq_34122731/article/details/90730587