Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #68400

Re: std::thread - DESPERATE NEED HELP!!!

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c++
Subject Re: std::thread - DESPERATE NEED HELP!!!
Date 2019-09-13 12:53 -0700
Organization Aioe.org NNTP Server
Message-ID <qlgs3q$3cj$1@gioia.aioe.org> (permalink)
References <NRaeF.332144$R83.166244@fx23.am4>

Show all headers | View raw


On 9/11/2019 11:16 AM, Szyk Cech wrote:
> 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);
> }

This is not how a condition variable was designed to be used. In fact, 
iirc, a conforming condvar impl does not even need to block, it can just 
yield the thread and continue on. You need to contain your wait 
predicate in a _loop_. The dec function from my example:
__________________________
void dec() // wait
{
     std::unique_lock<std::mutex> lock(m_mutex);
     while (m_count == 0) m_cond.wait(lock);
     --m_count;
}
__________________________

Notice the while loop?

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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