/** =====================================================================================** Filename: Mutex.cpp** Description:** Version: 1.0* Created: 2016年01月18日 14时23分02秒* Last Modified: 2017年12月31日 12:30:49* Revision: none* Compiler: gcc** Author: zt (),* Organization:** =====================================================================================*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <iostream>#include <thread>#include <mutex>#include <chrono>#include <sys/syscall.h>class TestData{public:TestData(){d = 0;}~TestData(){}public:uint8_t d;};volatile int count = 0;std::mutex mtx;void attempt_10k_increases (){for ( int i = 0; i < 10000; ++i ){if ( mtx.try_lock() ){++count;mtx.unlock();}}}#if 0void print_block ( int n, char c ){mtx.lock();for ( int i = 0; i < n ; ++i )std::cout << c;std::cout << " " << syscall ( SYS_gettid ) << std::endl;mtx.unlock();}#endifint main ( int argc, char* argv[] ){( void ) argc;( void ) argv;#if 0std::thread ( print_block, 30, '*' ).detach();std::thread ( print_block, 30, '#' ).detach();std::this_thread::sleep_for ( std::chrono::seconds ( 1 ) );#endifstd::thread threads[10];for ( int i = 0; i < 10 ; ++i ){threads[i] = std::thread ( attempt_10k_increases );threads[i].detach();}sleep ( 1 );std::cout << count << std::endl;return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。