Created
November 5, 2020 13:22
-
-
Save 4ge32/8e823e375b8a91805f11518fee224464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The template for writing concurrent program to experiment | |
#include <atomic> | |
#include <cstdio> | |
#include <thread> | |
#include <vector> | |
const static unsigned int producer = 1; | |
const static unsigned int consumer = 2; | |
static std::atomic<int> val(0); | |
void producer_func(void) { | |
val.fetch_add(1); | |
} | |
void consumer_func(void) { std::printf("%s!\n", __func__); } | |
int main(void) { | |
std::vector<std::thread> pro; | |
std::vector<std::thread> con; | |
for (auto i = 0; i < producer; ++i) { | |
pro.push_back(std::thread(producer_func)); | |
} | |
for (auto i = 0; i < consumer; ++i) { | |
con.push_back(std::thread(consumer_func)); | |
} | |
for (auto &p : pro) { | |
p.join(); | |
} | |
for (auto &c : con) { | |
c.join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment