简体   繁体   English

线程-同步和休眠线程拒绝唤醒(LINUX)

[英]Thread - synchronizing and sleeping thread refuses to wake up (LINUX)

I'm developing an application For OpenSUSE 12.1. 我正在为OpenSUSE 12.1开发一个应用程序。

This application has a main thread and other two threads running instances of the same functions. 该应用程序有一个主线程和另外两个运行相同功能实例的线程。 I'm trying to use pthread_barrier to synchronize all threads but I'm having some problems: 我正在尝试使用pthread_barrier同步所有线程,但遇到一些问题:

  1. When I put the derived threads to sleep, they will never wake up for some reason. 当我使派生线程进入睡眠状态时,由于某种原因它们永远不会唤醒。
  2. (in the case when I remove the sleep from the other threads, throwing CPU usage to the sky) In some point all the threads reach pthread_barrier_wait() but none of them continues execution after that. (在我从其他线程中删除睡眠的情况下,将CPU使用率扔到了空中)在某些时候,所有线程都到达了pthread_barrier_wait()但是之后没有一个继续执行。

Here's some pseudo code trying to illustrate what I'm doing. 这是一些伪代码,试图说明我在做什么。

pthread_barrier_t barrier;
int main(void)
{
    pthread_barrier_init(&barrier, NULL , 3);
    pthread_create(&thread_id1, NULL,&thread_func, (void*) &params1);
    pthread_create(&thread_id2v, NULL,&thread_func, (void*) &params2);

    while(1)
    {
        doSomeWork();
        nanosleep(&t1, &t2);

        pthread_barrier_wait(&barrier);

        doSomeMoreWork();
   }
}

void *thread_func(void *params)
{
    init_thread(params);

    while(1)
    {
        nanosleep(&t1, &t2);
        doAnotherWork();

        pthread_barrier_wait(&barrier);
    }
}

I would suggest to use condition variables in order to synchronize threads. 我建议使用条件变量来同步线程。 Here some website about how to do it i hope it helps. 在这里一些有关如何做到这一点的网站,希望对您有所帮助。

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

I don't think it has to do with the barrier as you've presented it in the pseudocode. 我认为这与您在伪代码中介绍的障碍无关。 I'm making an assumption that your glibc is approximately the same as my machine. 我假设您的glibc与我的机器大致相同。 I compiled roughly your pseudo-code and it's running like I expect: the threads do some work, the main thread does some work, they all reach the barrier and then loop. 我粗略地编译了您的伪代码,它的运行与我预期的一样:线程完成了一些工作,主线程完成了一些工作,它们全部到达障碍然后循环。

Can you comment more about any other synchronization methods or what the work functions are? 您能否对其他同步方法或工作功能有更多评论?

This is the the example program I'm using: 这是我使用的示例程序:

#include <pthread.h>
#include <stdio.h>
#include <time.h>

struct timespec req = {1,0}; //{.tv_sec = 1, .tv_nsec = 0};
struct timespec rem = {0,0}; //{.tv_sec = 0, .tv_nsec = 0};

pthread_barrier_t barrier;

void *thread_func(void *params) {
   long int name;
   name = (long int)params;
   while(1) {
      printf("This is thread %ld\n", name);
      nanosleep(&req, &rem);

      pthread_barrier_wait(&barrier);

      printf("More work from %ld\n", name);
   }
}

int main(void)
{
   pthread_t th1, th2;

   pthread_barrier_init(&barrier, NULL , 3);
   pthread_create(&th1, NULL, &thread_func, (void*)1);
   pthread_create(&th2, NULL, &thread_func, (void*)2);

   while(1) {
      nanosleep(&req, &rem);
      printf("This is the parent\n\n");

      pthread_barrier_wait(&barrier);
   }
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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