Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:43:54+00:00 2026-06-01T10:43:54+00:00

This is a strange problem.I init sem and destory it first,then i init it

  • 0

This is a strange problem.I init sem and destory it first,then i init it again in a thread.then, i can’t wake it again.the code is:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>

sem_t sem;
int key = 1;

static void *
wait_func()
{
    printf("i'm wait\n");
    sem_wait(&sem);
}

static void *
cancel_func(void *arg)
{
    pthread_t tid = *(pthread_t *)arg;
    if (key == 1)
    {
        sleep(1);
        key = 0;
    }
    else
        sleep(3);
    if(pthread_cancel(tid) == 0)
        printf("cancle!\n");

    sem_init(&sem, 0, 0);
    sem_destroy(&sem);
}

int
main(int argc, char *argv[])
{
    pthread_t wthread, cthread;
    pthread_attr_t attr;
    int i = 0;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    sem_init(&sem, 0, 0);

    while (i < 2)
    {
    //        sem_init(&sem, 0, 0);

        pthread_create(&wthread, &attr, wait_func, NULL);
        if (i < 1)
            pthread_create(&cthread, &attr, cancel_func, &wthread);
        if (key == 0)
        {
            sleep(2);
            if (sem_post(&sem) == 0)
                printf("post!\n");
            key = 1;
        }
        sleep(4);
        ++i;
    }
    sleep(1000);
}

however,it will be work that changed sem_init in while loop like comment!So i have straced two program,and finded 1)init in thread,sem_post didn’t call sys_futex,like this:

nanosleep({2, 0}, i'm wait

{2, 0}) = 0
write(1, “post!\n”, 6post!
) = 6

2)init in main process,sem_post call sys_futex,like this:

nanosleep({2, 0}, i'm wait

{2, 0}) = 0
futex(0x600dc0, FUTEX_WAKE_PRIVATE, 1) = 1
write(1, “post!\n”, 6post!
) = 6

Then,i thought maybe this is a problem with syscall.I have used gdb to disassemble two program in sem_post. unfortunately,1)init in thread,it also call syscall in sem_post;2) compared their registry status where rip is syscall,same too.

in thread:
rax            0xca     202          //sys_futex
rbx            0x3c0e61bbc0     257939323840
rcx            0x0      0            //utime
rdx            0x1      1            //val
rsi            0x81     129          //op:private_wake
rdi            0x600dc0 6294976      //sem uaddr
in main process:
rax            0xca     202
rbx            0x3c0e61bbc0     257939323840
rcx            0x0      0
rdx            0x1      1
rsi            0x81     129
rdi            0x600da0 6294944

At last,i have no idea about this problem.Please give me some advise to find out the solution,thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:43:57+00:00Added an answer on June 1, 2026 at 10:43 am

    I think that your problem lies in the access to key. There is no guarantee that the access to shared variables is atomic. In particular the compiler might optimize the reading of a value from memory, such that it doesn’t see a modification of the value that is done in another thread.

    To avoid that the optimizer kicks in, you’d have to declare your key variable volatile. But I am not sure that the way you have written your program would guarantee that there are memory barriers that would guarantee that the threads are guaranteed to seen modifications and that the concurrent write is not mixing things up.

    Modern C11 also has _Atomic to ensure that the access is atomic. But C11 is not yet fully implemented (there may be some compilers that have that feature). If it would be, there would be a problem with semaphores, since C11 only has mutexes and not semaphores as lock structures. How these two features would cooperate is not specified, yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very strange problem, and I just hope that I can clearly
This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL)
This problem is very strange and I'm hoping someone can help me. For the
I’ve got this strange problem whereby the content within a scroll viewer increases in
i have this strange problem with the PHP function CTYPE_ALNUM if i do: PHP:
Starting from Android 3.2 I have this strange problem. It's very easy to reproduce:
This is a strange problem I'm having, I've downloaded the WebSharingAppDemo sample from Microosfts
This is a really strange problem, that appears to be somewhat intermittent (although it
OK, this problem is strange and I want to know if I am accidentally
I've got this strange problem using py-amqp and the Flopsy module. I have written

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.