C++ 如何在C++;?
正如我所知,创建和终止线程是突然的 每次使用pthread_kill()都不是一个好方法,所以我要 使用C++ 如何在C++;?,c++,multithreading,pthreads,resume,C++,Multithreading,Pthreads,Resume,正如我所知,创建和终止线程是突然的 每次使用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
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("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;
}
请找个人帮我一下好吗?在对上述代码稍作修改后,它似乎起到了作用。感谢guy指出上述代码中的问题,更改如下
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#在1上定义
#定义0
无效gpio_写入(int-fd,int-value);
无效*led闪烁(无效*参数);
类PThread{
公众:
pthread_t threadID;
挥发性int悬浮;
int-fd;
pthread_mutex_t lock;
PThread(intfd1)
{
这->fd=fd1;
this->suspended=1;//初始状态:暂停闪烁直到恢复调用
pthread\u mutex\u init(&this->lock,NULL);
pthread_创建(&this->threadID,NULL,led_闪存,(void*)this);
}
~PThread()
{
pthread_join(this->threadID,NULL);
pthread\u mutex\u destroy(&this->lock);
}
void suspendBlink(){
pthread_mutex_lock(&this->lock);
此->暂停=1;
pthread_mutex_unlock(&this->lock);
}
void resumebink(){
pthread_mutex_lock(&this->lock);
此->暂停=0;
pthread_mutex_unlock(&this->lock);
}
};
无效gpio_写入(int-fd,int-value)
{
如果(值!=0)
printf(“%d:on\n”,fd);
其他的
printf(“%d:off\n”,fd);
}
无效*发光二极管闪烁(无效*参数)
{
PThread*pt=(PThread*)参数;
int-fd=pt->fd;
而(1)
{
如果(!(pt->暂停))
{
gpio_写入(fd,on);
usleep(1);
gpio_写入(fd,关闭);
usleep(1);
}
}
返回NULL;
}
int main()
{
//使用初始状态创建线程:暂停/停止闪烁,直到恢复调用
类PThread redLED(1);
类PThread amberLED(2);
类PThread绿色发光二极管(3);
//开始闪烁
redLED.resumebink();
amberLED.resumebink();
greenLED.resumebink();
睡眠(5);
//暂停/停止闪烁
amberLED.suspendBlink();
睡眠(5);
redLED.suspendBlink();
睡眠(5);
amberLED.suspendBlink();
睡眠(5);
redLED.resumebink();
pthread_exit(NULL);
返回0;
}
Kill/suspend/resume所有坏消息。尽量不要从用户代码中执行这些操作。尽量安排创建线程的唯一时间是在进程启动时,杀死线程的唯一时间是在进程终止时。pt->suspended既不是易失性的,也不是原子性的。因此,我猜测编译器在(pt->suspended==false){…}
时优化了,而在中(pt->suspended==false){再次:…;再次转到;}
。如果从信号处理程序更改变量,Volatile将是正确的选择。为了更好地使用多线程,请使用原子。“调用thread1.suspend()没有任何效果。”阅读代码时,我希望红色指示灯继续闪烁(请参阅前面的注释),而主线程只是停留在那里等待恢复条件。对吗?您是希望挂起阻止主线程还是希望led_闪烁停止直到恢复?当前led闪存只会退出线程,如果它看到pt->suspended==true
,就不会有任何恢复。您可以用一个POSIXsem\u t
替换pthread\u mutex\u t
和pthread\u cond\u t
。然后你的suspend()
变成了一个简单的sem\u wait()
,而resume()
变成了sem\u post()
。因为您真正做的是阻塞。代码天生就是C++代码,因为<代码>类pTrx{}};代码>及其内容无效。我已正确地重新标记和重新命名。