Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68341
| Newsgroups | comp.lang.c++ |
|---|---|
| From | Szyk Cech <szykcech@spoko.pl> |
| Subject | std::thread - DESPERATE NEED HELP!!! |
| Message-ID | <NRaeF.332144$R83.166244@fx23.am4> (permalink) |
| Organization | Newshosting.com - Highest quality at a great price! www.newshosting.com |
| Date | 2019-09-11 20:16 +0200 |
Hi all!
[What I want]
I want write reliable and modern Semaphore class.
I want write unit test to validate it.
[What I do]
I visited some education institution pages and inspired by it
I rewrote their examples. Addresses are:
http://sandbox.mc.edu/~bennet/cs422b/notes/semaphore_h.html
http://sandbox.mc.edu/~bennet/cs422b/notes/semaphore_cpp.html
As you can see this class is simple and silly as hell.
But it not works for me.
I have race conditions for unknown reasons.
It works in debuger. It works when I add some:
cout << "message" << endl << flush;
But not works in stand alone (debug and release both).
My system is Linux KDE Neon (based on Ubuntu 18.04 LTS).
My processor is Intel Core Duo (released in 2009).
[How to reproduce]
I paste here my code with complete minimal example.
It will compile in release mode.
It is fully automatic you need only to create 3 small files
and run: run.sh form commad line.
After 2-3 executions it hangs for unknown reasons.
Please help me! Thanks in advance and best regards!
Szyk Cech
[Files]
=============================================================
BEGIN: run.sh
#!/bin/bash
./make.sh
lCounter=0
while [ 1 ]; do
lCounter=$[lCounter + 1]
echo "Run: $lCounter"
./semtest
done
END: run.sh
=============================================================
BEGIN: make.sh
#!/bin/bash
g++ -std=c++17 -pthread -O2 -s -DNDEBUG ./Main.cpp -o semtest
END: make.sh
=============================================================
BEGIN: Main.cpp
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <thread>
#include <vector>
#include <iostream>
using namespace std;
class Semaphore
{
public:
Semaphore(int aCounter);
public:
int value();
void up();
void down();
protected:
int mCounter;
std::mutex mAccessMutex;
std::condition_variable mWaitingCondition;
};
Semaphore::Semaphore(int aCounter)
: mCounter(aCounter)
{
}
int Semaphore::value()
{
return mCounter;
}
void Semaphore::down()
{
unique_lock lock(mAccessMutex);
--mCounter;
if(mCounter < 0)
mWaitingCondition.wait(lock);
}
void Semaphore::up()
{
unique_lock lock(mAccessMutex);
++mCounter;
if(mCounter <= 0)
mWaitingCondition.notify_one();
}
void SemTestThread(Semaphore* aSem)
{
aSem->down();
}
int main()
{
Semaphore lSem(5);
vector<thread> lArray;
lArray.resize(15);
for(unsigned long i(0); i < lArray.size(); ++i)
lArray[i] = thread(SemTestThread, &lSem);
bool lResult(true);
for(int i(0); i < 5; ++i)
lArray[i].join();
lResult = lResult && (lSem.value() == -10);
for(int i(10); i > 0; --i)
{
lSem.up();
lArray[15 - i].join();
lResult = lResult && (lSem.value() == -i + 1);
}
cout << "lResult: " << lResult << endl << flush;
// return EXIT_SUCCESS;
}
END: Main.cpp
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
std::thread - DESPERATE NEED HELP!!! Szyk Cech <szykcech@spoko.pl> - 2019-09-11 20:16 +0200
Re: std::thread - DESPERATE NEED HELP!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-11 23:01 -0700
Re: std::thread - DESPERATE NEED HELP!!! Ralf Fassel <ralfixx@gmx.de> - 2019-09-12 11:14 +0200
Re: std::thread - DESPERATE NEED HELP!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-13 12:53 -0700
Re: std::thread - DESPERATE NEED HELP!!! Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-13 23:38 -0400
Re: std::thread - DESPERATE NEED HELP!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-13 22:07 -0700
Re: std::thread - DESPERATE NEED HELP!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-13 22:17 -0700
Re: std::thread - DESPERATE NEED HELP!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-09-13 23:01 -0700
csiph-web