我必须在具有这种条件的线程内进行循环:
while(running)
(running
是设置为true
)的哨兵布尔变量
但是,当我点击按钮(Qt -> QPushButton
)时,它会调用一个函数(pause()
)并且running
必须设置为false
。
我的主题代码如下:
DWORD CServeurTcp::ClientThread(SOCKET soc, BluetoothThread *_bt)
{
ifstream fichier("test.txt", ios::in);
string ligne, mess_crypt, donnees, cut;
int compteurReception = 0;
if(fichier)
{
while(running)
{
if((_bt->getReception() != 0)&&(compteurReception != _bt->getReception()))
{
compteurReception = _bt->getReception();
getline(fichier, ligne);
cut = ligne.substr (0,12);
cryp->vigenere_crypter(cut,mess_crypt,"VIGE");
donnees = salle + " - " + mess_crypt;
emis = send(soc, donnees.c_str(), strlen(donnees.c_str()), 0);
if(emis == SOCKET_ERROR)
Affiche_Erreurs( WSAGetLastError() );
else
cout << "Nombre de caracteres envoyes: " << strlen(donnees.c_str()) << endl;
}
Sleep(1000);
}
fichier.close();
}
else
cout << endl << "Impossible d'ouvrir le fichier !" << endl;
ExitThread(0);
return 0;
}
这是班级:
class CServeurTcp;
struct thread_param_client{
CServeurTcp* cli;
SOCKET soc;
BluetoothThread *_bt;
};
class CServeurTcp
{
private:
SOCKET client;
int erreur, emis, recus;
bool running;
DWORD ClientThread(SOCKET, BluetoothThread*);
string salle;
Vigenere *cryp;
BluetoothThread *_bth;
public:
CServeurTcp(string, unsigned short, string, BluetoothThread*);
~CServeurTcp();
int pause();
static DWORD WINAPI ThreadLauncher_client(void *p)
{
struct thread_param_client *Obj = reinterpret_cast<struct thread_param_client*>(p);
CServeurTcp *s = Obj->cli;
return s->ClientThread(Obj->soc,Obj->_bt);
}
};
pause()
方法必须将running
设置为false,然后停止线程的循环,但它没有工作......
我怎么能做到这一点?
(当我调用pause()
时,关闭线程的循环以关闭它)
由于
答案 0 :(得分:4)
jeffs@jeff-desktop:~/skyset$ python3 attributes.py
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'm', 'q']
jeffs@jeff-desktop:~/skyset$
不是原子的,也不是用互斥锁保护的,所以从多个线程访问它是不安全的,你的程序有数据竞争 - 因此它的行为是未定义的,你可以不假设它的结果。