本文整理汇总了C++中mutex::try_lock方法的典型用法代码示例。如果您正苦于以下问题:C++ mutex::try_lock方法的具体用法?C++ mutex::try_lock怎么用?C++ mutex::try_lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mutex
的用法示例。
在下文中一共展示了mutex::try_lock方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: try_lock
bool try_lock()
{
check_for_hierarchy_violation ();
if (!internal_mutex.try_lock())
return false;
update_hierarchy_value ();
return true;
}
示例2: faultyBusyIncrement
void faultyBusyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m2.unlock(); // expected-warning{{Potential copy-paste error; did you really mean to use 'm2' here?}}
if (i > 1000) {
return;
}
}
}
}
示例3: busyIncrement
void busyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m1.unlock(); // expected-note{{Similar code using 'm1' here}}
if (i > 1000) {
return;
}
}
}
}
示例4: busyIncrement
void busyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m1.unlock(); // expected-note{{suggestion is based on the usage of this variable in a similar piece of code}}
if (i > 1000) {
return;
}
}
}
}
示例5: faultyBusyIncrement
void faultyBusyIncrement() {
while (true) {
if (m1.try_lock()) {
++i;
m2.unlock(); // expected-warning{{suspicious code clone detected; did you mean to use 'm1'?}}
if (i > 1000) {
return;
}
}
}
}
示例6: lector
void lector()
{
//while(1)
{
mtx.lock();
cl++;
db.try_lock();
mtx.unlock();
file_read();
mtx.lock();
cl--;
if(cl == 0) db.unlock();
mtx.unlock();
}
}
示例7: read_count
ul read_count() {
ul sum;
int lock = gblcnt_mutex.try_lock();
if ( lock ) {
sum = globalcount;
for ( auto &ctr : counter ) {
sum += (ul)ctr;
}
gblcnt_mutex.unlock();
} else {
return 0;
}
return sum;
}
示例8: add_count
bool add_count ( ul delta, atomic<int> *counter, ul *countermax ) {
if ( (*countermax - *counter) >= delta ) { // Fastpath
*counter += delta;
return true;
}
int lock = gblcnt_mutex.try_lock();
if ( lock ) {
globalize_count( counter, countermax );
if ( globalcountmax - globalcount - globalreserve < (ul)delta ) {
gblcnt_mutex.unlock();
return false;
}
globalcount += (ul)delta; // Slowpath
balance_count( counter, countermax );
gblcnt_mutex.unlock();
return true;
} else {
return false;
}
}
示例9: readLoc
void Sloong::SloongWallUS::ProcessEventList(int id, queue<RECVINFO>* pList, mutex& oLock, int sock, int nPriorityLevel, CLuaPacket* pUserInfo, CEpollEx* pEpoll, CMsgProc* pMsgProc)
{
// if not empty
while (!pList->empty())
{
if (oLock.try_lock() == false)
{
continue;
}
unique_lock<mutex> readLoc(oLock, std::adopt_lock);
// recheck is not empty when lock done.
if (pList->empty())
{
oLock.unlock();
continue;
}
RECVINFO msg = pList->front();
pList->pop();
oLock.unlock();
ProcessEvent(id, &msg, sock, nPriorityLevel, pUserInfo, pEpoll,pMsgProc);
}
}
示例10: parallelQuickSortWithPool1
void parallelQuickSortWithPool1(vector<int> &arr, int callCnt, int st = -1, int ed = -1)
{
//c++11 里的线程内变量为 thread_local 或者__thread(mac下测试正常能用),但是vs从2015开始才支持,所以win下用 __declspec (thread),但是经测试,发现无效
//static __declspec (thread) int regressionCnt = 0;//无效,因为只有一个线程能用到,其他线程阻塞
//regressionCnt++;
//cout << "id=" << std::this_thread::get_id() << ',' << "regressionCnt=" << regressionCnt << endl;
callCnt++;
bool loop = true;
while (loop){
//mtx2.lock();
if (cpuFinishCount <= 0)
{
//mtx2.unlock();
//cout << "id=" << std::this_thread::get_id() << " is breaked"<<endl;
//printf("id=%d is breaked\n", std::this_thread::get_id());
//printArr(arr);
break;
}
//mtx2.unlock();
if (st != -1 || mtx.try_lock()>0)
{
int start, end;
if (st!=-1)
{
start = st; end = ed;
loop = false;//坑啊,因为漏了这一句,查了半天错
}
else{
if (taskArr.size() == 0){
mtx.unlock();
break; //这里必须break
//continue;
}
auto task = taskArr[taskArr.size() - 1];
taskArr.pop_back();
mtx.unlock();
start = task->start;
end = task->end;
callCnt = 0;
//free(task);//???这里会报错,故注释
}
int a = arr[start];
int j = start + 1;
for (int i = j; i <= end; i++)
{
if (arr[i] <= a)
{
if (i != j) swap0(arr, i, j++);
else j++;
}
}
if (j > start + 1)
{
swap0(arr, start, j - 1);
}
//cout << "id=" << std::this_thread::get_id() << ','<<"start,end="<< start<<","<<end<<endl;
//printf("id=%d , ", std::this_thread::get_id());
//printf("start=%d ,end=%d\n", start, end);
//printArr(arr);
auto id = std::this_thread::get_id();
bool has2 = j > start + 2 && j < end;
if (j > start + 2)
{
//mtx2.lock();
cpuFinishCount++;
//mtx2.unlock();
int nextstart = start;
int nextend = j - 2;
while (arr[nextend] == arr[j - 1] && nextend>start)//去重,重复相同的直接跳过,
nextend--;
if (nextstart != nextend)
{
if (has2 || callCnt > NESTING)
{
auto v0 = (struct v *)malloc(sizeof(struct v*));
v0->start = nextstart;
v0->end = nextend;
//mtx.lock();
taskArr.push_back(move(v0));
//mtx.unlock();
}
else
{
parallelQuickSortWithPool1(arr, callCnt, nextstart, nextend);
}
}
}
if (j < end)
{
//mtx2.lock();
cpuFinishCount++;
//mtx2.unlock();
int nextstart= j ;
int nextend= end;
while (arr[nextstart] == arr[j - 1] && nextstart<nextend)
nextstart++;
if (nextstart != nextend)
{
//.........这里部分代码省略.........
示例11: ExtTryLock
int ExtTryLock() {
return m_mutex.try_lock()?1:0;
}
示例12: try_lock
bool try_lock(error_code& ec = throws)
{
return try_lock("mutex::try_lock", ec);
}