选择行为

发布于 2024-09-09 00:31:40 字数 138 浏览 8 评论 0原文

这可能是一个简单的问题,但我找不到明确的答案。我在 C 代码中有多个线程,其中一个使用 select 等待 n 秒。我的问题是,它是否会阻塞整个进程 n 秒(如 usleep),或者 select 是否仅阻塞调用线程(更像 nanosleep)。 感谢您的回答。

It could probably be a simple question but I couldn't find a clear answer for it. I have multiple threads in c code and one of them uses select to wait for n seconds. The question that I have is that does it blocks the entire process for n seconds (like usleep) or does select blocks only the calling thread (more like nanosleep).
Thanks for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦幻的心爱 2024-09-16 00:31:40

我见过几种实现,其中一个线程在 select 上阻塞,而其他线程继续处理 - 所以,是的,它只阻塞正在运行的线程。

(抱歉没有带任何参考资料)

I've seen several implementations in which one thread is blocking on select while other threads continue processing - so, yes, it only blocks the running thread.

(Sorry for not bringing any references)

岁月染过的梦 2024-09-16 00:31:40

select 的 POSIX 规范特别提到“thread”仅在一处,它讨论通过 pselect() 恢复调用线程的信号掩码。

与其他答案一样,我的经验也表明答案是肯定的,它只会阻塞调用线程。

The POSIX spec for select specifically mentions "thread" in only one place, where it talks about restoring the signal mask of the calling thread by pselect().

As with the other answers, my experience also says the answer is yes, it only blocks the calling thread.

ら栖息 2024-09-16 00:31:40

是的。一个草率但仍然相当结论性的测试。

#include <iostream>
#include <pthread.h>
#include <sys/time.h>

using namespace std;

pthread_mutex_t cout_mutex = PTHREAD_MUTEX_INITIALIZER;

void *task1(void *X)
{
   timeval t = {0, 100000};

    for (int i = 0; i < 10; ++i)
    {
        pthread_mutex_lock(&cout_mutex);
        cout << "Thread A going to sleep" << endl;
        pthread_mutex_unlock(&cout_mutex);

        select(0, NULL, NULL, NULL, &t);

        pthread_mutex_lock(&cout_mutex);
        cout << "Thread A awake" << endl;
        pthread_mutex_unlock(&cout_mutex);
    }

   return (NULL);
}


void *task2(void *X)
{
   pthread_mutex_lock(&cout_mutex);
   cout << "Thread B down for the long sleep" << endl;
   pthread_mutex_unlock(&cout_mutex);

   timeval t = {5, 0};
   select(0, NULL, NULL, NULL, &t);

   pthread_mutex_lock(&cout_mutex);
   cout << "Thread B glad to be awake" << endl;
   pthread_mutex_unlock(&cout_mutex);

   return (NULL);
}


int main(int argc, char *argv[])
{
  pthread_t ThreadA,ThreadB;

  pthread_create(&ThreadA,NULL,task1,NULL);
  pthread_create(&ThreadB,NULL,task2,NULL);

  pthread_join(ThreadA,NULL);
  pthread_join(ThreadB,NULL);

  return (0);
}  

Yes. A sloppy but still pretty conclusive test.

#include <iostream>
#include <pthread.h>
#include <sys/time.h>

using namespace std;

pthread_mutex_t cout_mutex = PTHREAD_MUTEX_INITIALIZER;

void *task1(void *X)
{
   timeval t = {0, 100000};

    for (int i = 0; i < 10; ++i)
    {
        pthread_mutex_lock(&cout_mutex);
        cout << "Thread A going to sleep" << endl;
        pthread_mutex_unlock(&cout_mutex);

        select(0, NULL, NULL, NULL, &t);

        pthread_mutex_lock(&cout_mutex);
        cout << "Thread A awake" << endl;
        pthread_mutex_unlock(&cout_mutex);
    }

   return (NULL);
}


void *task2(void *X)
{
   pthread_mutex_lock(&cout_mutex);
   cout << "Thread B down for the long sleep" << endl;
   pthread_mutex_unlock(&cout_mutex);

   timeval t = {5, 0};
   select(0, NULL, NULL, NULL, &t);

   pthread_mutex_lock(&cout_mutex);
   cout << "Thread B glad to be awake" << endl;
   pthread_mutex_unlock(&cout_mutex);

   return (NULL);
}


int main(int argc, char *argv[])
{
  pthread_t ThreadA,ThreadB;

  pthread_create(&ThreadA,NULL,task1,NULL);
  pthread_create(&ThreadB,NULL,task2,NULL);

  pthread_join(ThreadA,NULL);
  pthread_join(ThreadB,NULL);

  return (0);
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文