简体   繁体   中英

Can't find the deadlock

I got this question in a job interview and for the life of me I couldn't find the answer (and they don't tell you the answer at the end since it's a written test):

int thread2_finished = 0;

void t1() {
    printf("t1 is running\n");
    while (!thread2_finished) { usleep(50); }
    printf("t2 is doing stuff\n");    
}


void t2() {
    printf("t2 is running\n");
    sleep(5);
    printf("t2 woke up\n");    
    thread2_finished = 1;
    printf("t2 finished\n");
}

What we know is that most of the times it works - but sometimes thread1 never exists (doesn't print the last message) while thread2 does print all his messages - How is that possible?

I'm guessing i'm missing something basic here but the only thing I could think about it that the problem is with the cache - like T1 loads the value (0) and cache it, then immediately T2 changes the value to 1, but for some odd reason T1 will keep using the old cached value but that seems weird to me.

像这样写的代码似乎是正确的(并且是一种逻辑方式),但是如果您使用真实环境,它将无法正常工作,此修复将是volatile关键字,但是原因有点复杂,还因为此关键字的行为更改每种语言/编译器,此处提供正确答案

You have no guarantee that t1 has started when t2 runs. sleep is not a valid substitute for proper thread synchronization.

One could also argue that thread2_finished should be volatile. In reality that shouldn't really matter since the compiler doesn't know what usleep does and therefore can't assume that usleep won't change global variables. On the other hand, on many architectures you need to have better synchronization than just updating a global variable. On some the other thread (if running on a different cpu) might not see the updated value of the global variable for a long time, on others (if not cache coherent) it might never see it. The locking primitives in the operating system should provide enough logic to make sure that side effects from one thread are seen by other threads.

In short - even though this could work in most cases, never do this. Use proper locking, condition variables and semaphores.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM