- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
#include <ppl.h>
#include <windows.h>
#include <ppltasks.h>
#include <iostream>
#include <vector>
using namespace Concurrency;
using namespace std;
CRITICAL_SECTION cs6;
int main(int argc, char* argv[])
{
size_t concurentThreadsSupported = std::thread::hardware_concurrency();
cout << concurentThreadsSupported << endl;
//deadlock hazard increased with concurentThreadsSupported decreasing
size_t taskAmountForWasteVirtualCores = concurentThreadsSupported - 1;//must be equal to (virtual processor thread amount from Concurrency::IResourceManager) - 1
vector<task<void>> t;
for (size_t i = 0; i<taskAmountForWasteVirtualCores; ++i)
t.push_back(create_task([]{
Sleep(INFINITE);//some very long IO operation or deadlocked by EnterCriticalSection or sql transaction or other
}));
Sleep(1000);
cout << "another way:" << endl;
InitializeCriticalSection(&cs6);
auto locker = create_task([]{
cout << "locker" << endl;
EnterCriticalSection(&cs6);//same as begin sql transaction
cout << "locker entered cs 6" << endl;
Concurrency::wait(500);//Deadlock by any concurrency context switching directly or indirectly by std or MFC (events, mutex, etc)
cout << "locker played" << endl;
LeaveCriticalSection(&cs6);//same as end sql transaction
cout << "~locker ok" << endl;
});
auto locked = create_task([]{
cout << "locked" << endl;
EnterCriticalSection(&cs6);//same as begin sql transaction
cout << "locked entered cs 6" << endl;
Concurrency::wait(500);
cout << "locked played" << endl;
LeaveCriticalSection(&cs6);//same as end sql transaction
cout << "~locked ok" << endl;
});
Sleep(1000);
cout << "FAIL" << endl;
return 0;
}
laMer007 04.03.2015 16:48 # 0
laMer007 04.03.2015 17:12 # 0
laMer007 04.03.2015 19:12 # 0
http://rextester.com/AKZL36928
laMer007 04.03.2015 19:13 # 0
kegdan 04.03.2015 20:20 # +5
OCEHHuu_nemyx 16.10.2020 13:51 # 0
TarasB 04.03.2015 22:18 # +5
roman-kashitsyn 04.03.2015 23:45 # +2
blackhearted 05.03.2015 14:26 # +2
laMer007 17.03.2015 23:46 # 0
guest 01.04.2017 10:43 # −15