Run clang-format
This commit is contained in:
104
.clang-format
Normal file
104
.clang-format
Normal file
@@ -0,0 +1,104 @@
|
||||
Language: Cpp
|
||||
# BasedOnStyle: Google
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: TopLevelDefinitions
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
BreakBeforeBraces : Custom
|
||||
BraceWrapping:
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: true
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
BeforeCatch: true
|
||||
BeforeElse: true
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeInheritanceComma: true
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IncludeCategories:
|
||||
- Regex: '^<.*\.h>'
|
||||
Priority: 1
|
||||
- Regex: '^<.*'
|
||||
Priority: 2
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IndentCaseLabels: false
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: false
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: false
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 1
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Left
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: true
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Cpp11
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
114
.cproject
Normal file
114
.cproject
Normal file
@@ -0,0 +1,114 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
|
||||
<storageModule moduleId="org.eclipse.cdt.core.settings">
|
||||
<cconfiguration id="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012" moduleId="org.eclipse.cdt.core.settings" name="Debug">
|
||||
<externalSettings/>
|
||||
<extensions>
|
||||
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012" name="Debug" parent="cdt.managedbuild.config.gnu.cross.exe.debug">
|
||||
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012." name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.debug.1122035376" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.debug">
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.890879207" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/concurrency-in-action}/Debug" id="cdt.managedbuild.builder.gnu.cross.730695170" managedBuildOn="true" name="Gnu Make Builder.Debug" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.680385655" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
|
||||
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.1148144853" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.max" id="gnu.c.compiler.option.debugging.level.1348513497" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.2123668139" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.241274475" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.427163038" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.max" id="gnu.cpp.compiler.option.debugging.level.1633475294" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1614770923" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.301173203" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.1152604941" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1534571725" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
|
||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||
</inputType>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.archiver.1417897260" name="Cross GCC Archiver" superClass="cdt.managedbuild.tool.gnu.cross.archiver"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.assembler.206754207" name="Cross GCC Assembler" superClass="cdt.managedbuild.tool.gnu.cross.assembler">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.2135072614" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
|
||||
</tool>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
<cconfiguration id="cdt.managedbuild.config.gnu.cross.exe.release.1308789762">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.cross.exe.release.1308789762" moduleId="org.eclipse.cdt.core.settings" name="Release">
|
||||
<externalSettings/>
|
||||
<extensions>
|
||||
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.release.1308789762" name="Release" parent="cdt.managedbuild.config.gnu.cross.exe.release">
|
||||
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.release.1308789762." name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.release.2089802121" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.release">
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.321811258" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/concurrency-in-action}/Release" id="cdt.managedbuild.builder.gnu.cross.581862874" managedBuildOn="true" name="Gnu Make Builder.Release" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.1263617891" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
|
||||
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.option.optimization.level.1570617197" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.none" id="gnu.c.compiler.option.debugging.level.1573251332" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1569558056" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.1348481540" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.100340695" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.none" id="gnu.cpp.compiler.option.debugging.level.584780047" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1437636074" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.170044107" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.1428396919" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.948393561" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
|
||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||
</inputType>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.archiver.1625798495" name="Cross GCC Archiver" superClass="cdt.managedbuild.tool.gnu.cross.archiver"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.assembler.70580537" name="Cross GCC Assembler" superClass="cdt.managedbuild.tool.gnu.cross.assembler">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.226212603" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
|
||||
</tool>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<project id="concurrency-in-action.cdt.managedbuild.target.gnu.cross.exe.508515326" name="Executable" projectType="cdt.managedbuild.target.gnu.cross.exe"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="scannerConfiguration">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012;cdt.managedbuild.config.gnu.cross.exe.debug.1192994012.;cdt.managedbuild.tool.gnu.cross.c.compiler.680385655;cdt.managedbuild.tool.gnu.c.compiler.input.2123668139">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012;cdt.managedbuild.config.gnu.cross.exe.debug.1192994012.;cdt.managedbuild.tool.gnu.cross.cpp.compiler.241274475;cdt.managedbuild.tool.gnu.cpp.compiler.input.1614770923">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.release.1308789762;cdt.managedbuild.config.gnu.cross.exe.release.1308789762.;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1348481540;cdt.managedbuild.tool.gnu.cpp.compiler.input.1437636074">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.release.1308789762;cdt.managedbuild.config.gnu.cross.exe.release.1308789762.;cdt.managedbuild.tool.gnu.cross.c.compiler.1263617891;cdt.managedbuild.tool.gnu.c.compiler.input.1569558056">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
</cproject>
|
||||
27
.project
Normal file
27
.project
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>concurrency-in-action</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<triggers>full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
25
.settings/language.settings.xml
Normal file
25
.settings/language.settings.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<project>
|
||||
<configuration id="cdt.managedbuild.config.gnu.cross.exe.debug.1192994012" name="Debug">
|
||||
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="564395207262711818" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
</extension>
|
||||
</configuration>
|
||||
<configuration id="cdt.managedbuild.config.gnu.cross.exe.release.1308789762" name="Release">
|
||||
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="564395207262711818" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
</extension>
|
||||
</configuration>
|
||||
</project>
|
||||
@@ -1,18 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
void hello()
|
||||
void
|
||||
hello()
|
||||
{
|
||||
std::cout << "Hello Concurrent World\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
// boost::thread t([]()
|
||||
std::thread t([]()
|
||||
{
|
||||
std::cout << "Hello Concurrent World\n";
|
||||
});
|
||||
std::thread t([]() { std::cout << "Hello Concurrent World\n"; });
|
||||
|
||||
t.join();
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <numeric> // std::accumulate
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
template <typename Iterator, typename T>
|
||||
struct accumulate_block
|
||||
{
|
||||
struct accumulate_block {
|
||||
void operator()(Iterator first, Iterator last, T& result)
|
||||
{
|
||||
result = std::accumulate(first, last, result);
|
||||
@@ -15,18 +14,19 @@ struct accumulate_block
|
||||
};
|
||||
|
||||
template <typename Iterator, typename T>
|
||||
T parallel_accumulate(Iterator first, Iterator last, T init)
|
||||
T
|
||||
parallel_accumulate(Iterator first, Iterator last, T init)
|
||||
{
|
||||
unsigned long const length = std::distance(first, last);
|
||||
|
||||
if (!length) return init;
|
||||
if (!length)
|
||||
return init;
|
||||
|
||||
unsigned long const min_per_thread = 25;
|
||||
unsigned long const max_threads =
|
||||
(length + min_per_thread - 1) / min_per_thread;
|
||||
|
||||
unsigned long const hardware_threads =
|
||||
std::thread::hardware_concurrency();
|
||||
unsigned long const hardware_threads = std::thread::hardware_concurrency();
|
||||
|
||||
unsigned long const num_threads =
|
||||
std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
|
||||
@@ -37,33 +37,33 @@ T parallel_accumulate(Iterator first, Iterator last, T init)
|
||||
std::vector<std::thread> threads(num_threads - 1);
|
||||
|
||||
Iterator block_start = first;
|
||||
for (unsigned long i = 0; i < (num_threads-1); ++i)
|
||||
{
|
||||
for (unsigned long i = 0; i < (num_threads - 1); ++i) {
|
||||
Iterator block_end = block_start;
|
||||
std::advance(block_end, block_size);
|
||||
threads[i] = std::thread(
|
||||
accumulate_block<Iterator,T>(),
|
||||
block_start,block_end,std::ref(results[i]));
|
||||
threads[i] = std::thread(accumulate_block<Iterator, T>(),
|
||||
block_start,
|
||||
block_end,
|
||||
std::ref(results[i]));
|
||||
block_start = block_end;
|
||||
}
|
||||
accumulate_block<std::vector<int>::iterator, int>()(
|
||||
block_start, last, results[num_threads - 1]);
|
||||
|
||||
std::for_each(threads.begin(),threads.end(),
|
||||
std::mem_fn(&std::thread::join));
|
||||
std::for_each(
|
||||
threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
|
||||
|
||||
return std::accumulate(results.begin(), results.end(), init);
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::vector<int> numbers(50);
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
numbers[i] = i;
|
||||
}
|
||||
int result = parallel_accumulate<std::vector<int>::iterator,int>
|
||||
(numbers.begin(),numbers.end(),0);
|
||||
int result = parallel_accumulate<std::vector<int>::iterator, int>(
|
||||
numbers.begin(), numbers.end(), 0);
|
||||
|
||||
std::cout << "result: " << result << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#include <climits>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <climits>
|
||||
|
||||
class hierarchical_mutex
|
||||
{
|
||||
class hierarchical_mutex {
|
||||
std::mutex internal_mutex;
|
||||
unsigned long const hierarchy_value;
|
||||
unsigned long previous_hierarchy_value;
|
||||
@@ -11,8 +10,7 @@ class hierarchical_mutex
|
||||
|
||||
void check_for_hierarchy_violation()
|
||||
{
|
||||
if(this_thread_hierarchy_value <= hierarchy_value)
|
||||
{
|
||||
if (this_thread_hierarchy_value <= hierarchy_value) {
|
||||
throw std::logic_error("mutex hierarchy violated");
|
||||
}
|
||||
}
|
||||
@@ -21,11 +19,12 @@ class hierarchical_mutex
|
||||
previous_hierarchy_value = this_thread_hierarchy_value;
|
||||
this_thread_hierarchy_value = hierarchy_value;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit hierarchical_mutex(unsigned long value):
|
||||
hierarchy_value(value),
|
||||
previous_hierarchy_value(0)
|
||||
{}
|
||||
explicit hierarchical_mutex(unsigned long value)
|
||||
: hierarchy_value(value), previous_hierarchy_value(0)
|
||||
{
|
||||
}
|
||||
void lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
@@ -46,12 +45,12 @@ public:
|
||||
return true;
|
||||
}
|
||||
};
|
||||
thread_local unsigned long
|
||||
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX);
|
||||
thread_local unsigned long hierarchical_mutex::this_thread_hierarchy_value(
|
||||
ULONG_MAX);
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
hierarchical_mutex m1(42);
|
||||
hierarchical_mutex m2(2000);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
#include <exception>
|
||||
#include <stack>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
struct empty_stack: std::exception
|
||||
{
|
||||
const char* what() const throw()
|
||||
{
|
||||
return "empty stack";
|
||||
}
|
||||
|
||||
struct empty_stack : std::exception {
|
||||
const char* what() const throw() { return "empty stack"; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_stack
|
||||
{
|
||||
class threadsafe_stack {
|
||||
private:
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
|
||||
public:
|
||||
threadsafe_stack() {}
|
||||
threadsafe_stack(const threadsafe_stack& other)
|
||||
@@ -35,7 +30,8 @@ public:
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
|
||||
data.pop();
|
||||
return res;
|
||||
@@ -43,7 +39,8 @@ public:
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
value = data.top();
|
||||
data.pop();
|
||||
}
|
||||
@@ -54,15 +51,14 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
threadsafe_stack<int> si;
|
||||
si.push(5);
|
||||
si.pop();
|
||||
if(!si.empty())
|
||||
{
|
||||
if (!si.empty()) {
|
||||
int x;
|
||||
si.pop(x);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
mutable std::mutex mut;
|
||||
std::queue<T> data_queue;
|
||||
@@ -76,7 +75,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// std::cout << "pushing " << i << std::endl;
|
||||
@@ -85,14 +85,16 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("poping %d\n", *q->wait_and_pop());
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread t1(push, &q);
|
||||
@@ -101,4 +103,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <list>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
|
||||
std::ostream&
|
||||
operator<<(std::ostream& ostr, const std::list<int>& list)
|
||||
{
|
||||
for (auto& i : list) {
|
||||
ostr << " " << i;
|
||||
@@ -15,54 +16,50 @@ std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::list<T> sequential_quick_sort(std::list<T> input)
|
||||
{
|
||||
if(input.empty())
|
||||
std::list<T>
|
||||
sequential_quick_sort(std::list<T> input)
|
||||
{
|
||||
if (input.empty()) {
|
||||
return input;
|
||||
}
|
||||
std::list<T> result;
|
||||
result.splice(result.begin(), input, input.begin());
|
||||
T const& pivot = *result.begin();
|
||||
auto divide_point=std::partition(input.begin(),input.end(),
|
||||
[&](T const& t){return t<pivot;});
|
||||
auto divide_point = std::partition(
|
||||
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
|
||||
std::list<T> lower_part;
|
||||
lower_part.splice(lower_part.end(),input,input.begin(),
|
||||
divide_point);
|
||||
auto new_lower(
|
||||
sequential_quick_sort(std::move(lower_part)));
|
||||
auto new_higher(
|
||||
sequential_quick_sort(std::move(input)));
|
||||
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
|
||||
auto new_lower(sequential_quick_sort(std::move(lower_part)));
|
||||
auto new_higher(sequential_quick_sort(std::move(input)));
|
||||
result.splice(result.end(), new_higher);
|
||||
result.splice(result.begin(), new_lower);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::list<T> parallel_quick_sort(std::list<T> input)
|
||||
{
|
||||
if(input.empty())
|
||||
std::list<T>
|
||||
parallel_quick_sort(std::list<T> input)
|
||||
{
|
||||
if (input.empty()) {
|
||||
return input;
|
||||
}
|
||||
std::list<T> result;
|
||||
result.splice(result.begin(), input, input.begin());
|
||||
T const& pivot = *result.begin();
|
||||
auto divide_point=std::partition(input.begin(),input.end(),
|
||||
[&](T const& t){return t<pivot;});
|
||||
auto divide_point = std::partition(
|
||||
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
|
||||
std::list<T> lower_part;
|
||||
lower_part.splice(lower_part.end(),input,input.begin(),
|
||||
divide_point);
|
||||
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
|
||||
std::future<std::list<T>> new_lower(
|
||||
std::async(¶llel_quick_sort<T>, std::move(lower_part)));
|
||||
auto new_higher(
|
||||
parallel_quick_sort(std::move(input)));
|
||||
auto new_higher(parallel_quick_sort(std::move(input)));
|
||||
result.splice(result.end(), new_higher);
|
||||
result.splice(result.begin(), new_lower.get());
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::list<int> list1 = {4, 2, 1, 5, 8, 9, 7};
|
||||
std::list<int> sorted1 = sequential_quick_sort(list1);
|
||||
@@ -71,4 +68,3 @@ int main()
|
||||
std::cout << "sorted2: " << sorted2 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
51
ch6/list.cpp
51
ch6/list.cpp
@@ -1,31 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class list
|
||||
{
|
||||
struct node
|
||||
{
|
||||
class list {
|
||||
struct node {
|
||||
std::mutex m;
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
|
||||
node()
|
||||
: next()
|
||||
{}
|
||||
node() : next() {}
|
||||
|
||||
node(T const& value)
|
||||
: data(std::make_shared<T>(value))
|
||||
{}
|
||||
node(T const& value) : data(std::make_shared<T>(value)) {}
|
||||
};
|
||||
|
||||
node head;
|
||||
|
||||
public:
|
||||
list()
|
||||
{}
|
||||
list() {}
|
||||
|
||||
~list()
|
||||
{
|
||||
@@ -48,8 +41,7 @@ class list
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while (node* const next = current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
lk.unlock();
|
||||
f(*next->data);
|
||||
@@ -63,12 +55,10 @@ class list
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while (node* const next = current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
lk.unlock();
|
||||
if (p(*next->data))
|
||||
{
|
||||
if (p(*next->data)) {
|
||||
return next->data;
|
||||
}
|
||||
current = next;
|
||||
@@ -82,17 +72,14 @@ class list
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while (node* const next = current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
if (p(*next->data))
|
||||
{
|
||||
if (p(*next->data)) {
|
||||
std::unique_ptr<node> old_next = std::move(current->next);
|
||||
current->next = std::move(next->next);
|
||||
next_lk.unlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
lk.unlock();
|
||||
current = next;
|
||||
lk = std::move(next_lk);
|
||||
@@ -101,7 +88,8 @@ class list
|
||||
}
|
||||
};
|
||||
|
||||
void push(list<int>* l)
|
||||
void
|
||||
push(list<int>* l)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -109,12 +97,14 @@ void push(list<int>* l)
|
||||
}
|
||||
}
|
||||
|
||||
void visit(int i)
|
||||
void
|
||||
visit(int i)
|
||||
{
|
||||
printf("visit %d\n", i);
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
list<int> l;
|
||||
std::thread t(push, &l);
|
||||
@@ -123,4 +113,3 @@ int main()
|
||||
t.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
template <typename Key, typename Value, typename Hash = std::hash<Key>>
|
||||
class lookup_table
|
||||
{
|
||||
class lookup_table {
|
||||
private:
|
||||
class bucket_type
|
||||
{
|
||||
class bucket_type {
|
||||
private:
|
||||
typedef std::pair<Key, Value> bucket_value;
|
||||
typedef std::list<bucket_value> bucket_data;
|
||||
@@ -22,9 +20,10 @@ class lookup_table
|
||||
|
||||
bucket_iterator find_entry_for(Key const& key) const
|
||||
{
|
||||
return std::find_if(data.begin(), data.end(),
|
||||
[&](bucket_value const& item)
|
||||
{return item.first == key;});
|
||||
return std::find_if(
|
||||
data.begin(), data.end(), [&](bucket_value const& item) {
|
||||
return item.first == key;
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -32,8 +31,7 @@ class lookup_table
|
||||
{
|
||||
boost::shared_lock<boost::shared_mutex> lock(mutex);
|
||||
bucket_iterator const found_entry = find_entry_for(key);
|
||||
return (found_entry == data.end()) ?
|
||||
default_value : found_entry->second;
|
||||
return (found_entry == data.end()) ? default_value : found_entry->second;
|
||||
}
|
||||
|
||||
void add_or_update_mapping(Key const& key, Value const& value)
|
||||
@@ -73,11 +71,9 @@ class lookup_table
|
||||
typedef Hash hash_type;
|
||||
|
||||
lookup_table(unsigned num_buckets = 19, const Hash& hasher_ = Hash())
|
||||
: buckets(num_buckets)
|
||||
, hasher(hasher_)
|
||||
{
|
||||
for (unsigned i = 0; i < num_buckets; ++i)
|
||||
: buckets(num_buckets), hasher(hasher_)
|
||||
{
|
||||
for (unsigned i = 0; i < num_buckets; ++i) {
|
||||
buckets[i].reset(new bucket_type);
|
||||
}
|
||||
}
|
||||
@@ -85,8 +81,7 @@ class lookup_table
|
||||
lookup_table(const lookup_table& other) = delete;
|
||||
lookup_table& operator=(const lookup_table& other) = delete;
|
||||
|
||||
Value value_for(Key const& key,
|
||||
Value const& default_value = Value()) const
|
||||
Value value_for(Key const& key, Value const& default_value = Value()) const
|
||||
{
|
||||
return get_bucket(key).value_for(key, default_value);
|
||||
}
|
||||
@@ -96,26 +91,20 @@ class lookup_table
|
||||
get_bucket(key).add_or_update_mapping(key, value);
|
||||
}
|
||||
|
||||
void remove_mapping(const Key& key)
|
||||
{
|
||||
get_bucket(key).remove_mapping(key);
|
||||
}
|
||||
void remove_mapping(const Key& key) { get_bucket(key).remove_mapping(key); }
|
||||
|
||||
std::map<Key, Value> get_map() const
|
||||
{
|
||||
std::vector<boost::unique_lock<boost::shared_mutex>> locks;
|
||||
for (unsigned i = 0; i < buckets.size(); ++i)
|
||||
{
|
||||
locks.push_back(boost::unique_lock<boost::shared_mutex>(
|
||||
buckets[i].mutex));
|
||||
for (unsigned i = 0; i < buckets.size(); ++i) {
|
||||
locks.push_back(
|
||||
boost::unique_lock<boost::shared_mutex>(buckets[i].mutex));
|
||||
}
|
||||
std::map<Key, Value> res;
|
||||
for (unsigned i = 0; i < buckets.size(); ++i)
|
||||
{
|
||||
for (unsigned i = 0; i < buckets.size(); ++i) {
|
||||
for (typename bucket_type::bucket_iterator it = buckets[i].data.begin();
|
||||
it != buckets[i].data.end();
|
||||
++it)
|
||||
{
|
||||
++it) {
|
||||
res.insert(*it);
|
||||
}
|
||||
}
|
||||
@@ -123,11 +112,11 @@ class lookup_table
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
lookup_table<int, std::string> t;
|
||||
t.add_or_update_mapping(1, "hello");
|
||||
std::cout << "value for 1 is " << t.value_for(1) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
mutable std::mutex mut;
|
||||
std::queue<std::shared_ptr<T>> data_queue;
|
||||
@@ -84,7 +83,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// std::cout << "pushing " << i << std::endl;
|
||||
@@ -93,14 +93,16 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("poping %d\n", *q->wait_and_pop());
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread t1(push, &q);
|
||||
@@ -109,4 +111,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
};
|
||||
@@ -37,8 +35,7 @@ class queue
|
||||
}
|
||||
|
||||
public:
|
||||
queue():head(new node),tail(head.get())
|
||||
{}
|
||||
queue() : head(new node), tail(head.get()) {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
@@ -51,8 +48,7 @@ class queue
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> new_data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));
|
||||
std::unique_ptr<node> p(new node);
|
||||
node* const new_tail = p.get();
|
||||
std::lock_guard<std::mutex> tail_lock(tail_mutex);
|
||||
@@ -62,7 +58,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -70,7 +67,8 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
int i = 0;
|
||||
while (true) {
|
||||
@@ -78,11 +76,13 @@ void pop(queue<int>* q)
|
||||
printf("poping %d\n", *p);
|
||||
++i;
|
||||
}
|
||||
if (i == 10) break;
|
||||
if (i == 10)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread th1(push, &q);
|
||||
@@ -91,4 +91,3 @@ int main()
|
||||
th2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
};
|
||||
@@ -74,8 +72,7 @@ class queue
|
||||
}
|
||||
|
||||
public:
|
||||
queue():head(new node),tail(head.get())
|
||||
{}
|
||||
queue() : head(new node), tail(head.get()) {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
@@ -105,8 +102,7 @@ class queue
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> new_data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));
|
||||
std::unique_ptr<node> p(new node);
|
||||
{
|
||||
std::lock_guard<std::mutex> tail_lock(tail_mutex);
|
||||
@@ -125,8 +121,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -134,7 +130,8 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
int i = 0;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
@@ -142,7 +139,8 @@ void pop(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread th1(push, &q);
|
||||
@@ -151,4 +149,3 @@ int main()
|
||||
th2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
private:
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
@@ -32,7 +31,8 @@ class stack
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (data.empty()) throw std::underflow_error("empty stack");
|
||||
if (data.empty())
|
||||
throw std::underflow_error("empty stack");
|
||||
std::shared_ptr<T> const res(new T(data.top()));
|
||||
data.pop();
|
||||
return res;
|
||||
@@ -41,7 +41,8 @@ class stack
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (data.empty()) throw std::underflow_error("empty stack");
|
||||
if (data.empty())
|
||||
throw std::underflow_error("empty stack");
|
||||
value = data.top();
|
||||
data.pop();
|
||||
}
|
||||
@@ -53,10 +54,10 @@ class stack
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
stack<int> s;
|
||||
s.push(1);
|
||||
std::cout << *s.pop() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <cstdio>
|
||||
|
||||
unsigned const max_hazard_pointers = 100;
|
||||
struct hazard_pointer
|
||||
{
|
||||
struct hazard_pointer {
|
||||
std::atomic<std::thread::id> id;
|
||||
std::atomic<void*> pointer;
|
||||
};
|
||||
hazard_pointer hazard_pointers[max_hazard_pointers];
|
||||
|
||||
class hp_owner
|
||||
{
|
||||
class hp_owner {
|
||||
hazard_pointer* hp;
|
||||
|
||||
public:
|
||||
hp_owner(hp_owner const&) = delete;
|
||||
hp_owner operator=(hp_owner const&) = delete;
|
||||
hp_owner()
|
||||
: hp(nullptr)
|
||||
hp_owner() : hp(nullptr)
|
||||
{
|
||||
for (unsigned i = 0; i < max_hazard_pointers; ++i) {
|
||||
std::thread::id old_id;
|
||||
@@ -41,10 +38,7 @@ class hp_owner
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<void*>& get_pointer()
|
||||
{
|
||||
return hp->pointer;
|
||||
}
|
||||
std::atomic<void*>& get_pointer() { return hp->pointer; }
|
||||
|
||||
~hp_owner()
|
||||
{
|
||||
@@ -57,7 +51,3 @@ class hp_owner
|
||||
// hp->id.store(std::thread::id());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
@@ -9,12 +9,10 @@
|
||||
// mpmc queue
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node;
|
||||
struct counted_node_ptr
|
||||
{
|
||||
struct counted_node_ptr {
|
||||
int external_count;
|
||||
node* ptr;
|
||||
};
|
||||
@@ -22,16 +20,14 @@ class queue
|
||||
std::atomic<counted_node_ptr> head;
|
||||
std::atomic<counted_node_ptr> tail;
|
||||
|
||||
struct node_counter
|
||||
{
|
||||
struct node_counter {
|
||||
unsigned internal_count : 30;
|
||||
unsigned external_counters : 2;
|
||||
// you need only 2 bits because there are at most two such
|
||||
// counters (next and tail)
|
||||
};
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::atomic<T*> data;
|
||||
std::atomic<node_counter> count;
|
||||
std::atomic<counted_node_ptr> next;
|
||||
@@ -54,24 +50,19 @@ class queue
|
||||
|
||||
void release_ref()
|
||||
{
|
||||
node_counter old_counter=
|
||||
count.load(std::memory_order_relaxed);
|
||||
node_counter old_counter = count.load(std::memory_order_relaxed);
|
||||
node_counter new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
--new_counter.internal_count;
|
||||
}
|
||||
while(!count.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
if(!new_counter.internal_count &&
|
||||
!new_counter.external_counters)
|
||||
{
|
||||
} while (!count.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
if (!new_counter.internal_count && !new_counter.external_counters) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void set_new_tail(counted_node_ptr& old_tail,
|
||||
@@ -79,26 +70,25 @@ class queue
|
||||
{
|
||||
node* const current_tail_ptr = old_tail.ptr;
|
||||
while (!tail.compare_exchange_weak(old_tail, new_tail) &&
|
||||
old_tail.ptr == current_tail_ptr);
|
||||
old_tail.ptr == current_tail_ptr)
|
||||
;
|
||||
if (old_tail.ptr == current_tail_ptr)
|
||||
free_external_counter(old_tail);
|
||||
else
|
||||
current_tail_ptr->release_ref();
|
||||
}
|
||||
|
||||
static void increase_external_count(
|
||||
std::atomic<counted_node_ptr>& counter,
|
||||
static void increase_external_count(std::atomic<counted_node_ptr>& counter,
|
||||
counted_node_ptr& old_counter)
|
||||
{
|
||||
counted_node_ptr new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
++new_counter.external_count;
|
||||
}
|
||||
while(!counter.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
} while (!counter.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
old_counter.external_count = new_counter.external_count;
|
||||
}
|
||||
|
||||
@@ -106,30 +96,23 @@ class queue
|
||||
{
|
||||
node* const ptr = old_node_ptr.ptr;
|
||||
int const count_increase = old_node_ptr.external_count - 2;
|
||||
node_counter old_counter =
|
||||
ptr->count.load(std::memory_order_relaxed);
|
||||
node_counter old_counter = ptr->count.load(std::memory_order_relaxed);
|
||||
node_counter new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
--new_counter.external_counters;
|
||||
new_counter.internal_count += count_increase;
|
||||
}
|
||||
while(!ptr->count.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
if(!new_counter.internal_count &&
|
||||
!new_counter.external_counters)
|
||||
{
|
||||
} while (!ptr->count.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
if (!new_counter.internal_count && !new_counter.external_counters) {
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
queue()
|
||||
: head()
|
||||
, tail()
|
||||
{}
|
||||
queue() : head(), tail() {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
@@ -151,17 +134,13 @@ class queue
|
||||
new_next.ptr = new node;
|
||||
new_next.external_count = 1;
|
||||
counted_node_ptr old_tail = tail.load();
|
||||
for(;;)
|
||||
{
|
||||
for (;;) {
|
||||
increase_external_count(tail, old_tail);
|
||||
T* old_data = nullptr;
|
||||
if(old_tail.ptr->data.compare_exchange_strong(
|
||||
old_data,new_data.get()))
|
||||
{
|
||||
if (old_tail.ptr->data.compare_exchange_strong(old_data,
|
||||
new_data.get())) {
|
||||
counted_node_ptr old_next = {0};
|
||||
if(!old_tail.ptr->next.compare_exchange_strong(
|
||||
old_next,new_next))
|
||||
{
|
||||
if (!old_tail.ptr->next.compare_exchange_strong(old_next, new_next)) {
|
||||
delete new_next.ptr;
|
||||
new_next = old_next;
|
||||
}
|
||||
@@ -169,12 +148,9 @@ class queue
|
||||
new_data.release();
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
counted_node_ptr old_next = {0};
|
||||
if(old_tail.ptr->next.compare_exchange_strong(
|
||||
old_next,new_next))
|
||||
{
|
||||
if (old_tail.ptr->next.compare_exchange_strong(old_next, new_next)) {
|
||||
old_next = new_next;
|
||||
new_next.ptr = new node;
|
||||
}
|
||||
@@ -183,7 +159,8 @@ class queue
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<T> pop() {
|
||||
std::unique_ptr<T> pop()
|
||||
{
|
||||
counted_node_ptr old_head = head.load(std::memory_order_relaxed);
|
||||
|
||||
for (;;) {
|
||||
@@ -207,7 +184,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -215,7 +193,8 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
@@ -227,7 +206,8 @@ void pop(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread t1(push, &q);
|
||||
@@ -236,5 +216,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
node* next;
|
||||
|
||||
node()
|
||||
: next(nullptr)
|
||||
{}
|
||||
node() : next(nullptr) {}
|
||||
};
|
||||
|
||||
std::atomic<node*> head;
|
||||
@@ -30,11 +26,9 @@ class queue
|
||||
head.store(old_head->next);
|
||||
return old_head;
|
||||
}
|
||||
|
||||
public:
|
||||
queue()
|
||||
: head(new node)
|
||||
, tail(head.load())
|
||||
{}
|
||||
queue() : head(new node), tail(head.load()) {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
@@ -70,7 +64,8 @@ class queue
|
||||
}
|
||||
};
|
||||
|
||||
void push(queue<int>* q)
|
||||
void
|
||||
push(queue<int>* q)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -78,7 +73,8 @@ void push(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(queue<int>* q)
|
||||
void
|
||||
pop(queue<int>* q)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
@@ -90,7 +86,8 @@ void pop(queue<int>* q)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
queue<int> q;
|
||||
std::thread t1(push, &q);
|
||||
@@ -99,4 +96,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,31 +11,26 @@
|
||||
// it can reclaimed. By far the easiest such mechanism to reason about is the
|
||||
// use of hazard ponters.
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
node* next;
|
||||
|
||||
node(const T& data_)
|
||||
: data(std::make_shared<T>(data_))
|
||||
{}
|
||||
node(const T& data_) : data(std::make_shared<T>(data_)) {}
|
||||
};
|
||||
|
||||
std::atomic<node*> head;
|
||||
std::atomic<unsigned> threads_in_pop;
|
||||
std::atomic<node*> to_be_deleted;
|
||||
|
||||
|
||||
static void delete_nodes(node* nodes)
|
||||
{
|
||||
while (nodes) {
|
||||
@@ -98,9 +93,8 @@ class stack
|
||||
// store the first node in the chain as the new to_be_deleted pointer
|
||||
// have to use compare_exchange_weak in a loop here in order to ensure
|
||||
// that you don't leak any nodes that have been added by another thread
|
||||
while (!to_be_deleted.compare_exchange_weak(
|
||||
last->next,first));
|
||||
|
||||
while (!to_be_deleted.compare_exchange_weak(last->next, first))
|
||||
;
|
||||
}
|
||||
|
||||
void chain_pending_node(node* n)
|
||||
@@ -112,26 +106,23 @@ class stack
|
||||
}
|
||||
|
||||
public:
|
||||
stack()
|
||||
: head(nullptr)
|
||||
, threads_in_pop(0)
|
||||
, to_be_deleted(nullptr)
|
||||
{}
|
||||
stack() : head(nullptr), threads_in_pop(0), to_be_deleted(nullptr) {}
|
||||
|
||||
void push(const T& data)
|
||||
{
|
||||
node* const new_node = new node(data);
|
||||
new_node->next = head.load();
|
||||
// loop to gurantee that last->next is correct
|
||||
while (!head.compare_exchange_weak(new_node->next, new_node));
|
||||
while (!head.compare_exchange_weak(new_node->next, new_node))
|
||||
;
|
||||
}
|
||||
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
++threads_in_pop; // increase counter before doing anything else
|
||||
node* old_head = head.load();
|
||||
while (old_head &&
|
||||
!head.compare_exchange_weak(old_head, old_head->next));
|
||||
while (old_head && !head.compare_exchange_weak(old_head, old_head->next))
|
||||
;
|
||||
std::shared_ptr<T> res;
|
||||
if (old_head) {
|
||||
res.swap(old_head->data); // extract data from node rather than
|
||||
@@ -142,7 +133,8 @@ class stack
|
||||
}
|
||||
};
|
||||
|
||||
void push(stack<int>* s)
|
||||
void
|
||||
push(stack<int>* s)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -150,7 +142,8 @@ void push(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(stack<int>* s)
|
||||
void
|
||||
pop(stack<int>* s)
|
||||
{
|
||||
int count = 0;
|
||||
std::shared_ptr<int> e;
|
||||
@@ -162,7 +155,8 @@ void pop(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
stack<int> s;
|
||||
std::thread t1(push, &s);
|
||||
@@ -171,4 +165,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include "hazard_pointer.h"
|
||||
|
||||
std::atomic<void*>& get_hazard_pointer_for_current_thread()
|
||||
std::atomic<void*>&
|
||||
get_hazard_pointer_for_current_thread()
|
||||
{
|
||||
// The first time each thread calls this function, a new instance of
|
||||
// hp_owner is created. The constructor for this new instance then
|
||||
@@ -17,12 +18,14 @@ std::atomic<void*>& get_hazard_pointer_for_current_thread()
|
||||
// accesses are much faster because the pointer is cached, so the table
|
||||
// doesn't have to be scanned again.
|
||||
|
||||
printf("get harzard pointer for current thread, thread id: %d\n", std::this_thread::get_id());
|
||||
printf("get harzard pointer for current thread, thread id: %d\n",
|
||||
std::this_thread::get_id());
|
||||
thread_local static hp_owner hazard;
|
||||
return hazard.get_pointer();
|
||||
}
|
||||
|
||||
bool outstanding_hazard_pointers_for(void *p)
|
||||
bool
|
||||
outstanding_hazard_pointers_for(void* p)
|
||||
{
|
||||
for (unsigned i = 0; i < max_hazard_pointers; ++i) {
|
||||
if (hazard_pointers[i].pointer.load() == p) {
|
||||
@@ -33,44 +36,43 @@ bool outstanding_hazard_pointers_for(void *p)
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void do_delete(void* p)
|
||||
void
|
||||
do_delete(void* p)
|
||||
{
|
||||
delete static_cast<T*>(p);
|
||||
}
|
||||
|
||||
struct data_to_reclaim
|
||||
{
|
||||
struct data_to_reclaim {
|
||||
void* data;
|
||||
std::function<void(void*)> deleter;
|
||||
data_to_reclaim* next;
|
||||
|
||||
template <typename T>
|
||||
data_to_reclaim(T* p)
|
||||
: data(p)
|
||||
, deleter(&do_delete<T>)
|
||||
, next(0)
|
||||
{}
|
||||
|
||||
~data_to_reclaim()
|
||||
data_to_reclaim(T* p) : data(p), deleter(&do_delete<T>), next(0)
|
||||
{
|
||||
deleter(data);
|
||||
}
|
||||
|
||||
~data_to_reclaim() { deleter(data); }
|
||||
};
|
||||
|
||||
std::atomic<data_to_reclaim*> nodes_to_reclaim;
|
||||
void add_to_reclaim_list(data_to_reclaim* node)
|
||||
void
|
||||
add_to_reclaim_list(data_to_reclaim* node)
|
||||
{
|
||||
node->next = nodes_to_reclaim.load();
|
||||
while (!nodes_to_reclaim.compare_exchange_weak(node->next, node));
|
||||
while (!nodes_to_reclaim.compare_exchange_weak(node->next, node))
|
||||
;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void reclaim_later(T* data)
|
||||
void
|
||||
reclaim_later(T* data)
|
||||
{
|
||||
add_to_reclaim_list(new data_to_reclaim(data));
|
||||
}
|
||||
|
||||
void delete_nodes_with_no_hazards()
|
||||
void
|
||||
delete_nodes_with_no_hazards()
|
||||
{
|
||||
// first claims the entire list of nodes to be reclaimed;
|
||||
// ensures that this is the only thread trying to reclaim
|
||||
@@ -98,31 +100,26 @@ void delete_nodes_with_no_hazards()
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
node* next;
|
||||
|
||||
node(const T& data_)
|
||||
: data(std::make_shared<T>(data_))
|
||||
{}
|
||||
node(const T& data_) : data(std::make_shared<T>(data_)) {}
|
||||
};
|
||||
|
||||
std::atomic<node*> head;
|
||||
|
||||
public:
|
||||
stack()
|
||||
: head(nullptr)
|
||||
{}
|
||||
stack() : head(nullptr) {}
|
||||
|
||||
void push(const T& data)
|
||||
{
|
||||
node* const new_node = new node(data);
|
||||
new_node->next = head.load();
|
||||
while (!head.compare_exchange_weak(new_node->next, new_node));
|
||||
while (!head.compare_exchange_weak(new_node->next, new_node))
|
||||
;
|
||||
}
|
||||
|
||||
std::shared_ptr<T> pop()
|
||||
@@ -136,8 +133,7 @@ class stack
|
||||
hp.store(old_head);
|
||||
old_head = head.load();
|
||||
} while (old_head != temp);
|
||||
}
|
||||
while (old_head &&
|
||||
} while (old_head &&
|
||||
!head.compare_exchange_strong(old_head, old_head->next));
|
||||
hp.store(nullptr); // clear hazard pointer once you're finished
|
||||
std::shared_ptr<T> res;
|
||||
@@ -157,7 +153,8 @@ class stack
|
||||
}
|
||||
};
|
||||
|
||||
void push(stack<int>* s)
|
||||
void
|
||||
push(stack<int>* s)
|
||||
{
|
||||
printf("starting push\n");
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
@@ -166,7 +163,8 @@ void push(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(stack<int>* s)
|
||||
void
|
||||
pop(stack<int>* s)
|
||||
{
|
||||
printf("starting pop\n");
|
||||
int count = 0;
|
||||
@@ -179,7 +177,8 @@ void pop(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
printf("creating stack\n");
|
||||
stack<int> s;
|
||||
@@ -190,4 +189,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
private:
|
||||
struct node;
|
||||
|
||||
@@ -22,22 +21,17 @@ class stack
|
||||
// count minus one and external counter is discarded. Once the internal
|
||||
// count is equal to zero, there are no outstanding references to the node
|
||||
// and it can be safely deleted.
|
||||
struct counted_node_ptr
|
||||
{
|
||||
struct counted_node_ptr {
|
||||
int external_count;
|
||||
node* ptr;
|
||||
};
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::atomic_int internal_count;
|
||||
counted_node_ptr next;
|
||||
|
||||
node(const T& data_)
|
||||
: data(new T(data_))
|
||||
, internal_count(0)
|
||||
{}
|
||||
node(const T& data_) : data(new T(data_)), internal_count(0) {}
|
||||
};
|
||||
|
||||
std::atomic<counted_node_ptr> head;
|
||||
@@ -54,7 +48,6 @@ class stack
|
||||
// the count inside the spare bits of the pointer to fit it all back in a
|
||||
// single machine word.
|
||||
|
||||
|
||||
// once you've loaded the value of head, you must first increaes the count
|
||||
// of external references to the head node to indicate that you're
|
||||
// referencing it and to ensure that it's safe to deference it.
|
||||
@@ -64,12 +57,10 @@ class stack
|
||||
{
|
||||
counted_node_ptr new_counter;
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
++new_counter.external_count;
|
||||
}
|
||||
while (!head.compare_exchange_strong(old_counter, new_counter));
|
||||
} while (!head.compare_exchange_strong(old_counter, new_counter));
|
||||
|
||||
old_counter.external_count = new_counter.external_count;
|
||||
}
|
||||
@@ -77,7 +68,8 @@ class stack
|
||||
public:
|
||||
~stack()
|
||||
{
|
||||
while (pop());
|
||||
while (pop())
|
||||
;
|
||||
}
|
||||
|
||||
void push(const T& data)
|
||||
@@ -89,26 +81,24 @@ class stack
|
||||
// external reference to the node (the head pointer itself).
|
||||
new_node.external_count = 1;
|
||||
new_node.ptr->next = head.load();
|
||||
while (!head.compare_exchange_weak(new_node.ptr->next, new_node));
|
||||
while (!head.compare_exchange_weak(new_node.ptr->next, new_node))
|
||||
;
|
||||
}
|
||||
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
counted_node_ptr old_head = head.load();
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
increase_head_count(old_head);
|
||||
node* ptr = old_head.ptr;
|
||||
// if the pointer is a null pointer, you're at the end of list:
|
||||
// no more entires
|
||||
if (!ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
|
||||
// if the pointer isn't a null pointer, try to remove the node
|
||||
if (head.compare_exchange_strong(old_head,ptr->next))
|
||||
{
|
||||
if (head.compare_exchange_strong(old_head, ptr->next)) {
|
||||
// you've taken the ownership of the node and can swap out
|
||||
// the data in prepration for returning it.
|
||||
std::shared_ptr<T> res;
|
||||
@@ -123,9 +113,7 @@ class stack
|
||||
// if the reference count is now zero, the previous value
|
||||
// (which is what fetch_add returns) was the negative of what
|
||||
// you just added, in which case you can delete the node.
|
||||
if (ptr->internal_count.fetch_add(count_increase) ==
|
||||
-count_increase)
|
||||
{
|
||||
if (ptr->internal_count.fetch_add(count_increase) == -count_increase) {
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
@@ -142,15 +130,15 @@ class stack
|
||||
// from the stack), the internal reference count will be 1, so
|
||||
// subtracing 1 will set the count to zero. In this case, you can
|
||||
// delete the node here before you loop.
|
||||
else if (ptr->internal_count.fetch_add(-1) == 1)
|
||||
{
|
||||
else if (ptr->internal_count.fetch_add(-1) == 1) {
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void push(stack<int>* s)
|
||||
void
|
||||
push(stack<int>* s)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -158,7 +146,8 @@ void push(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(stack<int>* s)
|
||||
void
|
||||
pop(stack<int>* s)
|
||||
{
|
||||
int count = 0;
|
||||
std::shared_ptr<int> e;
|
||||
@@ -170,7 +159,8 @@ void pop(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
stack<int> s;
|
||||
std::thread t1(push, &s);
|
||||
@@ -179,5 +169,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,41 +1,40 @@
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
template <typename T>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::shared_ptr<node> next;
|
||||
|
||||
node(T const& data_)
|
||||
: data(std::make_shared<T>(data_))
|
||||
{}
|
||||
node(T const& data_) : data(std::make_shared<T>(data_)) {}
|
||||
};
|
||||
|
||||
std::shared_ptr<node> head;
|
||||
|
||||
public:
|
||||
void push(T const& data)
|
||||
{
|
||||
std::shared_ptr<node> const new_node = std::make_shared<node>(data);
|
||||
new_node->next = std::atomic_load(head);
|
||||
while (!std::atomic_compare_exchange_weak(&head,
|
||||
&new_node->next, new_node));
|
||||
while (!std::atomic_compare_exchange_weak(&head, &new_node->next, new_node))
|
||||
;
|
||||
}
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::shared_ptr<node> old_head = std::atomic_load(head);
|
||||
while (old_head && !std::atomic_compare_exchange_weak(&head,
|
||||
&old_head, old_head->next));
|
||||
while (old_head &&
|
||||
!std::atomic_compare_exchange_weak(&head, &old_head, old_head->next))
|
||||
;
|
||||
return old_head ? old_head->data : std::shared_ptr<T>();
|
||||
}
|
||||
};
|
||||
|
||||
void push(stack<int>* s)
|
||||
void
|
||||
push(stack<int>* s)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
printf("pushing %d\n", i);
|
||||
@@ -43,7 +42,8 @@ void push(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
void pop(stack<int>* s)
|
||||
void
|
||||
pop(stack<int>* s)
|
||||
{
|
||||
int count = 0;
|
||||
std::shared_ptr<int> e;
|
||||
@@ -55,7 +55,8 @@ void pop(stack<int>* s)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::shared_ptr<int> sp = std::make_shared<int>(1);
|
||||
// printf("std::atomic_is_lock_free(std::shared_ptr): %d\n",
|
||||
@@ -67,4 +68,3 @@ int main()
|
||||
t2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
void hello()
|
||||
void
|
||||
hello()
|
||||
{
|
||||
std::cout << "Hello Concurrent World\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t(hello);
|
||||
t.join();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
void test_concurrent_push_and_pop_on_empty_queue()
|
||||
void
|
||||
test_concurrent_push_and_pop_on_empty_queue()
|
||||
{
|
||||
threadsafe_queue<int> q;
|
||||
|
||||
@@ -8,24 +9,17 @@ void test_concurrent_push_and_pop_on_empty_queue()
|
||||
std::future<void> push_done;
|
||||
std::future<int> pop_done;
|
||||
|
||||
try
|
||||
{
|
||||
push_done=std::async(std::launch::async,
|
||||
[&q,ready,&push_ready]()
|
||||
{
|
||||
try {
|
||||
push_done = std::async(std::launch::async, [&q, ready, &push_ready]() {
|
||||
push_ready.set_value();
|
||||
ready.wait();
|
||||
q.push(42);
|
||||
}
|
||||
);
|
||||
pop_done=std::async(std::launch::async,
|
||||
[&q,ready,&pop_ready]()
|
||||
{
|
||||
});
|
||||
pop_done = std::async(std::launch::async, [&q, ready, &pop_ready]() {
|
||||
pop_ready.set_value();
|
||||
ready.wait();
|
||||
return q.pop();
|
||||
}
|
||||
);
|
||||
});
|
||||
push_ready.get_future().wait();
|
||||
pop_ready.get_future().wait();
|
||||
go.set_value();
|
||||
@@ -34,8 +28,7 @@ void test_concurrent_push_and_pop_on_empty_queue()
|
||||
assert(pop_done.get() == 42);
|
||||
assert(q.empty());
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
catch (...) {
|
||||
go.set_value();
|
||||
throw;
|
||||
}
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
#include <thread>
|
||||
|
||||
void do_something(int& i)
|
||||
void
|
||||
do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
struct func {
|
||||
int& i;
|
||||
|
||||
func(int& i_) : i(i_) {}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
for (unsigned j = 0; j < 1000000; ++j) {
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void oops()
|
||||
void
|
||||
oops()
|
||||
{
|
||||
int some_local_state = 0;
|
||||
func my_func(some_local_state);
|
||||
@@ -29,7 +28,8 @@ void oops()
|
||||
my_thread.detach();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
oops();
|
||||
}
|
||||
|
||||
@@ -1,46 +1,47 @@
|
||||
#include <thread>
|
||||
|
||||
void do_something(int& i)
|
||||
void
|
||||
do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
struct func {
|
||||
int& i;
|
||||
|
||||
func(int& i_) : i(i_) {}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
for (unsigned j = 0; j < 1000000; ++j) {
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void do_something_in_current_thread()
|
||||
{}
|
||||
void
|
||||
do_something_in_current_thread()
|
||||
{
|
||||
}
|
||||
|
||||
void f()
|
||||
void
|
||||
f()
|
||||
{
|
||||
int some_local_state = 0;
|
||||
func my_func(some_local_state);
|
||||
std::thread t(my_func);
|
||||
try
|
||||
{
|
||||
try {
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
catch (...) {
|
||||
t.join();
|
||||
throw;
|
||||
}
|
||||
t.join();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include <thread>
|
||||
|
||||
class thread_guard
|
||||
{
|
||||
class thread_guard {
|
||||
std::thread& t;
|
||||
|
||||
public:
|
||||
explicit thread_guard(std::thread& t_):
|
||||
t(t_)
|
||||
{}
|
||||
explicit thread_guard(std::thread& t_) : t(t_) {}
|
||||
~thread_guard()
|
||||
{
|
||||
if(t.joinable())
|
||||
{
|
||||
if (t.joinable()) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
@@ -18,31 +15,32 @@ public:
|
||||
thread_guard& operator=(thread_guard const&) = delete;
|
||||
};
|
||||
|
||||
void do_something(int& i)
|
||||
void
|
||||
do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
struct func {
|
||||
int& i;
|
||||
|
||||
func(int& i_) : i(i_) {}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
for (unsigned j = 0; j < 1000000; ++j) {
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void do_something_in_current_thread()
|
||||
{}
|
||||
void
|
||||
do_something_in_current_thread()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void f()
|
||||
void
|
||||
f()
|
||||
{
|
||||
int some_local_state;
|
||||
func my_func(some_local_state);
|
||||
@@ -52,7 +50,8 @@ void f()
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
@@ -1,61 +1,61 @@
|
||||
#include <thread>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
void open_document_and_display_gui(std::string const& filename)
|
||||
{}
|
||||
void
|
||||
open_document_and_display_gui(std::string const& filename)
|
||||
{
|
||||
}
|
||||
|
||||
bool done_editing()
|
||||
bool
|
||||
done_editing()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
enum command_type{
|
||||
open_new_document
|
||||
};
|
||||
enum command_type { open_new_document };
|
||||
|
||||
|
||||
struct user_command
|
||||
{
|
||||
struct user_command {
|
||||
command_type type;
|
||||
|
||||
user_command():
|
||||
type(open_new_document)
|
||||
{}
|
||||
user_command() : type(open_new_document) {}
|
||||
};
|
||||
|
||||
user_command get_user_input()
|
||||
user_command
|
||||
get_user_input()
|
||||
{
|
||||
return user_command();
|
||||
}
|
||||
|
||||
std::string get_filename_from_user()
|
||||
std::string
|
||||
get_filename_from_user()
|
||||
{
|
||||
return "foo.doc";
|
||||
}
|
||||
|
||||
void process_user_input(user_command const& cmd)
|
||||
{}
|
||||
void
|
||||
process_user_input(user_command const& cmd)
|
||||
{
|
||||
}
|
||||
|
||||
void edit_document(std::string const& filename)
|
||||
void
|
||||
edit_document(std::string const& filename)
|
||||
{
|
||||
open_document_and_display_gui(filename);
|
||||
while(!done_editing())
|
||||
{
|
||||
while (!done_editing()) {
|
||||
user_command cmd = get_user_input();
|
||||
if(cmd.type==open_new_document)
|
||||
{
|
||||
if (cmd.type == open_new_document) {
|
||||
std::string const new_name = get_filename_from_user();
|
||||
std::thread t(edit_document, new_name);
|
||||
t.detach();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
process_user_input(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
edit_document("bar.doc");
|
||||
}
|
||||
|
||||
@@ -1,24 +1,31 @@
|
||||
#include <thread>
|
||||
|
||||
void some_function()
|
||||
{}
|
||||
void
|
||||
some_function()
|
||||
{
|
||||
}
|
||||
|
||||
void some_other_function(int)
|
||||
{}
|
||||
void
|
||||
some_other_function(int)
|
||||
{
|
||||
}
|
||||
|
||||
std::thread f()
|
||||
std::thread
|
||||
f()
|
||||
{
|
||||
void some_function();
|
||||
return std::thread(some_function);
|
||||
}
|
||||
std::thread g()
|
||||
std::thread
|
||||
g()
|
||||
{
|
||||
void some_other_function(int);
|
||||
std::thread t(some_other_function, 42);
|
||||
return t;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t1 = f();
|
||||
t1.join();
|
||||
|
||||
@@ -1,48 +1,46 @@
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
class scoped_thread
|
||||
{
|
||||
class scoped_thread {
|
||||
std::thread t;
|
||||
|
||||
public:
|
||||
explicit scoped_thread(std::thread t_):
|
||||
t(std::move(t_))
|
||||
explicit scoped_thread(std::thread t_) : t(std::move(t_))
|
||||
{
|
||||
if (!t.joinable())
|
||||
throw std::logic_error("No thread");
|
||||
}
|
||||
~scoped_thread()
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
~scoped_thread() { t.join(); }
|
||||
scoped_thread(scoped_thread const&) = delete;
|
||||
scoped_thread& operator=(scoped_thread const&) = delete;
|
||||
};
|
||||
|
||||
void do_something(int& i)
|
||||
void
|
||||
do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
struct func {
|
||||
int& i;
|
||||
|
||||
func(int& i_) : i(i_) {}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
for (unsigned j = 0; j < 1000000; ++j) {
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void do_something_in_current_thread()
|
||||
{}
|
||||
void
|
||||
do_something_in_current_thread()
|
||||
{
|
||||
}
|
||||
|
||||
void f()
|
||||
void
|
||||
f()
|
||||
{
|
||||
int some_local_state;
|
||||
scoped_thread t(std::thread(func(some_local_state)));
|
||||
@@ -50,7 +48,8 @@ void f()
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
void do_work(unsigned id)
|
||||
{}
|
||||
void
|
||||
do_work(unsigned id)
|
||||
{
|
||||
}
|
||||
|
||||
void f()
|
||||
void
|
||||
f()
|
||||
{
|
||||
std::vector<std::thread> threads;
|
||||
for(unsigned i=0;i<20;++i)
|
||||
{
|
||||
for (unsigned i = 0; i < 20; ++i) {
|
||||
threads.push_back(std::thread(do_work, i));
|
||||
}
|
||||
std::for_each(threads.begin(),threads.end(),
|
||||
std::mem_fn(&std::thread::join));
|
||||
std::for_each(
|
||||
threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <thread>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
template <typename Iterator, typename T>
|
||||
struct accumulate_block
|
||||
{
|
||||
struct accumulate_block {
|
||||
void operator()(Iterator first, Iterator last, T& result)
|
||||
{
|
||||
result = std::accumulate(first, last, result);
|
||||
@@ -15,7 +14,8 @@ struct accumulate_block
|
||||
};
|
||||
|
||||
template <typename Iterator, typename T>
|
||||
T parallel_accumulate(Iterator first,Iterator last,T init)
|
||||
T
|
||||
parallel_accumulate(Iterator first, Iterator last, T init)
|
||||
{
|
||||
unsigned long const length = std::distance(first, last);
|
||||
|
||||
@@ -26,8 +26,7 @@ T parallel_accumulate(Iterator first,Iterator last,T init)
|
||||
unsigned long const max_threads =
|
||||
(length + min_per_thread - 1) / min_per_thread;
|
||||
|
||||
unsigned long const hardware_threads=
|
||||
std::thread::hardware_concurrency();
|
||||
unsigned long const hardware_threads = std::thread::hardware_concurrency();
|
||||
|
||||
unsigned long const num_threads =
|
||||
std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
|
||||
@@ -38,28 +37,28 @@ T parallel_accumulate(Iterator first,Iterator last,T init)
|
||||
std::vector<std::thread> threads(num_threads - 1);
|
||||
|
||||
Iterator block_start = first;
|
||||
for(unsigned long i=0;i<(num_threads-1);++i)
|
||||
{
|
||||
for (unsigned long i = 0; i < (num_threads - 1); ++i) {
|
||||
Iterator block_end = block_start;
|
||||
std::advance(block_end, block_size);
|
||||
threads[i]=std::thread(
|
||||
accumulate_block<Iterator,T>(),
|
||||
block_start,block_end,std::ref(results[i]));
|
||||
threads[i] = std::thread(accumulate_block<Iterator, T>(),
|
||||
block_start,
|
||||
block_end,
|
||||
std::ref(results[i]));
|
||||
block_start = block_end;
|
||||
}
|
||||
accumulate_block<Iterator, T>()(block_start, last, results[num_threads - 1]);
|
||||
|
||||
std::for_each(threads.begin(),threads.end(),
|
||||
std::mem_fn(&std::thread::join));
|
||||
std::for_each(
|
||||
threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
|
||||
|
||||
return std::accumulate(results.begin(), results.end(), init);
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::vector<int> vi;
|
||||
for(int i=0;i<10;++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
vi.push_back(10);
|
||||
}
|
||||
int sum = parallel_accumulate(vi.begin(), vi.end(), 5);
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <algorithm>
|
||||
|
||||
std::list<int> some_list;
|
||||
std::mutex some_mutex;
|
||||
|
||||
void add_to_list(int new_value)
|
||||
void
|
||||
add_to_list(int new_value)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(some_mutex);
|
||||
some_list.push_back(new_value);
|
||||
}
|
||||
bool list_contains(int value_to_find)
|
||||
bool
|
||||
list_contains(int value_to_find)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(some_mutex);
|
||||
return std::find(some_list.begin(),some_list.end(),value_to_find)
|
||||
!= some_list.end();
|
||||
return std::find(some_list.begin(), some_list.end(), value_to_find) !=
|
||||
some_list.end();
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
add_to_list(42);
|
||||
std::cout<<"contains(1)="<<list_contains(1)<<", contains(42)="<<list_contains(42)<<std::endl;
|
||||
std::cout << "contains(1)=" << list_contains(1)
|
||||
<< ", contains(42)=" << list_contains(42) << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <mutex>
|
||||
class Y
|
||||
{
|
||||
class Y {
|
||||
private:
|
||||
int some_detail;
|
||||
mutable std::mutex m;
|
||||
@@ -10,6 +9,7 @@ private:
|
||||
std::lock_guard<std::mutex> lock_a(m);
|
||||
return some_detail;
|
||||
}
|
||||
|
||||
public:
|
||||
Y(int sd) : some_detail(sd) {}
|
||||
|
||||
@@ -23,5 +23,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
struct some_resource
|
||||
{
|
||||
void do_something()
|
||||
{}
|
||||
|
||||
struct some_resource {
|
||||
void do_something() {}
|
||||
};
|
||||
|
||||
|
||||
std::shared_ptr<some_resource> resource_ptr;
|
||||
std::mutex resource_mutex;
|
||||
void foo()
|
||||
void
|
||||
foo()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(resource_mutex);
|
||||
if(!resource_ptr)
|
||||
{
|
||||
if (!resource_ptr) {
|
||||
resource_ptr.reset(new some_resource);
|
||||
}
|
||||
lk.unlock();
|
||||
resource_ptr->do_something();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
foo();
|
||||
}
|
||||
|
||||
@@ -1,32 +1,21 @@
|
||||
#include <mutex>
|
||||
|
||||
struct connection_info
|
||||
{};
|
||||
|
||||
struct data_packet
|
||||
{};
|
||||
|
||||
struct connection_handle
|
||||
{
|
||||
void send_data(data_packet const&)
|
||||
{}
|
||||
data_packet receive_data()
|
||||
{
|
||||
return data_packet();
|
||||
}
|
||||
struct connection_info {
|
||||
};
|
||||
|
||||
struct remote_connection_manager
|
||||
{
|
||||
connection_handle open(connection_info const&)
|
||||
{
|
||||
return connection_handle();
|
||||
}
|
||||
struct data_packet {
|
||||
};
|
||||
|
||||
struct connection_handle {
|
||||
void send_data(data_packet const&) {}
|
||||
data_packet receive_data() { return data_packet(); }
|
||||
};
|
||||
|
||||
struct remote_connection_manager {
|
||||
connection_handle open(connection_info const&) { return connection_handle(); }
|
||||
} connection_manager;
|
||||
|
||||
|
||||
class X
|
||||
{
|
||||
class X {
|
||||
private:
|
||||
connection_info connection_details;
|
||||
connection_handle connection;
|
||||
@@ -36,10 +25,12 @@ private:
|
||||
{
|
||||
connection = connection_manager.open(connection_details);
|
||||
}
|
||||
|
||||
public:
|
||||
X(connection_info const& connection_details_):
|
||||
connection_details(connection_details_)
|
||||
{}
|
||||
X(connection_info const& connection_details_)
|
||||
: connection_details(connection_details_)
|
||||
{
|
||||
}
|
||||
void send_data(data_packet const& data)
|
||||
{
|
||||
std::call_once(connection_init_flag, &X::open_connection, this);
|
||||
@@ -52,5 +43,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
class dns_entry
|
||||
{};
|
||||
class dns_entry {
|
||||
};
|
||||
|
||||
class dns_cache
|
||||
{
|
||||
class dns_cache {
|
||||
std::map<std::string, dns_entry> entries;
|
||||
boost::shared_mutex entry_mutex;
|
||||
|
||||
public:
|
||||
dns_entry find_entry(std::string const& domain)
|
||||
{
|
||||
@@ -26,5 +26,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
#include <mutex>
|
||||
|
||||
class some_data
|
||||
{
|
||||
class some_data {
|
||||
int a;
|
||||
std::string b;
|
||||
|
||||
public:
|
||||
void do_something()
|
||||
{}
|
||||
void do_something() {}
|
||||
};
|
||||
|
||||
class data_wrapper
|
||||
{
|
||||
class data_wrapper {
|
||||
private:
|
||||
some_data data;
|
||||
std::mutex m;
|
||||
|
||||
public:
|
||||
template <typename Function>
|
||||
void process_data(Function func)
|
||||
@@ -25,20 +24,23 @@ public:
|
||||
|
||||
some_data* unprotected;
|
||||
|
||||
void malicious_function(some_data& protected_data)
|
||||
void
|
||||
malicious_function(some_data& protected_data)
|
||||
{
|
||||
unprotected = &protected_data;
|
||||
}
|
||||
|
||||
data_wrapper x;
|
||||
|
||||
void foo()
|
||||
void
|
||||
foo()
|
||||
{
|
||||
x.process_data(malicious_function);
|
||||
unprotected->do_something();
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
foo();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
#include <deque>
|
||||
template <typename T, typename Container = std::deque<T>>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
public:
|
||||
explicit stack(const Container&);
|
||||
explicit stack(Container&& = Container());
|
||||
template <class Alloc> explicit stack(const Alloc&);
|
||||
template <class Alloc> stack(const Container&, const Alloc&);
|
||||
template <class Alloc> stack(Container&&, const Alloc&);
|
||||
template <class Alloc> stack(stack&&, const Alloc&);
|
||||
template <class Alloc>
|
||||
explicit stack(const Alloc&);
|
||||
template <class Alloc>
|
||||
stack(const Container&, const Alloc&);
|
||||
template <class Alloc>
|
||||
stack(Container&&, const Alloc&);
|
||||
template <class Alloc>
|
||||
stack(stack&&, const Alloc&);
|
||||
|
||||
bool empty() const;
|
||||
size_t size() const;
|
||||
@@ -20,5 +23,7 @@ public:
|
||||
void swap(stack&&);
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
|
||||
struct empty_stack: std::exception
|
||||
{
|
||||
struct empty_stack : std::exception {
|
||||
const char* what() const throw();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_stack
|
||||
{
|
||||
class threadsafe_stack {
|
||||
public:
|
||||
threadsafe_stack();
|
||||
threadsafe_stack(const threadsafe_stack&);
|
||||
@@ -20,5 +18,7 @@ public:
|
||||
bool empty() const;
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
#include <exception>
|
||||
#include <stack>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
struct empty_stack: std::exception
|
||||
{
|
||||
const char* what() const throw()
|
||||
{
|
||||
return "empty stack";
|
||||
}
|
||||
|
||||
struct empty_stack : std::exception {
|
||||
const char* what() const throw() { return "empty stack"; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_stack
|
||||
{
|
||||
class threadsafe_stack {
|
||||
private:
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
|
||||
public:
|
||||
threadsafe_stack() {}
|
||||
threadsafe_stack(const threadsafe_stack& other)
|
||||
@@ -35,7 +30,8 @@ public:
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
|
||||
data.pop();
|
||||
return res;
|
||||
@@ -43,7 +39,8 @@ public:
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
value = data.top();
|
||||
data.pop();
|
||||
}
|
||||
@@ -54,15 +51,14 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
threadsafe_stack<int> si;
|
||||
si.push(5);
|
||||
si.pop();
|
||||
if(!si.empty())
|
||||
{
|
||||
if (!si.empty()) {
|
||||
int x;
|
||||
si.pop(x);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#include <mutex>
|
||||
|
||||
class some_big_object
|
||||
{};
|
||||
class some_big_object {
|
||||
};
|
||||
|
||||
void swap(some_big_object& lhs,some_big_object& rhs)
|
||||
{}
|
||||
|
||||
class X
|
||||
void
|
||||
swap(some_big_object& lhs, some_big_object& rhs)
|
||||
{
|
||||
}
|
||||
|
||||
class X {
|
||||
private:
|
||||
some_big_object some_detail;
|
||||
mutable std::mutex m;
|
||||
|
||||
public:
|
||||
X(some_big_object const& sd) : some_detail(sd) {}
|
||||
|
||||
@@ -25,5 +27,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,64 +1,68 @@
|
||||
#include <mutex>
|
||||
|
||||
class hierarchical_mutex
|
||||
{
|
||||
class hierarchical_mutex {
|
||||
public:
|
||||
explicit hierarchical_mutex(unsigned level)
|
||||
{}
|
||||
explicit hierarchical_mutex(unsigned level) {}
|
||||
|
||||
void lock()
|
||||
{}
|
||||
void unlock()
|
||||
{}
|
||||
void lock() {}
|
||||
void unlock() {}
|
||||
};
|
||||
|
||||
|
||||
hierarchical_mutex high_level_mutex(10000);
|
||||
hierarchical_mutex low_level_mutex(5000);
|
||||
|
||||
int do_low_level_stuff()
|
||||
int
|
||||
do_low_level_stuff()
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
|
||||
int low_level_func()
|
||||
int
|
||||
low_level_func()
|
||||
{
|
||||
std::lock_guard<hierarchical_mutex> lk(low_level_mutex);
|
||||
return do_low_level_stuff();
|
||||
}
|
||||
|
||||
void high_level_stuff(int some_param)
|
||||
{}
|
||||
void
|
||||
high_level_stuff(int some_param)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void high_level_func()
|
||||
void
|
||||
high_level_func()
|
||||
{
|
||||
std::lock_guard<hierarchical_mutex> lk(high_level_mutex);
|
||||
high_level_stuff(low_level_func());
|
||||
}
|
||||
|
||||
void thread_a()
|
||||
void
|
||||
thread_a()
|
||||
{
|
||||
high_level_func();
|
||||
}
|
||||
|
||||
hierarchical_mutex other_mutex(100);
|
||||
void do_other_stuff()
|
||||
{}
|
||||
void
|
||||
do_other_stuff()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void other_stuff()
|
||||
void
|
||||
other_stuff()
|
||||
{
|
||||
high_level_func();
|
||||
do_other_stuff();
|
||||
}
|
||||
|
||||
void thread_b()
|
||||
void
|
||||
thread_b()
|
||||
{
|
||||
std::lock_guard<hierarchical_mutex> lk(other_mutex);
|
||||
other_stuff();
|
||||
}
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
class hierarchical_mutex
|
||||
{
|
||||
class hierarchical_mutex {
|
||||
std::mutex internal_mutex;
|
||||
unsigned long const hierarchy_value;
|
||||
unsigned long previous_hierarchy_value;
|
||||
@@ -10,8 +9,7 @@ class hierarchical_mutex
|
||||
|
||||
void check_for_hierarchy_violation()
|
||||
{
|
||||
if(this_thread_hierarchy_value <= hierarchy_value)
|
||||
{
|
||||
if (this_thread_hierarchy_value <= hierarchy_value) {
|
||||
throw std::logic_error("mutex hierarchy violated");
|
||||
}
|
||||
}
|
||||
@@ -20,11 +18,12 @@ class hierarchical_mutex
|
||||
previous_hierarchy_value = this_thread_hierarchy_value;
|
||||
this_thread_hierarchy_value = hierarchy_value;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit hierarchical_mutex(unsigned long value):
|
||||
hierarchy_value(value),
|
||||
previous_hierarchy_value(0)
|
||||
{}
|
||||
explicit hierarchical_mutex(unsigned long value)
|
||||
: hierarchy_value(value), previous_hierarchy_value(0)
|
||||
{
|
||||
}
|
||||
void lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
@@ -45,12 +44,12 @@ public:
|
||||
return true;
|
||||
}
|
||||
};
|
||||
thread_local unsigned long
|
||||
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX);
|
||||
thread_local unsigned long hierarchical_mutex::this_thread_hierarchy_value(
|
||||
ULONG_MAX);
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
hierarchical_mutex m1(42);
|
||||
hierarchical_mutex m2(2000);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#include <mutex>
|
||||
|
||||
class some_big_object
|
||||
{};
|
||||
class some_big_object {
|
||||
};
|
||||
|
||||
void swap(some_big_object& lhs,some_big_object& rhs)
|
||||
{}
|
||||
|
||||
class X
|
||||
void
|
||||
swap(some_big_object& lhs, some_big_object& rhs)
|
||||
{
|
||||
}
|
||||
|
||||
class X {
|
||||
private:
|
||||
some_big_object some_detail;
|
||||
mutable std::mutex m;
|
||||
|
||||
public:
|
||||
X(some_big_object const& sd) : some_detail(sd) {}
|
||||
|
||||
@@ -25,5 +27,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,25 +1,30 @@
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
bool more_data_to_prepare()
|
||||
bool
|
||||
more_data_to_prepare()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
struct data_chunk
|
||||
{};
|
||||
struct data_chunk {
|
||||
};
|
||||
|
||||
data_chunk prepare_data()
|
||||
data_chunk
|
||||
prepare_data()
|
||||
{
|
||||
return data_chunk();
|
||||
}
|
||||
|
||||
void process(data_chunk&)
|
||||
{}
|
||||
void
|
||||
process(data_chunk&)
|
||||
{
|
||||
}
|
||||
|
||||
bool is_last_chunk(data_chunk&)
|
||||
bool
|
||||
is_last_chunk(data_chunk&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -28,10 +33,10 @@ std::mutex mut;
|
||||
std::queue<data_chunk> data_queue;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
void data_preparation_thread()
|
||||
{
|
||||
while(more_data_to_prepare())
|
||||
void
|
||||
data_preparation_thread()
|
||||
{
|
||||
while (more_data_to_prepare()) {
|
||||
data_chunk const data = prepare_data();
|
||||
std::lock_guard<std::mutex> lk(mut);
|
||||
data_queue.push(data);
|
||||
@@ -39,10 +44,10 @@ void data_preparation_thread()
|
||||
}
|
||||
}
|
||||
|
||||
void data_processing_thread()
|
||||
{
|
||||
while(true)
|
||||
void
|
||||
data_processing_thread()
|
||||
{
|
||||
while (true) {
|
||||
std::unique_lock<std::mutex> lk(mut);
|
||||
data_cond.wait(lk, [] { return !data_queue.empty(); });
|
||||
data_chunk data = data_queue.front();
|
||||
@@ -54,7 +59,8 @@ void data_processing_thread()
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t1(data_preparation_thread);
|
||||
std::thread t2(data_processing_thread);
|
||||
|
||||
@@ -1,24 +1,19 @@
|
||||
#include <future>
|
||||
void process_connections(connection_set& connections)
|
||||
void
|
||||
process_connections(connection_set& connections)
|
||||
{
|
||||
while(!done(connections))
|
||||
{
|
||||
for(connection_iterator
|
||||
connection=connections.begin(),end=connections.end();
|
||||
while (!done(connections)) {
|
||||
for (connection_iterator connection = connections.begin(),
|
||||
end = connections.end();
|
||||
connection != end;
|
||||
++connection)
|
||||
{
|
||||
if(connection->has_incoming_data())
|
||||
{
|
||||
++connection) {
|
||||
if (connection->has_incoming_data()) {
|
||||
data_packet data = connection->incoming();
|
||||
std::promise<payload_type>& p=
|
||||
connection->get_promise(data.id);
|
||||
std::promise<payload_type>& p = connection->get_promise(data.id);
|
||||
p.set_value(data.payload);
|
||||
}
|
||||
if(connection->has_outgoing_data())
|
||||
{
|
||||
outgoing_packet data=
|
||||
connection->top_of_outgoing_queue();
|
||||
if (connection->has_outgoing_data()) {
|
||||
outgoing_packet data = connection->top_of_outgoing_queue();
|
||||
connection->send(data.payload);
|
||||
data.promise.set_value(true);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
std::condition_variable cv;
|
||||
bool done;
|
||||
std::mutex m;
|
||||
bool wait_loop()
|
||||
bool
|
||||
wait_loop()
|
||||
{
|
||||
auto const timeout= std::chrono::steady_clock::now()+
|
||||
std::chrono::milliseconds(500);
|
||||
auto const timeout =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(500);
|
||||
std::unique_lock<std::mutex> lk(m);
|
||||
while(!done)
|
||||
{
|
||||
while (!done) {
|
||||
if (cv.wait_until(lk, timeout) == std::cv_status::timeout)
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
template <typename T>
|
||||
std::list<T> sequential_quick_sort(std::list<T> input)
|
||||
{
|
||||
if(input.empty())
|
||||
std::list<T>
|
||||
sequential_quick_sort(std::list<T> input)
|
||||
{
|
||||
if (input.empty()) {
|
||||
return input;
|
||||
}
|
||||
std::list<T> result;
|
||||
result.splice(result.begin(), input, input.begin());
|
||||
T const& pivot = *result.begin();
|
||||
auto divide_point=std::partition(input.begin(),input.end(),
|
||||
[&](T const& t){return t<pivot;});
|
||||
auto divide_point = std::partition(
|
||||
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
|
||||
std::list<T> lower_part;
|
||||
lower_part.splice(lower_part.end(),input,input.begin(),
|
||||
divide_point);
|
||||
auto new_lower(
|
||||
sequential_quick_sort(std::move(lower_part)));
|
||||
auto new_higher(
|
||||
sequential_quick_sort(std::move(input)));
|
||||
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
|
||||
auto new_lower(sequential_quick_sort(std::move(lower_part)));
|
||||
auto new_higher(sequential_quick_sort(std::move(input)));
|
||||
result.splice(result.end(), new_higher);
|
||||
Using synchronization of operations to simplify code
|
||||
result.splice(result.begin(),new_lower);
|
||||
Using synchronization of operations to simplify code result.splice(
|
||||
result.begin(), new_lower);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
template <typename T>
|
||||
std::list<T> parallel_quick_sort(std::list<T> input)
|
||||
{
|
||||
if(input.empty())
|
||||
std::list<T>
|
||||
parallel_quick_sort(std::list<T> input)
|
||||
{
|
||||
if (input.empty()) {
|
||||
return input;
|
||||
}
|
||||
std::list<T> result;
|
||||
result.splice(result.begin(), input, input.begin());
|
||||
T const& pivot = *result.begin();
|
||||
auto divide_point=std::partition(input.begin(),input.end(),
|
||||
[&](T const& t){return t<pivot;});
|
||||
auto divide_point = std::partition(
|
||||
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
|
||||
std::list<T> lower_part;
|
||||
lower_part.splice(lower_part.end(),input,input.begin(),
|
||||
divide_point);
|
||||
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
|
||||
std::future<std::list<T>> new_lower(
|
||||
std::async(¶llel_quick_sort<T>, std::move(lower_part)));
|
||||
auto new_higher(
|
||||
parallel_quick_sort(std::move(input)));
|
||||
auto new_higher(parallel_quick_sort(std::move(input)));
|
||||
result.splice(result.end(), new_higher);
|
||||
result.splice(result.begin(), new_lower.get());
|
||||
return result;
|
||||
|
||||
@@ -3,8 +3,7 @@ std::future<std::result_of<F(A&&)>::type>
|
||||
spawn_task(F&& f, A&& a)
|
||||
{
|
||||
typedef std::result_of<F(A &&)>::type result_type;
|
||||
std::packaged_task<result_type(A&&)>
|
||||
task(std::move(f));
|
||||
std::packaged_task<result_type(A &&)> task(std::move(f));
|
||||
std::future<result_type> res(task.get_future());
|
||||
std::thread t(std::move(task), std::move(a));
|
||||
t.detach();
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
struct card_inserted
|
||||
{
|
||||
struct card_inserted {
|
||||
std::string account;
|
||||
};
|
||||
class atm
|
||||
{
|
||||
class atm {
|
||||
messaging::receiver incoming;
|
||||
messaging::sender bank;
|
||||
messaging::sender interface_hardware;
|
||||
@@ -13,31 +11,25 @@ class atm
|
||||
void waiting_for_card()
|
||||
{
|
||||
interface_hardware.send(display_enter_card());
|
||||
incoming.wait()
|
||||
.handle<card_inserted>(
|
||||
[&](card_inserted const& msg)
|
||||
{
|
||||
incoming.wait().handle<card_inserted>([&](card_inserted const& msg) {
|
||||
account = msg.account;
|
||||
pin = "";
|
||||
interface_hardware.send(display_enter_pin());
|
||||
state = &atm::getting_pin;
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
void getting_pin();
|
||||
|
||||
public:
|
||||
void run()
|
||||
{
|
||||
state = &atm::waiting_for_card;
|
||||
try
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
try {
|
||||
for (;;) {
|
||||
(this->*state)();
|
||||
}
|
||||
}
|
||||
catch(messaging::close_queue const&)
|
||||
{
|
||||
catch (messaging::close_queue const&) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,31 +1,20 @@
|
||||
void atm::getting_pin()
|
||||
void
|
||||
atm::getting_pin()
|
||||
{
|
||||
incoming.wait()
|
||||
.handle<digit_pressed>(
|
||||
[&](digit_pressed const& msg)
|
||||
{
|
||||
.handle<digit_pressed>([&](digit_pressed const& msg) {
|
||||
unsigned const pin_length = 4;
|
||||
pin += msg.digit;
|
||||
if(pin.length()==pin_length)
|
||||
{
|
||||
if (pin.length() == pin_length) {
|
||||
bank.send(verify_pin(account, pin, incoming));
|
||||
state = &atm::verifying_pin;
|
||||
}
|
||||
}
|
||||
)
|
||||
.handle<clear_last_pressed>(
|
||||
[&](clear_last_pressed const& msg)
|
||||
{
|
||||
if(!pin.empty())
|
||||
{
|
||||
})
|
||||
.handle<clear_last_pressed>([&](clear_last_pressed const& msg) {
|
||||
if (!pin.empty()) {
|
||||
pin.resize(pin.length() - 1);
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
.handle<cancel_pressed>(
|
||||
[&](cancel_pressed const& msg)
|
||||
{
|
||||
state=&atm::done_processing;
|
||||
}
|
||||
);
|
||||
[&](cancel_pressed const& msg) { state = &atm::done_processing; });
|
||||
}
|
||||
|
||||
@@ -5,10 +5,14 @@ public:
|
||||
explicit queue(Container&& = Container());
|
||||
queue(queue&& q);
|
||||
|
||||
template <class Alloc> explicit queue(const Alloc&);
|
||||
template <class Alloc> queue(const Container&, const Alloc&);
|
||||
template <class Alloc> queue(Container&&, const Alloc&);
|
||||
template <class Alloc> queue(queue&&, const Alloc&);
|
||||
template <class Alloc>
|
||||
explicit queue(const Alloc&);
|
||||
template <class Alloc>
|
||||
queue(const Container&, const Alloc&);
|
||||
template <class Alloc>
|
||||
queue(Container&&, const Alloc&);
|
||||
template <class Alloc>
|
||||
queue(queue&&, const Alloc&);
|
||||
|
||||
queue& operator=(queue&& q);
|
||||
void swap(queue&& q);
|
||||
@@ -26,5 +30,7 @@ public:
|
||||
void pop();
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <memory>
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
public:
|
||||
threadsafe_queue();
|
||||
threadsafe_queue(const threadsafe_queue&);
|
||||
@@ -18,5 +17,7 @@ public:
|
||||
bool empty() const;
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
std::mutex mut;
|
||||
std::queue<T> data_queue;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
public:
|
||||
void push(T new_value)
|
||||
{
|
||||
@@ -25,9 +25,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct data_chunk
|
||||
{};
|
||||
struct data_chunk {
|
||||
};
|
||||
|
||||
data_chunk prepare_data();
|
||||
bool more_data_to_prepare();
|
||||
@@ -36,19 +35,19 @@ bool is_last_chunk(data_chunk);
|
||||
|
||||
threadsafe_queue<data_chunk> data_queue;
|
||||
|
||||
void data_preparation_thread()
|
||||
{
|
||||
while(more_data_to_prepare())
|
||||
void
|
||||
data_preparation_thread()
|
||||
{
|
||||
while (more_data_to_prepare()) {
|
||||
data_chunk const data = prepare_data();
|
||||
data_queue.push(data);
|
||||
}
|
||||
}
|
||||
|
||||
void data_processing_thread()
|
||||
{
|
||||
while(true)
|
||||
void
|
||||
data_processing_thread()
|
||||
{
|
||||
while (true) {
|
||||
data_chunk data;
|
||||
data_queue.wait_and_pop(data);
|
||||
process(data);
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
mutable std::mutex mut;
|
||||
std::queue<T> data_queue;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
public:
|
||||
threadsafe_queue()
|
||||
{}
|
||||
threadsafe_queue() {}
|
||||
threadsafe_queue(threadsafe_queue const& other)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(other.mut);
|
||||
@@ -69,5 +68,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{}
|
||||
int
|
||||
main()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
int find_the_answer_to_ltuae()
|
||||
int
|
||||
find_the_answer_to_ltuae()
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
void do_other_stuff()
|
||||
{}
|
||||
void
|
||||
do_other_stuff()
|
||||
{
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::future<int> the_answer = std::async(find_the_answer_to_ltuae);
|
||||
do_other_stuff();
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
#include <string>
|
||||
#include <future>
|
||||
#include <string>
|
||||
|
||||
struct X
|
||||
{
|
||||
struct X {
|
||||
void foo(int, std::string const&);
|
||||
std::string bar(std::string const&);
|
||||
};
|
||||
|
||||
|
||||
X x;
|
||||
auto f1 = std::async(&X::foo, &x, 42, "hello");
|
||||
auto f2 = std::async(&X::bar, x, "goodbye");
|
||||
|
||||
struct Y
|
||||
{
|
||||
struct Y {
|
||||
double operator()(double);
|
||||
};
|
||||
Y y;
|
||||
@@ -21,8 +18,7 @@ auto f3=std::async(Y(),3.141);
|
||||
auto f4 = std::async(std::ref(y), 2.718);
|
||||
X baz(X&);
|
||||
auto f6 = std::async(baz, std::ref(x));
|
||||
class move_only
|
||||
{
|
||||
class move_only {
|
||||
public:
|
||||
move_only();
|
||||
move_only(move_only&&);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
template <>
|
||||
class packaged_task<std::string(std::vector<char>*,int)>
|
||||
{
|
||||
class packaged_task<std::string(std::vector<char>*, int)> {
|
||||
public:
|
||||
template <typename Callable>
|
||||
explicit packaged_task(Callable&& f);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
@@ -10,10 +10,10 @@ std::deque<std::packaged_task<void()> > tasks;
|
||||
bool gui_shutdown_message_received();
|
||||
void get_and_process_gui_message();
|
||||
|
||||
void gui_thread()
|
||||
{
|
||||
while(!gui_shutdown_message_received())
|
||||
void
|
||||
gui_thread()
|
||||
{
|
||||
while (!gui_shutdown_message_received()) {
|
||||
get_and_process_gui_message();
|
||||
std::packaged_task<void()> task;
|
||||
{
|
||||
@@ -30,7 +30,8 @@ void gui_thread()
|
||||
std::thread gui_bg_thread(gui_thread);
|
||||
|
||||
template <typename Func>
|
||||
std::future<void> post_task_for_gui_thread(Func f)
|
||||
std::future<void>
|
||||
post_task_for_gui_thread(Func f)
|
||||
{
|
||||
std::packaged_task<void()> task(f);
|
||||
std::future<void> res = task.get_future();
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
class spinlock_mutex
|
||||
{
|
||||
class spinlock_mutex {
|
||||
std::atomic_flag flag;
|
||||
|
||||
public:
|
||||
spinlock_mutex():
|
||||
flag(ATOMIC_FLAG_INIT)
|
||||
{}
|
||||
spinlock_mutex() : flag(ATOMIC_FLAG_INIT) {}
|
||||
void lock()
|
||||
{
|
||||
while(flag.test_and_set(std::memory_order_acquire));
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
flag.clear(std::memory_order_release);
|
||||
while (flag.test_and_set(std::memory_order_acquire))
|
||||
;
|
||||
}
|
||||
void unlock() { flag.clear(std::memory_order_release); }
|
||||
};
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <assert.h>
|
||||
struct X
|
||||
{
|
||||
struct X {
|
||||
int i;
|
||||
std::string s;
|
||||
};
|
||||
@@ -11,7 +10,8 @@ struct X
|
||||
std::atomic<X*> p;
|
||||
std::atomic<int> a;
|
||||
|
||||
void create_x()
|
||||
void
|
||||
create_x()
|
||||
{
|
||||
X* x = new X;
|
||||
x->i = 42;
|
||||
@@ -20,7 +20,8 @@ void create_x()
|
||||
p.store(x, std::memory_order_release);
|
||||
}
|
||||
|
||||
void use_x()
|
||||
void
|
||||
use_x()
|
||||
{
|
||||
X* x;
|
||||
while (!(x = p.load(std::memory_order_consume)))
|
||||
@@ -29,11 +30,11 @@ void use_x()
|
||||
assert(x->s == "hello");
|
||||
assert(a.load(std::memory_order_relaxed) == 99);
|
||||
}
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t1(create_x);
|
||||
std::thread t2(use_x);
|
||||
t1.join();
|
||||
t2.join();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,25 +4,24 @@
|
||||
std::vector<int> queue_data;
|
||||
std::atomic<int> count;
|
||||
|
||||
void populate_queue()
|
||||
void
|
||||
populate_queue()
|
||||
{
|
||||
unsigned const number_of_items = 20;
|
||||
queue_data.clear();
|
||||
for(unsigned i=0;i<number_of_items;++i)
|
||||
{
|
||||
for (unsigned i = 0; i < number_of_items; ++i) {
|
||||
queue_data.push_back(i);
|
||||
}
|
||||
|
||||
count.store(number_of_items, std::memory_order_release);
|
||||
}
|
||||
|
||||
void consume_queue_items()
|
||||
{
|
||||
while(true)
|
||||
void
|
||||
consume_queue_items()
|
||||
{
|
||||
while (true) {
|
||||
int item_index;
|
||||
if((item_index=count.fetch_sub(1,std::memory_order_acquire))<=0)
|
||||
{
|
||||
if ((item_index = count.fetch_sub(1, std::memory_order_acquire)) <= 0) {
|
||||
wait_for_more_items();
|
||||
continue;
|
||||
}
|
||||
@@ -30,7 +29,8 @@ void consume_queue_items()
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread a(populate_queue);
|
||||
std::thread b(consume_queue_items);
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<bool> x, y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x_then_y()
|
||||
void
|
||||
write_x_then_y()
|
||||
{
|
||||
x.store(true, std::memory_order_relaxed);
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
y.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_relaxed));
|
||||
while (!y.load(std::memory_order_relaxed))
|
||||
;
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
if (x.load(std::memory_order_relaxed))
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,27 +1,31 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
bool x = false;
|
||||
std::atomic<bool> y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x_then_y()
|
||||
void
|
||||
write_x_then_y()
|
||||
{
|
||||
x = true;
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
y.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_relaxed));
|
||||
while (!y.load(std::memory_order_relaxed))
|
||||
;
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
if (x)
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> data;
|
||||
std::atomic_bool data_ready(false);
|
||||
|
||||
void reader_thread()
|
||||
{
|
||||
while(!data_ready.load())
|
||||
void
|
||||
reader_thread()
|
||||
{
|
||||
while (!data_ready.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
std::cout << "The answer=" << data[0] << "\n";
|
||||
}
|
||||
void writer_thread()
|
||||
void
|
||||
writer_thread()
|
||||
{
|
||||
data.push_back(42);
|
||||
data_ready = true;
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
#include <iostream>
|
||||
|
||||
void foo(int a,int b)
|
||||
void
|
||||
foo(int a, int b)
|
||||
{
|
||||
std::cout << a <<”,”<< b << std::endl;
|
||||
}
|
||||
|
||||
int get_num()
|
||||
int
|
||||
get_num()
|
||||
{
|
||||
static int i = 0;
|
||||
return ++i;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
foo(get_num(), get_num());
|
||||
}
|
||||
|
||||
@@ -1,35 +1,42 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<bool> x, y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x()
|
||||
void
|
||||
write_x()
|
||||
{
|
||||
x.store(true, std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
void write_y()
|
||||
void
|
||||
write_y()
|
||||
{
|
||||
y.store(true, std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
void read_x_then_y()
|
||||
void
|
||||
read_x_then_y()
|
||||
{
|
||||
while(!x.load(std::memory_order_seq_cst));
|
||||
while (!x.load(std::memory_order_seq_cst))
|
||||
;
|
||||
if (y.load(std::memory_order_seq_cst))
|
||||
++z;
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_seq_cst));
|
||||
while (!y.load(std::memory_order_seq_cst))
|
||||
;
|
||||
if (x.load(std::memory_order_seq_cst))
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<bool> x, y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x_then_y()
|
||||
void
|
||||
write_x_then_y()
|
||||
{
|
||||
x.store(true, std::memory_order_relaxed);
|
||||
y.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_relaxed));
|
||||
while (!y.load(std::memory_order_relaxed))
|
||||
;
|
||||
if (x.load(std::memory_order_relaxed))
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
std::atomic<int> x(0), y(0), z(0);
|
||||
std::atomic<bool> go(false);
|
||||
unsigned const loop_count = 10;
|
||||
|
||||
struct read_values
|
||||
{
|
||||
struct read_values {
|
||||
int x, y, z;
|
||||
};
|
||||
read_values values1[loop_count];
|
||||
@@ -15,12 +14,11 @@ read_values values2[loop_count];
|
||||
read_values values3[loop_count];
|
||||
read_values values4[loop_count];
|
||||
read_values values5[loop_count];
|
||||
void increment(std::atomic<int>* var_to_inc,read_values* values)
|
||||
{
|
||||
while(!go)
|
||||
std::this_thread::yield();
|
||||
for(unsigned i=0;i<loop_count;++i)
|
||||
void
|
||||
increment(std::atomic<int>* var_to_inc, read_values* values)
|
||||
{
|
||||
while (!go) std::this_thread::yield();
|
||||
for (unsigned i = 0; i < loop_count; ++i) {
|
||||
values[i].x = x.load(std::memory_order_relaxed);
|
||||
values[i].y = y.load(std::memory_order_relaxed);
|
||||
values[i].z = z.load(std::memory_order_relaxed);
|
||||
@@ -29,12 +27,11 @@ void increment(std::atomic<int>* var_to_inc,read_values* values)
|
||||
}
|
||||
}
|
||||
|
||||
void read_vals(read_values* values)
|
||||
{
|
||||
while(!go)
|
||||
std::this_thread::yield();
|
||||
for(unsigned i=0;i<loop_count;++i)
|
||||
void
|
||||
read_vals(read_values* values)
|
||||
{
|
||||
while (!go) std::this_thread::yield();
|
||||
for (unsigned i = 0; i < loop_count; ++i) {
|
||||
values[i].x = x.load(std::memory_order_relaxed);
|
||||
values[i].y = y.load(std::memory_order_relaxed);
|
||||
values[i].z = z.load(std::memory_order_relaxed);
|
||||
@@ -42,10 +39,10 @@ void read_vals(read_values* values)
|
||||
}
|
||||
}
|
||||
|
||||
void print(read_values* v)
|
||||
{
|
||||
for(unsigned i=0;i<loop_count;++i)
|
||||
void
|
||||
print(read_values* v)
|
||||
{
|
||||
for (unsigned i = 0; i < loop_count; ++i) {
|
||||
if (i)
|
||||
std::cout << ",";
|
||||
std::cout << "(" << v[i].x << "," << v[i].y << "," << v[i].z << ")";
|
||||
@@ -53,7 +50,8 @@ void print(read_values* v)
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t1(increment, &x, values1);
|
||||
std::thread t2(increment, &y, values2);
|
||||
|
||||
@@ -1,35 +1,42 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<bool> x, y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x()
|
||||
void
|
||||
write_x()
|
||||
{
|
||||
x.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
void write_y()
|
||||
void
|
||||
write_y()
|
||||
{
|
||||
y.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
void read_x_then_y()
|
||||
void
|
||||
read_x_then_y()
|
||||
{
|
||||
while(!x.load(std::memory_order_acquire));
|
||||
while (!x.load(std::memory_order_acquire))
|
||||
;
|
||||
if (y.load(std::memory_order_acquire))
|
||||
++z;
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_acquire));
|
||||
while (!y.load(std::memory_order_acquire))
|
||||
;
|
||||
if (x.load(std::memory_order_acquire))
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<bool> x, y;
|
||||
std::atomic<int> z;
|
||||
|
||||
void write_x_then_y()
|
||||
void
|
||||
write_x_then_y()
|
||||
{
|
||||
x.store(true, std::memory_order_relaxed);
|
||||
y.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
void read_y_then_x()
|
||||
void
|
||||
read_y_then_x()
|
||||
{
|
||||
while(!y.load(std::memory_order_acquire));
|
||||
while (!y.load(std::memory_order_acquire))
|
||||
;
|
||||
if (x.load(std::memory_order_relaxed))
|
||||
++z;
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
x = false;
|
||||
y = false;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <assert.h>
|
||||
|
||||
std::atomic<int> data[5];
|
||||
std::atomic<bool> sync1(false), sync2(false);
|
||||
|
||||
void thread_1()
|
||||
void
|
||||
thread_1()
|
||||
{
|
||||
data[0].store(42, std::memory_order_relaxed);
|
||||
data[1].store(97, std::memory_order_relaxed);
|
||||
@@ -15,15 +16,19 @@ void thread_1()
|
||||
sync1.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
void thread_2()
|
||||
void
|
||||
thread_2()
|
||||
{
|
||||
while(!sync1.load(std::memory_order_acquire));
|
||||
while (!sync1.load(std::memory_order_acquire))
|
||||
;
|
||||
sync2.store(std::memory_order_release);
|
||||
}
|
||||
|
||||
void thread_3()
|
||||
void
|
||||
thread_3()
|
||||
{
|
||||
while(!sync2.load(std::memory_order_acquire));
|
||||
while (!sync2.load(std::memory_order_acquire))
|
||||
;
|
||||
assert(data[0].load(std::memory_order_relaxed) == 42);
|
||||
assert(data[1].load(std::memory_order_relaxed) == 97);
|
||||
assert(data[2].load(std::memory_order_relaxed) == 17);
|
||||
@@ -31,7 +36,8 @@ void thread_3()
|
||||
assert(data[4].load(std::memory_order_relaxed) == 2003);
|
||||
}
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::thread t1(thread_1);
|
||||
std::thread t2(thread_2);
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
#include <exception>
|
||||
#include <stack>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
struct empty_stack: std::exception
|
||||
{
|
||||
const char* what() const throw()
|
||||
{
|
||||
return "empty stack";
|
||||
}
|
||||
struct empty_stack : std::exception {
|
||||
const char* what() const throw() { return "empty stack"; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_stack
|
||||
{
|
||||
class threadsafe_stack {
|
||||
private:
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
|
||||
public:
|
||||
threadsafe_stack() {}
|
||||
threadsafe_stack(const threadsafe_stack& other)
|
||||
@@ -34,16 +30,17 @@ public:
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
std::shared_ptr<T> const res(
|
||||
std::make_shared<T>(std::move(data.top())));
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(std::move(data.top())));
|
||||
data.pop();
|
||||
return res;
|
||||
}
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
value = std::move(data.top());
|
||||
data.pop();
|
||||
}
|
||||
@@ -53,4 +50,3 @@ public:
|
||||
return data.empty();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
std::unique_ptr<node> try_pop_head()
|
||||
{
|
||||
std::lock_guard<std::mutex> head_lock(head_mutex);
|
||||
if(head.get()==get_tail())
|
||||
{
|
||||
if (head.get() == get_tail()) {
|
||||
return std::unique_ptr<node>();
|
||||
}
|
||||
return pop_head();
|
||||
@@ -15,8 +13,7 @@ private:
|
||||
std::unique_ptr<node> try_pop_head(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> head_lock(head_mutex);
|
||||
if(head.get()==get_tail())
|
||||
{
|
||||
if (head.get() == get_tail()) {
|
||||
return std::unique_ptr<node>();
|
||||
}
|
||||
value = std::move(*head->data);
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <vector>
|
||||
|
||||
template <typename Key, typename Value, typename Hash = std::hash<Key>>
|
||||
class threadsafe_lookup_table
|
||||
{
|
||||
class threadsafe_lookup_table {
|
||||
private:
|
||||
class bucket_type
|
||||
{
|
||||
class bucket_type {
|
||||
private:
|
||||
typedef std::pair<Key, Value> bucket_value;
|
||||
typedef std::list<bucket_value> bucket_data;
|
||||
@@ -22,29 +20,28 @@ private:
|
||||
|
||||
bucket_iterator find_entry_for(Key const& key) const
|
||||
{
|
||||
return std::find_if(data.begin(),data.end(),
|
||||
[&](bucket_value const& item)
|
||||
{return item.first==key;});
|
||||
return std::find_if(
|
||||
data.begin(), data.end(), [&](bucket_value const& item) {
|
||||
return item.first == key;
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
Value value_for(Key const& key, Value const& default_value) const
|
||||
{
|
||||
boost::shared_lock<boost::shared_mutex> lock(mutex);
|
||||
bucket_iterator const found_entry = find_entry_for(key);
|
||||
return (found_entry==data.end())?
|
||||
default_value : found_entry->second;
|
||||
return (found_entry == data.end()) ? default_value : found_entry->second;
|
||||
}
|
||||
|
||||
void add_or_update_mapping(Key const& key, Value const& value)
|
||||
{
|
||||
std::unique_lock<boost::shared_mutex> lock(mutex);
|
||||
bucket_iterator const found_entry = find_entry_for(key);
|
||||
if(found_entry==data.end())
|
||||
{
|
||||
if (found_entry == data.end()) {
|
||||
data.push_back(bucket_value(key, value));
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
found_entry->second = value;
|
||||
}
|
||||
}
|
||||
@@ -53,8 +50,7 @@ private:
|
||||
{
|
||||
std::unique_lock<boost::shared_mutex> lock(mutex);
|
||||
bucket_iterator const found_entry = find_entry_for(key);
|
||||
if(found_entry!=data.end())
|
||||
{
|
||||
if (found_entry != data.end()) {
|
||||
data.erase(found_entry);
|
||||
}
|
||||
}
|
||||
@@ -74,22 +70,20 @@ public:
|
||||
typedef Value mapped_type;
|
||||
typedef Hash hash_type;
|
||||
|
||||
threadsafe_lookup_table(
|
||||
unsigned num_buckets=19, Hash const& hasher_=Hash()):
|
||||
buckets(num_buckets),hasher(hasher_)
|
||||
{
|
||||
for(unsigned i=0;i<num_buckets;++i)
|
||||
threadsafe_lookup_table(unsigned num_buckets = 19,
|
||||
Hash const& hasher_ = Hash())
|
||||
: buckets(num_buckets), hasher(hasher_)
|
||||
{
|
||||
for (unsigned i = 0; i < num_buckets; ++i) {
|
||||
buckets[i].reset(new bucket_type);
|
||||
}
|
||||
}
|
||||
|
||||
threadsafe_lookup_table(threadsafe_lookup_table const& other) = delete;
|
||||
threadsafe_lookup_table& operator=(
|
||||
threadsafe_lookup_table const& other)=delete;
|
||||
threadsafe_lookup_table& operator=(threadsafe_lookup_table const& other) =
|
||||
delete;
|
||||
|
||||
Value value_for(Key const& key,
|
||||
Value const& default_value=Value()) const
|
||||
Value value_for(Key const& key, Value const& default_value = Value()) const
|
||||
{
|
||||
return get_bucket(key).value_for(key, default_value);
|
||||
}
|
||||
@@ -99,10 +93,5 @@ public:
|
||||
get_bucket(key).add_or_update_mapping(key, value);
|
||||
}
|
||||
|
||||
void remove_mapping(Key const& key)
|
||||
{
|
||||
get_bucket(key).remove_mapping(key);
|
||||
}
|
||||
void remove_mapping(Key const& key) { get_bucket(key).remove_mapping(key); }
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
std::map<Key,Value> threadsafe_lookup_table::get_map() const
|
||||
std::map<Key, Value>
|
||||
threadsafe_lookup_table::get_map() const
|
||||
{
|
||||
std::vector<std::unique_lock<boost::shared_mutex>> locks;
|
||||
for(unsigned i=0;i<buckets.size();++i)
|
||||
{
|
||||
locks.push_back(
|
||||
std::unique_lock<boost::shared_mutex>(buckets[i].mutex));
|
||||
for (unsigned i = 0; i < buckets.size(); ++i) {
|
||||
locks.push_back(std::unique_lock<boost::shared_mutex>(buckets[i].mutex));
|
||||
}
|
||||
std::map<Key, Value> res;
|
||||
for(unsigned i=0;i<buckets.size();++i)
|
||||
{
|
||||
for (unsigned i = 0; i < buckets.size(); ++i) {
|
||||
for (bucket_iterator it = buckets[i].data.begin();
|
||||
it != buckets[i].data.end();
|
||||
++it)
|
||||
{
|
||||
++it) {
|
||||
res.insert(*it);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,28 +2,21 @@
|
||||
#include <mutex>
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_list
|
||||
{
|
||||
struct node
|
||||
{
|
||||
class threadsafe_list {
|
||||
struct node {
|
||||
std::mutex m;
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
|
||||
node():
|
||||
next()
|
||||
{}
|
||||
node() : next() {}
|
||||
|
||||
node(T const& value):
|
||||
data(std::make_shared<T>(value))
|
||||
{}
|
||||
node(T const& value) : data(std::make_shared<T>(value)) {}
|
||||
};
|
||||
|
||||
node head;
|
||||
|
||||
public:
|
||||
threadsafe_list()
|
||||
{}
|
||||
threadsafe_list() {}
|
||||
|
||||
~threadsafe_list()
|
||||
{
|
||||
@@ -46,8 +39,7 @@ public:
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while(node* const next=current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
lk.unlock();
|
||||
f(*next->data);
|
||||
@@ -61,12 +53,10 @@ public:
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while(node* const next=current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
lk.unlock();
|
||||
if(p(*next->data))
|
||||
{
|
||||
if (p(*next->data)) {
|
||||
return next->data;
|
||||
}
|
||||
current = next;
|
||||
@@ -80,17 +70,14 @@ public:
|
||||
{
|
||||
node* current = &head;
|
||||
std::unique_lock<std::mutex> lk(head.m);
|
||||
while(node* const next=current->next.get())
|
||||
{
|
||||
while (node* const next = current->next.get()) {
|
||||
std::unique_lock<std::mutex> next_lk(next->m);
|
||||
if(p(*next->data))
|
||||
{
|
||||
if (p(*next->data)) {
|
||||
std::unique_ptr<node> old_next = std::move(current->next);
|
||||
current->next = std::move(next->next);
|
||||
next_lk.unlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
lk.unlock();
|
||||
current = next;
|
||||
lk = std::move(next_lk);
|
||||
@@ -98,4 +85,3 @@ public:
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
mutable std::mutex mut;
|
||||
std::queue<T> data_queue;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
public:
|
||||
threadsafe_queue()
|
||||
{}
|
||||
threadsafe_queue() {}
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
@@ -33,8 +32,7 @@ public:
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mut);
|
||||
data_cond.wait(lk, [this] { return !data_queue.empty(); });
|
||||
std::shared_ptr<T> res(
|
||||
std::make_shared<T>(std::move(data_queue.front())));
|
||||
std::shared_ptr<T> res(std::make_shared<T>(std::move(data_queue.front())));
|
||||
data_queue.pop();
|
||||
return res;
|
||||
}
|
||||
@@ -53,8 +51,7 @@ public:
|
||||
std::lock_guard<std::mutex> lk(mut);
|
||||
if (data_queue.empty())
|
||||
return std::shared_ptr<T>();
|
||||
std::shared_ptr<T> res(
|
||||
std::make_shared<T>(std::move(data_queue.front())));
|
||||
std::shared_ptr<T> res(std::make_shared<T>(std::move(data_queue.front())));
|
||||
data_queue.pop();
|
||||
return res;
|
||||
}
|
||||
@@ -66,7 +63,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
threadsafe_queue<int> rq;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
mutable std::mutex mut;
|
||||
std::queue<std::shared_ptr<T>> data_queue;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
public:
|
||||
threadsafe_queue()
|
||||
{}
|
||||
threadsafe_queue() {}
|
||||
|
||||
void wait_and_pop(T& value)
|
||||
{
|
||||
@@ -58,11 +57,9 @@ public:
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> data(std::make_shared<T>(std::move(new_value)));
|
||||
std::lock_guard<std::mutex> lk(mut);
|
||||
data_queue.push(data);
|
||||
data_cond.notify_one();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,38 +1,30 @@
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
T data;
|
||||
std::unique_ptr<node> next;
|
||||
|
||||
node(T data_):
|
||||
data(std::move(data_))
|
||||
{}
|
||||
node(T data_) : data(std::move(data_)) {}
|
||||
};
|
||||
|
||||
std::unique_ptr<node> head;
|
||||
node* tail;
|
||||
|
||||
public:
|
||||
queue():
|
||||
tail(nullptr)
|
||||
{}
|
||||
queue() : tail(nullptr) {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
|
||||
std::shared_ptr<T> try_pop()
|
||||
{
|
||||
if(!head)
|
||||
{
|
||||
if (!head) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
std::shared_ptr<T> const res(
|
||||
std::make_shared<T>(std::move(head->data)));
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(std::move(head->data)));
|
||||
std::unique_ptr<node> const old_head = std::move(head);
|
||||
head = std::move(old_head->next);
|
||||
return res;
|
||||
@@ -42,15 +34,12 @@ public:
|
||||
{
|
||||
std::unique_ptr<node> p(new node(std::move(new_value)));
|
||||
node* const new_tail = p.get();
|
||||
if(tail)
|
||||
{
|
||||
if (tail) {
|
||||
tail->next = std::move(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
head = std::move(p);
|
||||
}
|
||||
tail = new_tail;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#include <memory>
|
||||
template <typename T>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
};
|
||||
@@ -13,17 +11,14 @@ private:
|
||||
node* tail;
|
||||
|
||||
public:
|
||||
queue():
|
||||
head(new node),tail(head.get())
|
||||
{}
|
||||
queue() : head(new node), tail(head.get()) {}
|
||||
|
||||
queue(const queue& other) = delete;
|
||||
queue& operator=(const queue& other) = delete;
|
||||
|
||||
std::shared_ptr<T> try_pop()
|
||||
{
|
||||
if(head.get()==tail)
|
||||
{
|
||||
if (head.get() == tail) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
std::shared_ptr<T> const res(head->data);
|
||||
@@ -34,8 +29,7 @@ public:
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> new_data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));
|
||||
std::unique_ptr<node> p(new node);
|
||||
tail->data = new_data;
|
||||
node* const new_tail = p.get();
|
||||
@@ -43,4 +37,3 @@ public:
|
||||
tail = new_tail;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
#include <mutex>
|
||||
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
};
|
||||
@@ -25,8 +23,7 @@ private:
|
||||
std::unique_ptr<node> pop_head()
|
||||
{
|
||||
std::lock_guard<std::mutex> head_lock(head_mutex);
|
||||
if(head.get()==get_tail())
|
||||
{
|
||||
if (head.get() == get_tail()) {
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<node> const old_head = std::move(head);
|
||||
@@ -34,11 +31,8 @@ private:
|
||||
return old_head;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
threadsafe_queue():
|
||||
head(new node),tail(head.get())
|
||||
{}
|
||||
threadsafe_queue() : head(new node), tail(head.get()) {}
|
||||
|
||||
threadsafe_queue(const threadsafe_queue& other) = delete;
|
||||
threadsafe_queue& operator=(const threadsafe_queue& other) = delete;
|
||||
@@ -51,8 +45,7 @@ public:
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> new_data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));
|
||||
std::unique_ptr<node> p(new node);
|
||||
node* const new_tail = p.get();
|
||||
std::lock_guard<std::mutex> tail_lock(tail_mutex);
|
||||
@@ -61,4 +54,3 @@ public:
|
||||
tail = new_tail;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::unique_ptr<node> next;
|
||||
};
|
||||
@@ -13,10 +11,9 @@ private:
|
||||
std::mutex tail_mutex;
|
||||
node* tail;
|
||||
std::condition_variable data_cond;
|
||||
|
||||
public:
|
||||
threadsafe_queue():
|
||||
head(new node),tail(head.get())
|
||||
{}
|
||||
threadsafe_queue() : head(new node), tail(head.get()) {}
|
||||
threadsafe_queue(const threadsafe_queue& other) = delete;
|
||||
threadsafe_queue& operator=(const threadsafe_queue& other) = delete;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
template <typename T>
|
||||
void threadsafe_queue<T>::push(T new_value)
|
||||
void
|
||||
threadsafe_queue<T>::push(T new_value)
|
||||
{
|
||||
std::shared_ptr<T> new_data(
|
||||
std::make_shared<T>(std::move(new_value)));
|
||||
std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));
|
||||
std::unique_ptr<node> p(new node);
|
||||
{
|
||||
std::lock_guard<std::mutex> tail_lock(tail_mutex);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
template <typename T>
|
||||
class threadsafe_queue
|
||||
{
|
||||
class threadsafe_queue {
|
||||
private:
|
||||
node* get_tail()
|
||||
{
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
#include <atomic>
|
||||
|
||||
class spinlock_mutex
|
||||
{
|
||||
class spinlock_mutex {
|
||||
std::atomic_flag flag;
|
||||
|
||||
public:
|
||||
spinlock_mutex():
|
||||
flag(ATOMIC_FLAG_INIT)
|
||||
{}
|
||||
spinlock_mutex() : flag(ATOMIC_FLAG_INIT) {}
|
||||
void lock()
|
||||
{
|
||||
while(flag.test_and_set(std::memory_order_acquire));
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
flag.clear(std::memory_order_release);
|
||||
while (flag.test_and_set(std::memory_order_acquire))
|
||||
;
|
||||
}
|
||||
void unlock() { flag.clear(std::memory_order_release); }
|
||||
};
|
||||
|
||||
@@ -2,30 +2,28 @@
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class lock_free_stack
|
||||
{
|
||||
class lock_free_stack {
|
||||
private:
|
||||
struct node;
|
||||
struct counted_node_ptr
|
||||
{
|
||||
struct counted_node_ptr {
|
||||
int external_count;
|
||||
node* ptr;
|
||||
};
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::atomic<int> internal_count;
|
||||
counted_node_ptr next;
|
||||
node(T const& data_):
|
||||
data(std::make_shared<T>(data_)),
|
||||
internal_count(0)
|
||||
{}
|
||||
node(T const& data_) : data(std::make_shared<T>(data_)), internal_count(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
std::atomic<counted_node_ptr> head;
|
||||
|
||||
public:
|
||||
~lock_free_stack()
|
||||
{
|
||||
while(pop());
|
||||
while (pop())
|
||||
;
|
||||
}
|
||||
void push(T const& data)
|
||||
{
|
||||
@@ -33,6 +31,7 @@ public:
|
||||
new_node.ptr = new node(data);
|
||||
new_node.external_count = 1;
|
||||
new_node.ptr->next = head.load();
|
||||
while(!head.compare_exchange_weak(new_node.ptr->next,new_node));
|
||||
while (!head.compare_exchange_weak(new_node.ptr->next, new_node))
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,44 +1,36 @@
|
||||
template <typename T>
|
||||
class lock_free_stack
|
||||
{
|
||||
class lock_free_stack {
|
||||
private:
|
||||
void increase_head_count(counted_node_ptr& old_counter)
|
||||
{
|
||||
counted_node_ptr new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
++new_counter.external_count;
|
||||
}
|
||||
while(!head.compare_exchange_strong(old_counter,new_counter));
|
||||
} while (!head.compare_exchange_strong(old_counter, new_counter));
|
||||
old_counter.external_count = new_counter.external_count;
|
||||
}
|
||||
|
||||
public:
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
counted_node_ptr old_head = head.load();
|
||||
for(;;)
|
||||
{
|
||||
for (;;) {
|
||||
increase_head_count(old_head);
|
||||
node* const ptr = old_head.ptr;
|
||||
if(!ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
if(head.compare_exchange_strong(old_head,ptr->next))
|
||||
{
|
||||
if (head.compare_exchange_strong(old_head, ptr->next)) {
|
||||
std::shared_ptr<T> res;
|
||||
res.swap(ptr->data);
|
||||
int const count_increase = old_head.external_count - 2;
|
||||
if(ptr->internal_count.fetch_add(count_increase)==
|
||||
-count_increase)
|
||||
{
|
||||
if (ptr->internal_count.fetch_add(count_increase) == -count_increase) {
|
||||
delete ptr;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else if(ptr->internal_count.fetch_sub(1)==1)
|
||||
{
|
||||
else if (ptr->internal_count.fetch_sub(1) == 1) {
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,84 +2,74 @@
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class lock_free_stack
|
||||
{
|
||||
class lock_free_stack {
|
||||
private:
|
||||
struct node;
|
||||
struct counted_node_ptr
|
||||
{
|
||||
struct counted_node_ptr {
|
||||
int external_count;
|
||||
node* ptr;
|
||||
};
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
std::atomic<int> internal_count;
|
||||
counted_node_ptr next;
|
||||
node(T const& data_):
|
||||
data(std::make_shared<T>(data_)),
|
||||
internal_count(0)
|
||||
{}
|
||||
node(T const& data_) : data(std::make_shared<T>(data_)), internal_count(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
std::atomic<counted_node_ptr> head;
|
||||
void increase_head_count(counted_node_ptr& old_counter)
|
||||
{
|
||||
counted_node_ptr new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
++new_counter.external_count;
|
||||
}
|
||||
while(!head.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
} while (!head.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
old_counter.external_count = new_counter.external_count;
|
||||
}
|
||||
|
||||
public:
|
||||
~lock_free_stack()
|
||||
{
|
||||
while(pop());
|
||||
while (pop())
|
||||
;
|
||||
}
|
||||
void push(T const& data)
|
||||
{
|
||||
counted_node_ptr new_node;
|
||||
new_node.ptr = new node(data);
|
||||
new_node.external_count = 1;
|
||||
new_node.ptr->next=head.load(std::memory_order_relaxed)
|
||||
while(!head.compare_exchange_weak(
|
||||
new_node.ptr->next,new_node,
|
||||
new_node.ptr->next = head.load(std::memory_order_relaxed) while (
|
||||
!head.compare_exchange_weak(new_node.ptr->next,
|
||||
new_node,
|
||||
std::memory_order_release,
|
||||
std::memory_order_relaxed));
|
||||
}
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
counted_node_ptr old_head=
|
||||
head.load(std::memory_order_relaxed);
|
||||
for(;;)
|
||||
{
|
||||
counted_node_ptr old_head = head.load(std::memory_order_relaxed);
|
||||
for (;;) {
|
||||
increase_head_count(old_head);
|
||||
node* const ptr = old_head.ptr;
|
||||
if(!ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
if (head.compare_exchange_strong(
|
||||
old_head,ptr->next,std::memory_order_relaxed))
|
||||
{
|
||||
old_head, ptr->next, std::memory_order_relaxed)) {
|
||||
std::shared_ptr<T> res;
|
||||
res.swap(ptr->data);
|
||||
int const count_increase = old_head.external_count - 2;
|
||||
if (ptr->internal_count.fetch_add(
|
||||
count_increase,std::memory_order_release)==-count_increase)
|
||||
{
|
||||
count_increase, std::memory_order_release) == -count_increase) {
|
||||
delete ptr;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else if(ptr->internal_count.fetch_add(
|
||||
-1,std::memory_order_relaxed)==1)
|
||||
{
|
||||
else if (ptr->internal_count.fetch_add(-1, std::memory_order_relaxed) ==
|
||||
1) {
|
||||
ptr->internal_count.load(std::memory_order_acquire);
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
@@ -1,40 +1,33 @@
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::shared_ptr<T> data;
|
||||
node* next;
|
||||
node():
|
||||
next(nullptr)
|
||||
{}
|
||||
node() : next(nullptr) {}
|
||||
};
|
||||
std::atomic<node*> head;
|
||||
std::atomic<node*> tail;
|
||||
node* pop_head()
|
||||
{
|
||||
node* const old_head = head.load();
|
||||
if(old_head==tail.load())
|
||||
{
|
||||
if (old_head == tail.load()) {
|
||||
return nullptr;
|
||||
}
|
||||
head.store(old_head->next);
|
||||
return old_head;
|
||||
}
|
||||
|
||||
public:
|
||||
lock_free_queue():
|
||||
head(new node),tail(head.load())
|
||||
{}
|
||||
lock_free_queue() : head(new node), tail(head.load()) {}
|
||||
lock_free_queue(const lock_free_queue& other) = delete;
|
||||
lock_free_queue& operator=(const lock_free_queue& other) = delete;
|
||||
~lock_free_queue()
|
||||
{
|
||||
while(node* const old_head=head.load())
|
||||
{
|
||||
while (node* const old_head = head.load()) {
|
||||
head.store(old_head->next);
|
||||
delete old_head;
|
||||
}
|
||||
@@ -42,8 +35,7 @@ public:
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
node* old_head = pop_head();
|
||||
if(!old_head)
|
||||
{
|
||||
if (!old_head) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
std::shared_ptr<T> const res(old_head->data);
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
void push(T new_value)
|
||||
void
|
||||
push(T new_value)
|
||||
{
|
||||
std::unique_ptr<T> new_data(new T(new_value));
|
||||
counted_node_ptr new_next;
|
||||
new_next.ptr = new node;
|
||||
new_next.external_count = 1;
|
||||
for(;;)
|
||||
{
|
||||
for (;;) {
|
||||
node* const old_tail = tail.load();
|
||||
T* old_data = nullptr;
|
||||
if(old_tail->data.compare_exchange_strong(
|
||||
old_data,new_data.get()))
|
||||
{
|
||||
if (old_tail->data.compare_exchange_strong(old_data, new_data.get())) {
|
||||
old_tail->next = new_next;
|
||||
tail.store(new_next.ptr);
|
||||
new_data.release();
|
||||
|
||||
@@ -1,24 +1,20 @@
|
||||
#include <atomic>
|
||||
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
struct node;
|
||||
struct counted_node_ptr
|
||||
{
|
||||
struct counted_node_ptr {
|
||||
int external_count;
|
||||
node* ptr;
|
||||
};
|
||||
std::atomic<counted_node_ptr> head;
|
||||
std::atomic<counted_node_ptr> tail;
|
||||
struct node_counter
|
||||
{
|
||||
struct node_counter {
|
||||
unsigned internal_count : 30;
|
||||
unsigned external_counters : 2;
|
||||
};
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
std::atomic<T*> data;
|
||||
std::atomic<node_counter> count;
|
||||
counted_node_ptr next;
|
||||
@@ -32,6 +28,7 @@ private:
|
||||
next.external_count = 0;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
void push(T new_value)
|
||||
{
|
||||
@@ -40,13 +37,11 @@ public:
|
||||
new_next.ptr = new node;
|
||||
new_next.external_count = 1;
|
||||
counted_node_ptr old_tail = tail.load();
|
||||
for(;;)
|
||||
{
|
||||
for (;;) {
|
||||
increase_external_count(tail, old_tail);
|
||||
T* old_data = nullptr;
|
||||
if(old_tail.ptr->data.compare_exchange_strong(
|
||||
old_data,new_data.get()))
|
||||
{
|
||||
if (old_tail.ptr->data.compare_exchange_strong(old_data,
|
||||
new_data.get())) {
|
||||
old_tail.ptr->next = new_next;
|
||||
old_tail = tail.exchange(new_next);
|
||||
free_external_counter(old_tail);
|
||||
|
||||
@@ -1,26 +1,22 @@
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
void release_ref();
|
||||
};
|
||||
|
||||
public:
|
||||
std::unique_ptr<T> pop()
|
||||
{
|
||||
counted_node_ptr old_head = head.load(std::memory_order_relaxed);
|
||||
for(;;)
|
||||
{
|
||||
for (;;) {
|
||||
increase_external_count(head, old_head);
|
||||
node* const ptr = old_head.ptr;
|
||||
if(ptr==tail.load().ptr)
|
||||
{
|
||||
if (ptr == tail.load().ptr) {
|
||||
ptr->release_ref();
|
||||
return std::unique_ptr<T>();
|
||||
}
|
||||
if(head.compare_exchange_strong(old_head,ptr->next))
|
||||
{
|
||||
if (head.compare_exchange_strong(old_head, ptr->next)) {
|
||||
T* const res = ptr->data.exchange(nullptr);
|
||||
free_external_counter(old_head);
|
||||
return std::unique_ptr<T>(res);
|
||||
|
||||
@@ -1,25 +1,19 @@
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
void release_ref()
|
||||
{
|
||||
node_counter old_counter=
|
||||
count.load(std::memory_order_relaxed);
|
||||
node_counter old_counter = count.load(std::memory_order_relaxed);
|
||||
node_counter new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
--new_counter.internal_count;
|
||||
}
|
||||
while(!count.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
if(!new_counter.internal_count &&
|
||||
!new_counter.external_counters)
|
||||
{
|
||||
} while (!count.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
if (!new_counter.internal_count && !new_counter.external_counters) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
static void increase_external_count(
|
||||
std::atomic<counted_node_ptr>& counter,
|
||||
static void increase_external_count(std::atomic<counted_node_ptr>& counter,
|
||||
counted_node_ptr& old_counter)
|
||||
{
|
||||
counted_node_ptr new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
++new_counter.external_count;
|
||||
}
|
||||
while(!counter.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
} while (!counter.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
old_counter.external_count = new_counter.external_count;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
template <typename T>
|
||||
class lock_free_queue
|
||||
{
|
||||
class lock_free_queue {
|
||||
private:
|
||||
static void free_external_counter(counted_node_ptr& old_node_ptr)
|
||||
{
|
||||
node* const ptr = old_node_ptr.ptr;
|
||||
int const count_increase = old_node_ptr.external_count - 2;
|
||||
node_counter old_counter=
|
||||
ptr->count.load(std::memory_order_relaxed);
|
||||
node_counter old_counter = ptr->count.load(std::memory_order_relaxed);
|
||||
node_counter new_counter;
|
||||
do
|
||||
{
|
||||
do {
|
||||
new_counter = old_counter;
|
||||
--new_counter.external_counters;
|
||||
new_counter.internal_count += count_increase;
|
||||
}
|
||||
while(!ptr->count.compare_exchange_strong(
|
||||
old_counter,new_counter,
|
||||
std::memory_order_acquire,std::memory_order_relaxed));
|
||||
if(!new_counter.internal_count &&
|
||||
!new_counter.external_counters)
|
||||
{
|
||||
} while (!ptr->count.compare_exchange_strong(old_counter,
|
||||
new_counter,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed));
|
||||
if (!new_counter.internal_count && !new_counter.external_counters) {
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
#include <atomic>
|
||||
|
||||
template <typename T>
|
||||
class lock_free_stack
|
||||
{
|
||||
class lock_free_stack {
|
||||
private:
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
T data;
|
||||
node* next;
|
||||
node(T const& data_):
|
||||
data(data_)
|
||||
{}
|
||||
node(T const& data_) : data(data_) {}
|
||||
};
|
||||
std::atomic<node*> head;
|
||||
|
||||
public:
|
||||
void push(T const& data)
|
||||
{
|
||||
node* const new_node = new node(data);
|
||||
new_node->next = head.load();
|
||||
while(!head.compare_exchange_weak(new_node->next,new_node));
|
||||
while (!head.compare_exchange_weak(new_node->next, new_node))
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user