From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) by sourceware.org (Postfix) with ESMTPS id 41D1F385780D for ; Tue, 30 Mar 2021 07:15:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 41D1F385780D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hobbelt.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ger.hobbelt@gmail.com Received: by mail-qv1-f47.google.com with SMTP id c3so6529205qvz.7 for ; Tue, 30 Mar 2021 00:15:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IokyjR6tklQKBdAzBHaHR+IWxcfcU/yXluk9zP53fks=; b=ZjqbwAFCdPqYRaX++4qtD1sisszu0b8I4p/Wv+TYk6cUIyfl7gWY2N5+Uv8ensavla vi9KBYscSdTtRvpJkBU2vnB9MYoSTWsV1C4dKdlLIGczMSoYG8kgLsUMGoDPK/xyPGEL I4ShzYVePKaVKalkTL4YOBQT2ovYdZ4N5/2OGJcfTEeA8nuQ2LW44SEk3QOjvO5v6nUf rbUhnasNWWXm8C08ob7UTgGxsOr+vrg5A79V9K3O3tsO8pOOxpnTr2qlzHl/sd0JC4pb AFHmPT/Zbwec6hh1VjnFypALEiG3ezgPAjxXAcOlqd8GyUzOTMiH0gc/chsHv1aembb5 LYig== X-Gm-Message-State: AOAM530zQhAJCh+0gucrzR9jRBPX+EM/DtnFD4fOgGUyKU3iEAG3d4+G gY1ZqZzhyuU+31hV+mYn87W4JSfs6V1IWT38h0Q= X-Google-Smtp-Source: ABdhPJyyHgfIMzn1R4jXDRZAjs5i7wvZyRUjUbshupo9ElzVDtspIin1NlaIQ1s4W6Pvq7/9VDqm8g0Dzqf+hx+wHbs= X-Received: by 2002:a05:6214:1624:: with SMTP id e4mr29106028qvw.58.1617088545866; Tue, 30 Mar 2021 00:15:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ger Hobbelt Date: Tue, 30 Mar 2021 09:15:34 +0200 Message-ID: Subject: Re: Problems with conditions To: markus.forrer@gmx.ch Cc: pthreads-win32@sourceware.org X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: pthreads-win32@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pthreads-win32 mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Mar 2021 07:15:48 -0000 See https://stackoverflow.com/questions/16522858/understanding-of-pthread-cond-wait-and-pthread-cond-signal/16524148, also read ALL comments there as some details get explained further. Compare your mutex lock.. Unlock code with the sample code in the answer there: your code has "nothing to do that requires..." so do not need to wait. On Mon, Mar 29, 2021, 14:20 Markus Forrer via Pthreads-win32 < pthreads-win32@sourceware.org> wrote: > I have re-implemented the C++ class OSEvent for a Windows simulation that > exists for a Linux platform. Thereby I have the problem that the function > pthread_cond_wait() does not wait until pthread_cond_signal() is called. > Something I have misunderstood. > > Many thanks for any hints! > > > OSEvent::OSEvent() > { > pthread_condattr_init(&_conditionAttribute); > pthread_cond_init(&_condition, &_conditionAttribute); > pthread_mutexattr_init(&_mutexAttribute); > pthread_mutexattr_settype(&_mutexAttribute, PTHREAD_MUTEX_ERRORCHECK); > pthread_mutex_init(&_mutex, &_mutexAttribute); > } > > OSEvent::~OSEvent() > { > pthread_cond_destroy(&_condition); > pthread_condattr_destroy(&_conditionAttribute); > pthread_mutex_destroy(&_mutex); > pthread_mutexattr_destroy(&_mutexAttribute); > } > > void OSEvent::signal() > { > pthread_mutex_lock(&_mutex); > pthread_cond_signal(&_condition); > pthread_mutex_unlock(&_mutex); > } > > OSEventError OSEvent::timedwait(uint32_t timeout) > { > struct timespec timeoutTime; > timeoutTime.tv_sec = timeout / 1000; > timeoutTime.tv_nsec = 1000000 * (timeout % 1000); > OSEventError success = OSEventError::NoError; > pthread_mutex_lock(&_mutex); > > // Timeout forever > if (timeout == 0) > { > if (pthread_cond_wait(&_condition, &_mutex) != 0) > { > success = OSEventError::Timeout; > } > } > > // Timeout time > else > { > if (pthread_cond_timedwait(&_condition, &_mutex, &timeoutTime) != > 0) > { > success = OSEventError::Timeout; > } > } > > pthread_mutex_unlock(&_mutex); > return success; > } > >