thread and lock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
int main()
{
	std::mutex m;
	std::thread t1([&]() {
		while (true)
		{
			m.lock();
			std::this_thread::sleep_for(std::chrono::seconds(1));
			std::cout << "xx" << std::endl;
			m.unlock();
			std::this_thread::yield();
		}
	});
	std::thread t2([&]() {
		m.lock();
		std::cout << "yy" << std::endl;
		m.unlock();
	});
	std::cin.get();
}

//on visual studio, thread 't2' can not lock 'm'
Thread t1 is joinable() when the program ends and the destructor of t1 calls std::terminate()
http://en.cppreference.com/w/cpp/thread/thread/~thread

Consider using std::lock_guard http://en.cppreference.com/w/cpp/thread/lock_guard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

int main()
{
	std::mutex m;
    
	std::thread t1( [&]() {
		for( int i = 0 ; i < 3 ; ++i )
		{
			m.lock(); 
			std::this_thread::sleep_for(std::chrono::seconds(1));
			std::cout << "t1: xx" << std::endl;
			m.unlock();
			// std::this_thread::yield(); // unnecessary
		}
	});
    
	std::thread t2([&]() {
		m.lock();
		std::cout << "t2: yy" << std::endl;
		m.unlock();
	});
    
	t2.join() ;
    t1.join() ;
}

http://rextester.com/ZVBNH58562
@JLBorges,
Thanks very much!

But I think that you do not understand me!

'while(true)' can not be removed!

If there is not a 'std::this_thread::yield();', it will lock the mutex all the time.
I want to change the situation, so I use 'std::this_thread::yield();'
But the situation has not improved!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

int main()
{
	std::mutex m;
    
	std::thread( [&]() {
		while(true)
		{
			m.lock(); 
			std::this_thread::sleep_for(std::chrono::seconds(1));
			std::cout << "t1: xx" << std::endl;
			m.unlock();
			// std::this_thread::yield(); // unnecessary
		}
	}).detach() ;
    
	std::thread t2([&]() {
		m.lock();
		std::cout << "t2: yy" << std::endl;
		m.unlock();
	});
    
	t2.join() ;
    std::this_thread::sleep_for(std::chrono::seconds(5));
}

http://rextester.com/EFK50531
on windows10 professional 64bit, visual studio 2015,

there is no 't2: yy' output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <atomic>

int main()
{
    std::mutex stdout_lock;
    std::atomic<bool> quit( false ) ;
    using namespace std::literals ;

    std::thread first( [&] () {
        while( !quit ) {
            {
                std::lock_guard<std::mutex> lock( stdout_lock );
                std::cout << "first: xx" << std::endl;
            }

            std::this_thread::sleep_for( 1ns ); // one nanosecond
        }
    } );

    std::thread second( [&] () {
        std::lock_guard<std::mutex> lock( stdout_lock );
        std::cout << "**** second: yy ****" << std::endl;
    } );

    second.join() ;
    std::this_thread::sleep_for( 30ms ); // thirty milliseconds
    quit = true ;
    first.join() ;
}

http://rextester.com/QTWCG33002
I contoured a project problem.

My colleague use 'sleep_for' method.

and I recommend he to use 'std::this_thread::yield()'.

why is 'yield' method invalid?
std::this_thread::yield is advisory.

On Windows, this presumably calls the WinAPI function ::SwitchToThread(), when the possible yield of execution is limited to a single processor: the processor running the calling thread.

The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority. https://msdn.microsoft.com/en-us/library/windows/desktop/ms686352(v=vs.85).aspx
thanks!
Topic archived. No new replies allowed.