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


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

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 22:07 -0700
Organization Aioe.org NNTP Server
Message-ID <qlhsjd$d5m$1@gioia.aioe.org> (permalink)
References <NRaeF.332144$R83.166244@fx23.am4> <2hZeF.171439$423.71319@fx07.iad>

Show all headers | View raw


On 9/13/2019 8:38 PM, Pavel wrote:
> 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);
>> }
>>
>> 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();
> Code might hang here because these 5 threads are waiting for a signal
> but the signal isonly sent by up() which is called below in the same thread.
> 
> On a side note, down() shall not really decrease the semaphore until it
> is > 0 and it shall wait for it in a loop in this non-exact-waiting test.
> 
> Sub-Note: sometimes (but I think, not in your case) it is correct to
> wait for signal not in a loop --

Using condition variables directly? If the code thinks that the return 
from a condvar wait means a signal was actually sent, well, this is 
mistaken. However, there is something called the eventcount, that can 
abuse a condvar wait. The predicate is atomic.

[...]

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