如何在C++中挂起和恢复 POSIX 线程?

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

本文关键字:恢复 POSIX 线程 挂起 C++      更新时间:2023-10-16

当我开始知道突然创建和终止线程时 每次都使用 pthread_kill(( 不是一个好方法,所以我要去 使用thread1.suspend()thread1.resume(),在需要时。如何做/实施?

以下面的 LED 闪烁代码作为参考。在thread1.start()期间,使用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("suspendedn");
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("Resumedn");
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: onn", fd);
else
printf("%d: offn", 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;
}