Memory Barriers at pthread using CYGWIN

Duncan Roe duncan_roe@optusnet.com.au
Fri Jun 9 01:44:15 GMT 2023


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.


More information about the Cygwin mailing list