繁体   English   中英

如何在C ++中暂停和恢复POSIX线程?

[英]How to suspend and resume a POSIX thread in C++?

我知道每次都使用pthread_kill()突然创建和终止线程不是一个好方法,所以我将在需要时使用使用thread1.suspend()thread1.resume()的线程的suspend和resume方法。 。 怎么做/实现呢?

以下面的LED闪烁代码为参考。 thread1.start()期间,创建带有suspended = false;线程suspended = false; 由于卡在while循环中而继续。 调用thread1.suspend()无效。

#define on 1
#define off 0
void gpio_write(int fd, int value);
void* led_Flash(void* args);


class PThread {
    public:

    pthread_t threadID;
    bool suspended;
    int fd;
    pthread_mutex_t m_SuspendMutex;
    pthread_cond_t m_ResumeCond;

    void start() {
        suspended = false;
        pthread_create(&threadID, NULL, led_Flash, (void*)this );
    }

    PThread(int fd1) { this->fd=fd1; }
    ~PThread() { }

    void suspend() {
        pthread_mutex_lock(&m_SuspendMutex);
        suspended = true;
        printf("suspended\n");
        do {
            pthread_cond_wait(&m_ResumeCond, &m_SuspendMutex);
        } while (suspended);
        pthread_mutex_unlock(&m_SuspendMutex);
    }

    void resume() {
        /* The shared state 'suspended' must be updated with the mutex held. */
        pthread_mutex_lock(&m_SuspendMutex);
        suspended = false;
        printf("Resumed\n");
        pthread_cond_signal(&m_ResumeCond);
        pthread_mutex_unlock(&m_SuspendMutex);
    }
};

void* led_Flash(void* args)
{  
    PThread* pt= (PThread*) args;
    int ret=0;
    int fd= pt->fd;

       while(pt->suspended == false)
        {
        gpio_write(fd,on);
        usleep(1); 
        gpio_write(fd,off);
        usleep(1); 
        }   


return NULL;
}


int main()
{
    int fd1=1,fd2=2, fd3=3;

    class PThread redLED(fd1);
    class PThread amberLED(fd2);
    class PThread greenLED(fd3);

    redLED.start();
    amberLED.start();
    greenLED.start();

    sleep(1);
    redLED.suspend();

return 0;
}

请问有什么身体可以帮助我吗?

对上述代码进行一些修改后,它似乎可以正常工作。 谢谢您指出以上代码中的问题,更改如下。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<iostream>
#define on 1
#define off 0
void gpio_write(int fd, int value);
void* led_Flash(void* args);


class PThread {
    public:

    pthread_t threadID;
    volatile int suspended;
    int fd;
    pthread_mutex_t lock;
    PThread(int fd1)
   {   
        this->fd=fd1; 
        this->suspended =1;  //Initial state: suspend blinking untill resume call 
        pthread_mutex_init(&this->lock,NULL); 
        pthread_create(&this->threadID, NULL, led_Flash, (void*)this );

    }
    ~PThread() 
    { 
      pthread_join(this->threadID , NULL);
      pthread_mutex_destroy(&this->lock);
    }

    void suspendBlink() {
        pthread_mutex_lock(&this->lock);
        this->suspended = 1;
        pthread_mutex_unlock(&this->lock);
    }

    void resumeBlink() {
        pthread_mutex_lock(&this->lock);
        this->suspended = 0;
        pthread_mutex_unlock(&this->lock);
    }
};

void gpio_write(int fd, int value)
{
if(value!=0)
 printf("%d: on\n", fd);
else
 printf("%d: off\n", fd);
}


void* led_Flash(void* args)
{  
    PThread* pt= (PThread*) args;
    int fd= pt->fd;

    while(1)
    {
    if(!(pt->suspended))
        {
        gpio_write(fd,on);
        usleep(1); 
        gpio_write(fd,off);
        usleep(1);
        }
   }


return NULL;
}


int main()
{
   //Create threads with Initial state: suspend/stop blinking untill resume call 
    class PThread redLED(1);
    class PThread amberLED(2);
    class PThread greenLED(3);

    // Start blinking
    redLED.resumeBlink();
    amberLED.resumeBlink();
    greenLED.resumeBlink();
    sleep(5);

    // suspend/stop blinking
    amberLED.suspendBlink();

    sleep(5);

    redLED.suspendBlink();

    sleep(5);

    amberLED.suspendBlink();

    sleep(5);     

    redLED.resumeBlink();  


pthread_exit(NULL);

return 0;
}

暂无
暂无

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

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