C ++ Concurrency and multithreading study notes - Detailed parameter passing

  • Passing temporary object
    • trap
    • to sum up
  • Temporary object parameter as a thread
    • The concept of thread id
    • When the temporary arrest of object construction
  • Passing class objects, smart pointers
  • Member function pointers to make the thread function

Temporary object passed as a parameter thread

Created more than one worker thread, the thread is determined based on the content of the work number. Each thread needs to know their number. There are many threads fallible wording

Example 1

Multi-thread function to be performed:

void my_print(const int &i, char* p_mybuff)
{
	cout << i << endl;
	cout << p_mybuff << endl;
	return ;
}

 The main function of writing

	int mvar = 1;
	int& mvary = mvar;
	char mybuf[] = "this is a test!";
	thread myobj(my_print, mvar, mybuf);
	myobj.join();

	cout << "Main Thread!!!" << endl;

 Explanation: Note the trap! ! ! Cited reference and pointer variable transmission.

Quote: surface seem correct, but there are a lot of details need to pay attention, if the join () changed to detach (), then the main thread and the child thread are executed, then the main thread that may arise over the implementation, but passing in parameters (quote form) of the resource has been released, there is no error possible? :

        myobj.detach()

  When creating the thread object, make a copy, the copy went in value, so it is not really a reference, the actual value is passed, the wrong resource release does not occur. But the other way round, then the value of the variable main thread does not change.

Pointer: If the parameter passed is a pointer to the second argument unsafe, if detach () when executing thread, not recommended "reference", the pointer will definitely have a problem!

What is the correct usage: do not pass by reference, the case if you want to pass the string.

char * p_mybuff into const string & p_mybuff, does not point to the same memory, when there has been variable type of conversion? In fact the presence of the primary functions performed finished, p_mybuff have been recovered, only the system to convert a string p_mybuff

	thread myobj(my_print, mvar, string(mybuf));

  If a cast string to generate a temporary object, in this case bound to temporary objects p_mybuff. When a thread is created, the method of constructing temporary variables to pass parameters is feasible, at this time will be called once the constructor, then detach can run correctly. In order to prevent the main thread exits, the child thread of illegal memory references.

1) for transmitting int, a value passed directly.

2) If the class object passed, to avoid an implicit type conversion. All while creating a thread to construct temporary objects, functions with reference to the reception passed in the function parameters. Otherwise, the system will construct a multi-function, resulting in unnecessary waste.

3) the main thread type conversion, then A (mysecondpar) operation this step by the main thread

thread myobj(myprint, mvar, A(mysecondpar));

thread myobj(myprint, mvar, mysecondpar);

4) is not recommended detach, join with

Temporary object passed as a parameter thread

Thread ID: at the operating system level need to schedule a thread, a thread ID, thread ID and file descriptors, used to manage resource allocation in this thread. Require scheduling between threads, there is also a priority concept. Each corresponds to a thread ID, thread ID can be used inside the C ++ Standard Library functions to acquire std :: this_thread :: get_id ().

When the function parameter passing to a thread, inlet parameters are const, const can not be modified in function, if you need to modify variables in the initialization of "mutable". In C ++, the mutable but also to break through the restrictions of const and settings. The mutable modified variable, the variable will always be in a state, even in a const function.

 

Guess you like

Origin www.cnblogs.com/rynerlute/p/11795565.html