<div dir="ltr"><div class="gmail_extra"><br></div><div class="gmail_extra">Jan Stancek <span dir="ltr"><<a href="mailto:jstancek@redhat.com" target="_blank">jstancek@redhat.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-HOEnZb"><div class="gmail-h5"><span class="gmail-"></span><br><span class="gmail-"></span></div></div><span class="gmail-">
> ​Maybe add:<br>
>     SAFE_PFUNC(pthread_mutex_<wbr>unlock(&mutex));<br>
> here too?​<br>
<br>
</span>There are no users at this point. Is there an issue having it locked at exit?<br>
<span class="gmail-"></span><span class="gmail-HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div style="font-size:small" class="gmail_default">​Not clear too. But after thinking over, maybe it's not a good idea to use pthread_mutex_lock() in this situation.</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">The reasons are:</div><div style="font-size:small" class="gmail_default">1. to avoid dead lock, it's not suggest to lock a mutex twice in one thread</div><div style="font-size:small" class="gmail_default">2. it's not suggest to lock mutex in thread_A but do unlock in thread_B<br></div></div></div><div class="gmail_extra"><br></div><div class="gmail_extra"><div style="font-size:small" class="gmail_default">​So what about using semaphore:</div><div style="font-size:small" class="gmail_default">-----------------------------</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">static sem_t sem_syn;<br><br>static void *thread_func(void *unused)<br>{</div><div style="font-size:small" class="gmail_default">...<br></div><div style="font-size:small" class="gmail_default">       sem_post(&sem_syn);<br><br>       while (waited_for_cancel_ms < TIMEOUT_MS) {<br>               usleep(SLEEP_MS*1000);<br>               waited_for_cancel_ms += SLEEP_MS;<br>       }<br>...<br>}</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">int main(void)<br>{<br>       sem_init(&sem_syn, 0, 0); <br><br>       SAFE_PFUNC(pthread_create(&th, NULL, thread_func, NULL));<br><br>       /* wait for thread to start */<br>       sem_wait(&sem_syn);<br>       SAFE_PFUNC(pthread_cancel(th));<br>...<br>}<br><span style="color:rgb(128,128,128);font-family:Consolas,"Courier New",Courier,mono,serif;font-size:12px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(248,248,248);text-decoration-style:initial;text-decoration-color:initial;display:inline;float:none"></span></div></div><div class="gmail_extra"><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div>
</div></div>