how does cobegin work? - concurrency

I don't know how do concurrent processes work? for example what is the output of this program maybe occur?
void main()
{
a = 0;
cobegin { p(); p(); }
}
const int n = 50;
int a;
int i;
void P()
{
for ( i=1; i<=n; i++) a++;
print ( a );
}

Related

Using for loop inside declaration of variable

Can I use for loop inside declaration a variable?
int main() {
int a = {
int b = 0;
for (int i = 0; i < 5; i++) {
b += i;
}
return b;
};
printf("%d", a);
}
You can use a lambda:
int main() {
int a = []{
int b = 0;
for (int i = 0; i < 5; i++) {
b += i;
}
return b;
}();
printf("%d", a);
}
It's important to note that you have to immediately execute it otherwise you attempt to store the lambda. Therefore the extra () at the end.
If you intent to reuse the lambda for multiple instantiations, you can store it separately like this:
int main() {
auto doCalculation = []{
int b = 0;
for (int i = 0; i < 5; i++) {
b += i;
}
return b;
};
int a = doCalculation();
printf("%d", a);
}
If you need it in more than one scope, use a function instead.
actually has been prepared by C++ committee..
constexpr has many usefulness not yet exlpored
constexpr int b(int l) {
int b=0;
for (int i = 0; i < l; i++)
b += i;
return b;
}
int main() {
constexpr int a = b(5);
printf("%d", a);
}

Memory leak without allocating any memory?

I'm working on a coding assignment for a C++ class. When I run my program I seem to be dealing with a memory leakage issue, which is weird since I am NOT explicitly allocating any memory in my code. I ran the program under gdb, and it seems as though the program crashes when running the destructor for a Deck object. I tried stepping through the code, but I when I do so I end up in a host of .h files related to vectors. Then suddenly, it stops. I tried going to a TA for some help, but they seem to be as perplexed as I am on the issue.
# include <stdlib.h>
# include <time.h>
# include <iostream>
# include <vector>
# include <stdio.h>
using namespace std;
//function signatures
float bustProbability (const int);
class Deck
{
public:
//data members
vector <int> cardArray;
vector <int> wasteCards;
//constructor
Deck();
//methods
void shuffleDeck();
void populateDeckWithCards();
void removeCopyCards();
int dealCard();
int remainingCards();
void showCards();
};
void Deck::removeCopyCards() {
for (unsigned int i = 0; i < wasteCards.size(); i++) {
bool removedCopy = false;
for (unsigned int j = 0; j < cardArray.size() && removedCopy == false; j++) {
if (cardArray[j] == wasteCards[i]) {
cardArray.erase (cardArray.begin() + j - 1);
removedCopy = true;
}
}
}
}
int Deck::dealCard() {
if (remainingCards() > 0) {
int tmp = cardArray.back();
wasteCards.push_back(tmp);
cardArray.pop_back();
return tmp;
}
else {
populateDeckWithCards();
removeCopyCards();
shuffleDeck();
//shuffle method
int tmp = cardArray.back();
cardArray.pop_back();
return tmp;
}
}
void Deck::populateDeckWithCards() {
//populate regular cards into array
for (int i = 2; i <= 10; i++) {
for (int j = 0; j < 4; j++) {
cardArray.push_back(i);
}
}
//populate J, Q, K into array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cardArray.push_back(10);
}
}
//populating array with Aces... treating them as special case '100'
for (int i = 0; i < 4; i++) {
cardArray.push_back(100);
}
return;
}
void Deck::showCards() {
for (unsigned int i = 0; i < cardArray.size(); i++) {
cout << cardArray[i] << endl;
}
}
Deck::Deck() {
wasteCards.clear();
cardArray.clear();
populateDeckWithCards();
shuffleDeck();
}
void Deck::shuffleDeck() {
int n = cardArray.size();
for(int a = n-1; a > 0; a--) {
int min = 0;
int max = a;
int j = min + rand() / (RAND_MAX / (max-min + 1) + 1);
int tmp = cardArray[a];
cardArray[a] = cardArray[j];
cardArray[j] = tmp;
}
return;
}
int Deck::remainingCards() {
return cardArray.size();
}
class Player {
public:
//data members
vector <int> playerHand;
//constructor
Player();
//methods
bool isBust();
int count();
void hit(Deck&);
void stand();
bool muckHand();
void showHand();
};
Player::Player() {
playerHand.clear();
}
void Player::showHand() {
for (unsigned int i = 0; i < playerHand.size(); i++) {
cout << playerHand[i] << endl;
}
return;
}
int Player::count() {
int handCount = 0;
for (unsigned int i = 0; i < playerHand.size(); i++) {
if (playerHand[i] != 100)
handCount += playerHand[i];
else {
if (playerHand[i] == 100) {
if ((handCount) > 11) {
handCount += 1;
}
else
handCount += 10;
}
}
}
return handCount;
}
bool Player::isBust() {
if (count() > 21)
return true;
else
return false;
}
void Player::hit(Deck& d) {
playerHand.push_back(d.dealCard());
}
void Player::stand() {
return;
}
bool Player::muckHand() {
playerHand.clear();
return true;
}
float bustProbability (const int threshHold) {
int threshHoldReached = 0;
Deck myDeck;
Player myPlayer;
Player dealer;
for (int i = 0; i < 10000; i++) {
myPlayer.hit(myDeck);
dealer.hit(myDeck);
myPlayer.hit(myDeck);
dealer.hit(myDeck);
while (myPlayer.count() < threshHold) {
myPlayer.hit(myDeck);
}
if (!(myPlayer.isBust())) {
++threshHoldReached;
}
myDeck.wasteCards.clear();
myPlayer.muckHand();
dealer.muckHand();
}
float bustFraction = float(threshHoldReached)/float(10000);
return bustFraction;
}
int main () {
cout << "blackjack simulation" << endl;
srand((unsigned int)time(NULL));
cout << bustProbability(19);
return 0;
}
I'm incredibly sorry for just posting my code, but I've spend 4 days on this issue, and I can't even begin to figure out what the problem is.
There is at least the line
cardArray.erase (cardArray.begin() + j - 1);
which seems to be dubious in case of j = 0

Merge sort for an array of strings in c++

I have a vector of strings named "values". I want to merge sort them but I kept getting bus error or segmentation error. I know the errors are from the merge_sort function but I don't know how to fix it. Any ideas?
int main()
{
int p=1,q=values.size()/2,r=values.size();
merge_sort(values,p,r);
for (unsigned int i = 0; i < values.size(); i++)
{
cout << values[i] << endl;
}
}
}
void merge_sort(vector<string> a,int p,int r)
{
int q;
if(p<r)
{
q=r/2;
merge_sort(a,p,q);
merge_sort(a,q+1,r);
merge(a,p,q,r);
}
}
void merge(vector<string> a,int p,int q,int r)
{
int n1=q-p+1;
int n2=r-q;
string L[n1+1];
string R[n2+1];
for(int i=0;i<n1;i++)
{
L[i]=a[p+i-1];
}
for(int j=0;j<n2;j++)
{
R[j]=a[q+j];
}
L[n1]="ZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
R[n2]="ZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
int i=0, j=0;
for(int k=0;k<r;k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
}
}

Passing an array as a parameter to a function

So I'm doing a programming assignment and I've ran into an issue, when every I attempt to pass the arrays to the header file, I receive an error while compiling, I'm not too clear as how to do this and would much appreciate so assistance in passing these arrays.
Here is the Header file "sorting.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int cost = 0;
void bubble(int Ar[],int N)
{
cost=0;
int swaps = 1;
while(swaps)
{
swaps=0;
for(int i = 0;i<N;i++)
{
if(Ar[i]>Ar[i++])
{
swap(Ar[i],Ar[i++]);
swaps = 1;
cost += 6;
}
cost++;
}
}
for(int i=0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
void shellSort(int Ar[], int N)
{
cost=0;
int swaps = 1;
int gap = N/2;
while(gap>0)
{
while(swaps)
{
swaps = 0;
for(int i = 0;i<N;i++)
{
if(Ar[i]>Ar[i+gap])
{
swap(Ar[i],Ar[i+gap]);
swaps = 1;
cost+=6;
}
cost++;
}
}
gap=gap/2;
}
for(int i = 0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
void quickSort(int Ar[],int left, int right, int N)
{
cost = 0;
int i=left,j=right,tmp;
int pivot = Ar[(left+right)/2];
/*partition*/
while(i<=j)
{
while(Ar[i]<pivot)i++;
while(Ar[j]>pivot)j--;
if(i<=j)
{
tmp=Ar[i];
Ar[i]=Ar[j];
Ar[j]=tmp;
i++;
j--;
cost +=6;
}
cost+=1;
}
/* recursion*/
if(left<j)quickSort(Ar,left,j,N);
if(i<right)quickSort(Ar,i,right,N);
for(int i=0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
/*#if _INCLUDE_LEVEL__<1
int main()
{
}
#endif*/
and here is the main file "sorting2.cpp"
#include <iostream>
#include <cstdlib>
#include "sorting.h"
using namespace std;
//void bubble();
//void shellSort();
//void quickSort();
int main()
{
int N = 20;
int Ar[N];
int Ar2[N];
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i] = rand()%100;
}
bubble(Ar[],N);
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i];
}
shellSort(Ar[],N);
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i];
}
quickSort(Ar[],0,19,N);
}
Thanks in advance!
Change
bubble(Ar[],N);
to
bubble(Ar, N);
(and in other similar places as well)
There are also other problems in your code:
Variable-length arrays are not part of C++ standard:
int Ar[N];
int Ar2[N];
You should change int N = 20; to const int N = 20;
This line produces undefined behavior because the order of evaluation of operator arguments is unspecified:
if(Ar[i]>Ar[i++])

