resources: Added pthreads to the resources.
diff --git a/Makefile b/Makefile
index cf3a08d..0f6cb5b 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@
$(riscv-benchmark-dir)/spmv.riscv \
$(riscv-benchmark-dir)/towers.riscv \
$(riscv-benchmark-dir)/vvadd.riscv
-riscv-dist-dir := $(output)/test-progs/riscv-tests
+riscv-dist-dir := $(output-dir)/test-progs/riscv-tests
# Insttest Parameters
insttest-src := $(source-dir)/insttest
@@ -27,7 +27,23 @@
$(insttest-bin)/insttest-rv64f \
$(insttest-bin)/insttest-rv64i \
$(insttest-bin)/insttest-rv64m
-insttest-dist-dir := $(output)/test-progs/insttest/bin/riscv/linux
+insttest-dist-dir := $(output-dir)/test-progs/insttest/bin/riscv/linux
+
+# PThreads Parameters
+pthreads-dir := $(source-dir)/pthreads
+pthreads-x86-make := Makefile.x86
+pthreads-a32-make := Makefile.aarch32
+pthreads-a64-make := Makefile.aarch64
+pthreads-ri5-make := Makefile.riscv
+pthreads-x86-dir := $(pthreads-dir)/bin.x86
+pthreads-a32-dir := $(pthreads-dir)/bin.aarch32
+pthreads-a64-dir := $(pthreads-dir)/bin.aarch64
+pthreads-ri5-dir := $(pthreads-dir)/bin.riscv64
+pthreads-out := $(output-dir)/test-progs/pthread
+pthreads-x86-out := $(pthreads-out)/x86
+pthreads-a32-out := $(pthreads-out)/aarch32
+pthreads-a64-out := $(pthreads-out)/aarch64
+pthreads-ri5-out := $(pthreads-out)/riscv64
# RISCV-Tests
.PHONY: riscv-tests
@@ -42,7 +58,7 @@
.PHONY: clean-riscv-tests
clean-riscv-tests:
-rm -r $(riscv-dist-dir)
- make -C $(riscv-tests-dir) clean
+ -make -C $(riscv-tests-dir) clean
# Insttests
.PHONY: insttests
@@ -56,12 +72,82 @@
.PHONY: clean-insttests
clean-insttests:
-rm -r $(insttest-dist-dir)
- make -C $(insttest-src) clean
+ -make -C $(insttest-src) clean
+
+# PThreads
+.PHONY: pthreads
+pthreads: pthreads-x86 pthreads-aarch32 pthreads-aarch64 pthreads-riscv64
+
+.PHONY: pthreads-x86
+pthreads-x86: $(pthreads-x86-out)
+
+$(pthreads-x86-out): $(pthreads-x86-dir) $(pthreads-out)
+ cp -r $(pthreads-x86-dir) $(pthreads-x86-out)
+
+$(pthreads-x86-dir): $(pthreads-dir) $(pthreads-dir)/$(pthreads-x86-make)
+ cd $(pthreads-dir) && make -f $(pthreads-x86-make)
+
+.PHONY: clean-pthreads-x86
+clean-pthreads-x86:
+ -rm -rf $(pthreads-x86-out)
+ -cd $(pthreads-dir) && make -f $(pthreads-x86-make) clean
+
+.PHONY: pthreads-aarch32
+pthreads-aarch32: $(pthreads-a32-out)
+
+$(pthreads-a32-out): $(pthreads-a32-dir) $(pthreads-out)
+ cp -r $(pthreads-a32-dir) $(pthreads-a32-out)
+
+$(pthreads-a32-dir): $(pthreads-dir) $(pthreads-dir)/$(pthreads-a32-make)
+ cd $(pthreads-dir) && make -f $(pthreads-a32-make)
+
+.PHONY: clean-pthreads-aarch32
+clean-pthreads-aarch32:
+ -rm -rf $(pthreads-a32-out)
+ -cd $(pthreads-dir) && make -f $(pthreads-a32-make) clean
+
+.PHONY: pthreads-aarch64
+pthreads-aarch64: $(pthreads-a64-out)
+
+$(pthreads-a64-out): $(pthreads-a64-dir) $(pthreads-out)
+ cp -r $(pthreads-a64-dir) $(pthreads-a64-out)
+
+$(pthreads-a64-dir): $(pthreads-dir) $(pthreads-dir)/$(pthreads-a64-make)
+ cd $(pthreads-dir) && make -f $(pthreads-a64-make)
+
+.PHONY: clean-pthreads-aarch64
+clean-pthreads-aarch64:
+ -rm -rf $(pthreads-x86-out)
+ -cd $(pthreads-dir) && make -f $(pthreads-a64-make) clean
+
+.PHONY: pthreads-riscv64
+pthreads-riscv64: $(pthreads-ri5-out)
+
+$(pthreads-ri5-out): $(pthreads-ri5-dir) $(pthreads-out)
+ cp -r $(pthreads-ri5-dir) $(pthreads-ri5-out)
+
+$(pthreads-ri5-dir): $(pthreads-dir) $(pthreads-dir)/$(pthreads-ri5-make)
+ cd $(pthreads-dir) && make -f $(pthreads-ri5-make)
+
+.PHONY: clean-pthreads-riscv64
+clean-pthreads-riscv64:
+ -rm -rf $(pthreads-ri5-out)
+ -cd $(pthreads-dir) && make -f $(pthreads-ri5-make) clean
+
+$(pthreads-out):
+ -mkdir -p $(pthreads-out)
+
+.PHONY: clean-pthreads
+clean-pthreads: clean-pthreads-x86 \
+ clean-pthreads-aarch32 \
+ clean-pthreads-aarch64 \
+ clean-pthreads-riscv64
+ -rm -rf $(pthreads-out)
# Global
.PHONY: all
-all: riscv-tests insttests
+all: riscv-tests insttests pthreads
.PHONY: clean
-clean: clean-riscv-tests clean-insttests
- -rm -r $(output)
+clean: clean-riscv-tests clean-insttests clean-pthreads
+ -rm -r $(output-dir)
diff --git a/README.md b/README.md
index f1e6d80..990e2d0 100644
--- a/README.md
+++ b/README.md
@@ -72,21 +72,18 @@
[mailto:bbruce@ucdavis.edu](bbruce@ucdavis.edu) to have the compiled sources
uploaded to the gem5 resources bucket.
-# Resource: RISCV Tests
+# Requirements
-Origin: <https://github.com/riscv/riscv-tests.git>
+These requirements, their prerequisites, and installation instructions have
+been written with the assumption that they shall be installed on an x86 Ubuntu
+18.04 system. Installation instructions may differ across other systems.
-Revision: 19bfdab48c2a6da4a2c67d5779757da7b073811d
+## RISC-V GNU Compiler Toolchain
-Local: `src/riscv-tests`
+The RISC-V GNU Compiler Toolchain is needed to cross-compile to the RISCV-V
+ISA infrastructure.
-## Dependencies
-
-The RISCV Tests requires the following dependencies:
-
-### RISC-V GNU Compiler Toolchain
-
-#### Prerequisites
+### Prerequisites
```
sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev \
@@ -94,17 +91,50 @@
patchutils bc zlib1g-dev libexpat-dev
```
-#### Installation
+### Installation
```
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
-./configure --prefix=/opt/riscv
-sudo make
+./configure --prefix=/opt/riscv --enable-multilib
+sudo make linux
```
**Ensure `/opt/riscv/bin` is added to the PATH environment variable**.
+## GNU ARM-32 bit Toolchain
+
+The GNU ARM-32 bit toolchain is required to cross compile to the ARM-32 bit
+ISA.
+
+### Installation
+
+The toolchain may be installed via the apt-get package manager:
+
+```
+sudo apt-get install git++-arm-linux-gnueabihf
+```
+
+## GNU ARM-64 bit Toolchain
+
+The GNU ARM-64 bit toolchain is required to cross compile to the ARM-64 bit
+ISA.
+
+### Installation
+
+The toolchain may be installved via the apt-get package manager:
+
+```
+sudo apt-get install g++-aarch64-lunux-gnu
+```
+
+# Resource: RISCV Tests
+
+Origin: <https://github.com/riscv/riscv-tests.git>
+
+Revision: 19bfdab48c2a6da4a2c67d5779757da7b073811d
+
+Local: `src/riscv-tests`
## Compilation
@@ -115,25 +145,9 @@
The output of this compilation can be found at
`output/test-progs/riscv-tests/`
+
# Resource: Insttests
-## Dependencies
-
-The Insttests require the following dependencies:
-
-### RISCV-V GNU Compiler Toolchain (with multilib)
-
-#### Installation
-
-```
-git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
-cd riscv-gnu-toolchain
-./configure --prefix=/opt/riscv --enable-multilib
-sudo make linux
-```
-
-**Ensure `/opt/riscv/bin` is appended to the PATH environment variable**.
-
## Compilation
```
@@ -143,6 +157,27 @@
The output of this compilation can be found in
`output/test-progs/insttest/bin/riscv/linux/`
+# Resource: PThreads
+
+## Compilation
+
+```
+make pthreads
+```
+
+This will compile the pthread binaries for the RISCV, ARM-32, ARM-64, and X86
+ISAs. Specific ISA compilations can be obtained via the following commands:
+
+```
+make pthreads-aarch32
+make pthreads-aarch64
+make pthreads-riscv64
+make pthreads-x86
+```
+
+The output of these compilations can be found in
+`output/test-progs/pthreads`
+
# Licensing
Each project under the `src` is under a different license. Before using
@@ -152,3 +187,5 @@
* **riscv-tests** : `src/riscv-tests/LICENSE`.
* **insttests** : Consult individual copyright notices of source files in
`src/insttests`.
+* **pthreads**: Consult individual copyright notices of source files in
+`src/pthreads`.
diff --git a/src/pthreads/Makefile.aarch32 b/src/pthreads/Makefile.aarch32
new file mode 100644
index 0000000..1acb097
--- /dev/null
+++ b/src/pthreads/Makefile.aarch32
@@ -0,0 +1,39 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+CROSS_COMPILE = arm-linux-gnueabihf-
+BIN_DIR = bin.aarch32
+
+-include Makefile.common
diff --git a/src/pthreads/Makefile.aarch64 b/src/pthreads/Makefile.aarch64
new file mode 100644
index 0000000..da30820
--- /dev/null
+++ b/src/pthreads/Makefile.aarch64
@@ -0,0 +1,39 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+CROSS_COMPILE = aarch64-linux-gnu-
+BIN_DIR = bin.aarch64
+
+-include Makefile.common
diff --git a/src/pthreads/Makefile.common b/src/pthreads/Makefile.common
new file mode 100644
index 0000000..a7385ce
--- /dev/null
+++ b/src/pthreads/Makefile.common
@@ -0,0 +1,64 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+CC := $(CROSS_COMPILE)$(CC)
+CXX := $(CROSS_COMPILE)$(CXX)
+LD := $(CXX)
+CCFLAGS ?=
+CXXFLAGS ?= -g -O3 -static -std=c++11
+LDFLAGS ?= -pthread
+
+BIN_DIR ?= ./bin
+
+TESTS = test_pthread_create_seq \
+ test_pthread_create_para \
+ test_pthread_mutex \
+ test_atomic \
+ test_pthread_cond \
+ test_std_thread \
+ test_std_mutex \
+ test_std_condition_variable
+
+all: $(addprefix $(BIN_DIR)/,$(TESTS))
+
+$(BIN_DIR)/%: src/%.cpp
+ @mkdir -p $(BIN_DIR)
+ $(CXX) -o $@ $(CCFLAGS) $(CXXFLAGS) $(LDFLAGS) $^
+
+clean:
+ $(RM) -r $(BIN_DIR)
+
+.PHONY: all clean
+
diff --git a/src/pthreads/Makefile.riscv b/src/pthreads/Makefile.riscv
new file mode 100644
index 0000000..ea486a7
--- /dev/null
+++ b/src/pthreads/Makefile.riscv
@@ -0,0 +1,39 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+CROSS_COMPILE = riscv64-unknown-linux-gnu-
+BIN_DIR = bin.riscv64
+
+-include Makefile.common
diff --git a/src/pthreads/Makefile.x86 b/src/pthreads/Makefile.x86
new file mode 100644
index 0000000..90013cd
--- /dev/null
+++ b/src/pthreads/Makefile.x86
@@ -0,0 +1,39 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+CROSS_COMPILE =
+BIN_DIR = bin.x86
+
+-include Makefile.common
diff --git a/src/pthreads/src/test_atomic.cpp b/src/pthreads/src/test_atomic.cpp
new file mode 100644
index 0000000..68c94e4
--- /dev/null
+++ b/src/pthreads/src/test_atomic.cpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pthread.h>
+
+#include <atomic>
+#include <cstdlib>
+#include <iostream>
+
+//------------------------------------------------------------------------
+// Create n threads, run them in parallel and wait for them in the master
+// thread.
+// Each child thread increments a shared variable m times atomically
+//------------------------------------------------------------------------
+
+#define MAX_N_WORKER_THREADS 10
+
+typedef struct
+{
+ int nsteps;
+ std::atomic<int>* shared_var;
+} ThreadArg;
+
+void* func( void* args )
+{
+ ThreadArg* my_args = ( ThreadArg* ) args;
+
+ int nsteps = my_args->nsteps;
+ std::atomic<int>* shared_var = my_args->shared_var;
+
+ for ( int i = 0; i < nsteps; ++i ) {
+ std::atomic_fetch_add(shared_var, 1);
+ }
+
+ return nullptr;
+}
+
+int main( int argc, const char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ // allocate all threads
+ pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
+
+ // variable shared among all threads
+ std::atomic<int> shared_var(0);
+
+ // number of steps each thread increments the shared_var
+ int nsteps = 1000;
+
+ // set up threads' arguments
+ ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
+
+ int ret = 0;
+ for ( size_t tid = 0; tid < MAX_N_WORKER_THREADS; tid++ ){
+ t_args[tid].nsteps = nsteps;
+ t_args[tid].shared_var = &shared_var;
+
+ // spawn thread
+ ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
+
+ if (ret != 0) {
+ break;
+ }
+
+ n_worker_threads++;
+ }
+
+ // sync up all threads
+ for ( int tid = 0; tid < n_worker_threads; ++tid ) {
+ pthread_join( threads[tid], nullptr );
+ }
+
+ // clean up
+ delete[] threads;
+ delete[] t_args;
+
+ // verify
+ if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_pthread_cond.cpp b/src/pthreads/src/test_pthread_cond.cpp
new file mode 100644
index 0000000..5552f78
--- /dev/null
+++ b/src/pthreads/src/test_pthread_cond.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Author: Moyang Wang
+ */
+
+#include <pthread.h>
+
+#include <cstdlib>
+#include <iostream>
+#include <mutex>
+#include <vector>
+
+//------------------------------------------------------------------------
+// Test pthread_cond
+//------------------------------------------------------------------------
+// The master thread creates N threads, each of which waits on a
+// condition variable of a signal to start. The master thread then set
+// the signal and notifies all other threads to begin.
+
+#define MAX_N_WORKER_THREADS 10
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
+
+bool ready = false;
+
+void* print_id( void* arg_vptr )
+{
+ pthread_mutex_lock( &mutex );
+ long id = (long)arg_vptr;
+
+ while (!ready) {
+ pthread_cond_wait( &cv, &mutex );
+ }
+ // ...
+ std::cout << "thread " << id << '\n';
+
+ pthread_mutex_unlock( &mutex );
+
+ return nullptr;
+}
+
+void go()
+{
+ pthread_mutex_lock( &mutex );
+ ready = true;
+ pthread_cond_broadcast( &cv );
+ pthread_mutex_unlock( &mutex );
+}
+
+int main( int argc, char* argv[] )
+{
+ size_t n_worker_threads = 0;
+
+ std::vector< pthread_t > threads( MAX_N_WORKER_THREADS );
+
+ int ret = 0;
+ for ( size_t i = 0; i < MAX_N_WORKER_THREADS; i++ ){
+ ret = pthread_create( &threads[i], nullptr, print_id, (void*)i );
+ if (ret != 0) {
+ break;
+ }
+ n_worker_threads++;
+ }
+
+ std::cout << n_worker_threads << " threads ready to race...\n";
+
+ go();
+
+ for ( size_t i = 1; i < n_worker_threads; i++ ) {
+ pthread_join( threads[i], nullptr );
+ }
+
+ if (n_worker_threads < 1) {
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_pthread_create_para.cpp b/src/pthreads/src/test_pthread_create_para.cpp
new file mode 100644
index 0000000..1b10c9f
--- /dev/null
+++ b/src/pthreads/src/test_pthread_create_para.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pthread.h>
+
+#include <cstdlib>
+#include <iostream>
+
+//------------------------------------------------------------------------
+// Create n threads, run them in parallel and wait for them in the master
+// thread.
+// Each child thread writes its thread id to an output array
+//------------------------------------------------------------------------
+
+#define MAX_N_WORKER_THREADS 10
+
+typedef struct
+{
+ int tid;
+ int* output;
+} ThreadArg;
+
+void* func( void* args )
+{
+ ThreadArg* my_args = ( ThreadArg* ) args;
+
+ // write tid to this thread's output
+ (*my_args->output) = my_args->tid;
+
+ return nullptr;
+}
+
+int main( int argc, const char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ // allocate all threads
+ pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
+ ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
+
+ // create an output array for all threads
+ int* outputs = new int[MAX_N_WORKER_THREADS];
+ int ret;
+
+ // try to spawn as many worker threads as possible
+ for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
+
+ // set up thread args
+ t_args[tid].tid = tid;
+ t_args[tid].output = outputs + tid;
+
+ // spawn thread
+ ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
+ if (ret != 0) {
+ break;
+ }
+
+ n_worker_threads++;
+ }
+
+ // sync up all threads
+ for ( int tid = 0; tid < n_worker_threads; ++tid ) {
+ pthread_join( threads[tid], nullptr );
+ }
+
+ // verify
+ bool passed = true;
+ for ( int i = 0; i < n_worker_threads; ++i ) {
+ if ( outputs[i] != i ) {
+ passed = false;
+ }
+ }
+
+ // clean up
+ delete[] threads;
+ delete[] t_args;
+ delete[] outputs;
+
+ // failed if outputs are not correct or no worker thread was spawned
+ if (!passed || n_worker_threads < 1)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_pthread_create_seq.cpp b/src/pthreads/src/test_pthread_create_seq.cpp
new file mode 100644
index 0000000..2a5a571
--- /dev/null
+++ b/src/pthreads/src/test_pthread_create_seq.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pthread.h>
+
+#include <cstdlib>
+#include <iostream>
+
+//------------------------------------------------------------------------
+// Create n threads and run them one after another
+// Each child thread writes its thread id to an output array
+//------------------------------------------------------------------------
+
+#define MAX_N_WORKER_THREADS 10
+
+typedef struct
+{
+ int tid;
+ int* output;
+} ThreadArg;
+
+void* func( void* args )
+{
+ ThreadArg* my_args = ( ThreadArg* ) args;
+
+ // write tid to this thread's output
+ (*my_args->output) = my_args->tid;
+
+ return nullptr;
+}
+
+int main( int argc, const char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ // allocate all threads
+ pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
+ ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
+
+ // create an output array for all threads
+ int* outputs = new int[MAX_N_WORKER_THREADS];
+ int ret;
+
+ // try to spawn as many worker threads as possible
+ for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
+
+ // set up thread args
+ t_args[tid].tid = tid;
+ t_args[tid].output = outputs + tid;
+
+ // spawn thread
+ ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
+ if (ret != 0 ) {
+ break;
+ }
+
+ n_worker_threads++;
+
+ // wait for the thread to join before moving on
+ pthread_join( threads[tid], nullptr );
+ }
+
+ // verify
+ bool passed = true;
+ for ( int i = 0; i < n_worker_threads; ++i ) {
+ if ( outputs[i] != i ) {
+ passed = false;
+ break;
+ }
+ }
+
+ // clean up
+ delete[] threads;
+ delete[] t_args;
+ delete[] outputs;
+
+ // failed if outputs are not correct or no worker thread was spawned
+ if (!passed || n_worker_threads < 1)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_pthread_mutex.cpp b/src/pthreads/src/test_pthread_mutex.cpp
new file mode 100644
index 0000000..313539b
--- /dev/null
+++ b/src/pthreads/src/test_pthread_mutex.cpp
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pthread.h>
+
+#include <cstdlib>
+#include <iostream>
+
+//------------------------------------------------------------------------
+// Create n threads, run them in parallel and wait for them in the master
+// thread.
+// Each child thread increments a shared variable m times
+//------------------------------------------------------------------------
+
+#define MAX_N_WORKER_THREADS 10
+
+typedef struct
+{
+ int nsteps;
+ int* shared_var;
+ pthread_mutex_t* lock;
+} ThreadArg;
+
+void* func( void* args )
+{
+ ThreadArg* my_args = ( ThreadArg* ) args;
+
+ int nsteps = my_args->nsteps;
+ int* shared_var = my_args->shared_var;
+ pthread_mutex_t* lock = my_args->lock;
+
+ for ( int i = 0; i < nsteps; ++i ) {
+ // acquire the lock
+ pthread_mutex_lock(lock);
+
+ // increment the shared_var
+ (*shared_var)++;
+
+ // release the lock
+ pthread_mutex_unlock(lock);
+ }
+
+ return nullptr;
+}
+
+int main( int argc, const char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ // allocate all threads
+ pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
+ ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
+
+ // variable shared among all threads
+ int shared_var = 0;
+
+ // number of steps each thread increments the shared_var
+ int nsteps = 10000;
+
+ // create a shared lock
+ pthread_mutex_t lock;
+ pthread_mutex_init(&lock, NULL);
+
+ int ret;
+
+ // try to spawn as many worker threads as possible
+ for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
+ t_args[tid].nsteps = nsteps;
+ t_args[tid].shared_var = &shared_var;
+ t_args[tid].lock = &lock;
+
+ // spawn thread
+ ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
+
+ if (ret != 0)
+ break;
+
+ n_worker_threads++;
+ }
+
+ // sync up all threads
+ for ( int tid = 0; tid < n_worker_threads; ++tid ) {
+ pthread_join( threads[tid], nullptr );
+ }
+
+ // verify
+ bool passed = true;
+ if ( shared_var != n_worker_threads * nsteps )
+ passed = false;
+
+ // clean up
+ delete[] threads;
+ delete[] t_args;
+
+ if (!passed || n_worker_threads < 1)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_std_condition_variable.cpp b/src/pthreads/src/test_std_condition_variable.cpp
new file mode 100644
index 0000000..b3d162d
--- /dev/null
+++ b/src/pthreads/src/test_std_condition_variable.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <condition_variable>
+#include <iostream>
+#include <mutex>
+#include <thread>
+#include <vector>
+
+//------------------------------------------------------------------------
+// Test std::condition_variable
+//------------------------------------------------------------------------
+// The master thread creates N threads, each of which waits on a
+// condition variable of a signal to start. The master thread then set
+// the signal and notifies all other threads to begin.
+
+#define MAX_N_WORKER_THREADS 10
+
+std::mutex mtx;
+std::condition_variable cv;
+bool ready = false;
+
+void print_id( size_t id )
+{
+ std::unique_lock<std::mutex> lck(mtx);
+ while (!ready)
+ cv.wait(lck);
+ // ...
+ std::cout << "thread " << id << '\n';
+}
+
+void go()
+{
+ std::unique_lock<std::mutex> lck(mtx);
+ ready = true;
+ cv.notify_all();
+}
+
+int main( int argc, char* argv[] )
+{
+ size_t n_worker_threads = 0;
+
+ std::vector< std::thread > threads;
+
+ for ( size_t i = 0; i < MAX_N_WORKER_THREADS; i++ ) {
+ try {
+ threads.push_back( std::thread( print_id, i ) );
+ } catch ( const std::system_error& err ) {
+ break;
+ }
+ n_worker_threads++;
+ }
+
+ std::cout << n_worker_threads << " threads ready to race...\n";
+ go(); // go!
+
+ for (int i = 0; i < n_worker_threads; ++i) {
+ threads[i].join();
+ }
+
+ // if there is no timeout (i.e., threads are all waken up properly, this
+ // test always succeeds)
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_std_mutex.cpp b/src/pthreads/src/test_std_mutex.cpp
new file mode 100644
index 0000000..e81f60f
--- /dev/null
+++ b/src/pthreads/src/test_std_mutex.cpp
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <cstdlib>
+#include <iostream>
+#include <mutex>
+#include <thread>
+#include <vector>
+
+//------------------------------------------------------------------------
+// Create n threads, run them in parallel and wait for them in the master
+// thread.
+// Each child thread increments a shared variable m times
+//------------------------------------------------------------------------
+
+#define MAX_N_WORKER_THREADS 10
+
+int main( int argc, const char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ // allocate all threads
+ std::vector< std::thread > threads;
+
+ // mutex to protect the shared variable
+ std::mutex my_mutex;
+
+ // variable shared among all threads
+ int shared_var = 0;
+
+ // number of steps each thread increments the shared_var
+ int nsteps = 1000;
+
+ for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
+ try {
+ threads.push_back( std::thread( [&] {
+ std::lock_guard<std::mutex> guard(my_mutex);
+ for ( int i = 0; i < nsteps; ++i )
+ shared_var++;
+ } ) );
+ } catch ( const std::system_error& err ) {
+ break;
+ }
+ n_worker_threads++;
+ }
+
+ // sync up all threads
+ for (int i = 0; i < n_worker_threads; ++i) {
+ threads[i].join();
+ }
+
+ // verify
+ if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1) {
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/pthreads/src/test_std_thread.cpp b/src/pthreads/src/test_std_thread.cpp
new file mode 100644
index 0000000..6634d90
--- /dev/null
+++ b/src/pthreads/src/test_std_thread.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2018, Cornell University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of Cornell University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <thread>
+#include <vector>
+
+//------------------------------------------------------------------------
+// Test std::thread
+//------------------------------------------------------------------------
+// Create n threads, run them in parallel and wait for them in the master
+// thread.
+// Each child thread writes its thread id to an output array
+
+#define MAX_N_WORKER_THREADS 10
+
+int main( int argc, char* argv[] )
+{
+ int n_worker_threads = 0;
+
+ std::vector< std::thread > threads;
+ std::vector<int> outputs( MAX_N_WORKER_THREADS, 0 );
+
+ for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
+ try {
+ threads.push_back( std::thread( [&] (size_t thread_id ) {
+ std::cout << "Hello from thread " << thread_id
+ << std::endl;
+ outputs[thread_id] = thread_id;
+ }, tid ) );
+ } catch ( const std::system_error& err ) {
+ break;
+ }
+ n_worker_threads++;
+ }
+
+ std::cout << "Hello from master thread" << std::endl;
+
+ // sync up all threads
+ for (int i = 0; i < n_worker_threads; ++i) {
+ threads[i].join();
+ }
+
+ if (n_worker_threads < 1) {
+ return EXIT_FAILURE;
+ }
+
+ for ( int i = 0; i < n_worker_threads; ++i ) {
+ if ( outputs[i] != i ) {
+ return EXIT_FAILURE;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}