public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Duncan Roe <duncan_roe@optusnet.com.au>
To: cygwin@cygwin.com
Subject: Re: Memory Barriers at pthread using CYGWIN
Date: Fri, 9 Jun 2023 11:44:15 +1000	[thread overview]
Message-ID: <ZIKD70cnYSPK3kfC@slk15.local.net> (raw)
In-Reply-To: <CAPxKPA79MOTXkUEKUPFGSWdHr_O93Q44kXs_CbN=YoxfpqJhcw@mail.gmail.com>

On Thu, Jun 08, 2023 at 05:29:59PM +0300, cygwin wrote:
> Hi,
>
> I wrote a simple test for pthread_barrier_wait. it won't work as expected.
>
> result should be
>
> r1 = 1, r2 = 1
>
> Thanks,
> Mümin

> cmake_minimum_required(VERSION 3.26)
>
> project(test)
>
> set(CMAKE_CXX_STANDARD 14)
> set(CMAKE_CXX_STANDARD_REQUIRED ON)
> set(CMAKE_CXX_EXTENSIONS OFF)
>
>
> add_definitions(-D__POSIX_VISIBLE=200112 -D_POSIX_C_SOURCE=200112)
>
> add_executable(test main.cpp)
>
> target_link_libraries(test pthread)

> #include <stdio.h>
> #include <pthread.h>
>
> int x = 0;
> int y = 0;
> int r1 = 0;
> int r2 = 0;
>
> pthread_barrier_t barrier;
>
> void* thread1(void* arg) {
>     y = 1;
>     pthread_barrier_wait(&barrier);  // Memory barrier
>     r1 = x;
>     return NULL;
> }
>
> void* thread2(void* arg) {
>     x = 1;
>     pthread_barrier_wait(&barrier);  // Memory barrier
>     r2 = y;
>     return NULL;
> }
>
> int main() {
>     pthread_t t1, t2;
>
>     pthread_barrier_init(&barrier, NULL, 2);
>
>     pthread_create(&t1, NULL, thread1, NULL);
>     pthread_create(&t2, NULL, thread2, NULL);
>
>     pthread_join(t1, NULL);
>     pthread_join(t2, NULL);
>
>     printf("r1 = %d, r2 = %d\n", r1, r2);
>
>     pthread_barrier_destroy(&barrier);
>
>     return 0;
> }

WFFM (compiled as a C program)

Cheers ... Duncan.

  parent reply	other threads:[~2023-06-09  1:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 14:29 Mümin A.
2023-06-08 14:37 ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2023-06-08 22:58   ` Mümin A.
2023-06-09  0:19     ` Kevin Schnitzius
2023-06-09  0:22     ` Takashi Yano
2023-06-09  1:44 ` Duncan Roe [this message]
     [not found] <CAPxKPA6bRfF19pXrYT04TqbxettyYf=ypgfdySysqAJyB9BfxA@mail.gmail.com>
     [not found] ` <CAPxKPA48sKPqwGc-jU7UQvQhsbT0N6A9LJXCAZeaa1rjeRtuNg@mail.gmail.com>
     [not found]   ` <96718c68-af84-45a5-9332-337ec4b2f04a@email.android.com>
2023-06-09 12:25     ` Takashi Yano
     [not found] ` <64831a27.170a0220.1f805.81bfSMTPIN_ADDED_BROKEN@mx.google.com>
2023-06-10  4:08   ` Takashi Yano
2023-06-20 12:53     ` Takashi Yano
2023-06-22 10:19       ` Takashi Yano
2023-06-22 11:48         ` Corinna Vinschen
2023-06-22 23:49           ` Takashi Yano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZIKD70cnYSPK3kfC@slk15.local.net \
    --to=duncan_roe@optusnet.com.au \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).