Boost returning values from multithread vector

I am trying to develop a code which generates N threads into a loop. Each thread generates 40 random numbers and pick from them the highest. Afterwards, I have to choose the highest number from all. However, when I return the highest value of each thread (b) it is empty, this is the code I am using:
class rdm_thr
{
public:
rdm_thr()
{
}
void rdmgen()
{
default_random_engine generator;
double rdm;
b=0;
normal_distribution<double> normal(0, 1);
for(int i=0; i<40; i++)
{
rdm = normal(generator);
if(rdm>b)
b = rdm;
}
}
};
void main()
{
vector<boost::thread *> z;
vector<rdm_thr> o;
boost::function<void()> th_func;
for (int i = 0; i < 2; i++)
o.push_back(rdm_thr());
for (int i = 0; i < 2; i++)
{
th_func = boost::bind(&rdm_thr::rdmgen, &o[i]);
boost::thread thr(th_func);
z.push_back(&thr);
}
for (int i = 0; i < 2; i++)
{
z[i]->join();
}
}
Is there another way to do it?
You could change your class logic as such:
class rdm_thr
{
public:
rdm_thr() {}
void rdmgen()
{
...
}
void join() { t.join(); }
void start()
{
t = boost::thread(boost::bind(&rdm_thr::rdmgen, this));
}
private:
boost::thread t;
// could also be pointer type and 'new/delete' would have to be used in that event
};
#define TSZ 2
void main()
{
std::vector<rdm_thr*> o;
int i = 0;
for (; i < TSZ; i++) {
o.push_back(new rdm_thr());
o.back()->start();
}
for (i = 0; i < TSZ; i++) {
o[i]->join();
delete o[i]; //clean up
}
}
And if you didn't want to change your class logic, you could do the following in your main function:
#define TSZ 2
void main()
{
std::vector<boost::thread *> z;
std::vector<rdm_thr *> o;
int i = 0;
for (; i < TSZ; i++) {
o.push_back(new rdm_thr());
z.push_back(new boost::thread(boost::bind(&rdm_thr::rdmgen, o.back())));
}
for (i = 0; i < TSZ; i++) {
z[i]->join();
delete z[i];
delete o[i];
}
}
I don't have access to a compiler right now so I can't verify 100%, but as your asking more on theory, the above code is to help illustrate alternative ways of achieving similar results.
I hope that can help