如何在线程之间进行通信和等效的waitforsingleobject()
How to communicate between threads and equivalent for Waitforsingleobject()
我只是在学校开始C ,我正在尝试同时进行Linux和Windows。所以这是我的代码和练习。问题是我不知道如何为线程再次运行,每当我运行时,我都不会得到正确的字符。
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
bool t;
#define NUM_THREADS 3
void *Saisie(void *PT)
{
char TT[150];
cout << "Entrez une chaîne de caractères :" <<endl;
cin >> TT;
t = true;
pthread_exit(NULL);
}
void *Visualisation(void *PT)
{
cout<<"La chaine transmise est :" << &*(char*)PT <<endl;
pthread_exit(NULL);
}
int main ()
{
pthread_t TH1;
pthread_t TH2;
char TT[150];
t = false;
while (t == false){
pthread_create(&TH1,NULL,Saisie,&TT); // Création du thread TH1
pthread_join(TH1,NULL);
}
if (t == true){
pthread_create(&TH2,NULL,Visualisation,&TT); // Création du thread TH2
pthread_join(TH2,NULL);
}
cout << "nFin du programme – saisir une lettre pour fermern";
cin >> TT;
}
我的输出如下:
entrez unechaînedeCaractères:测试La Chaine Transmise EST: 250 365 277 357 376
非常感谢!
同步线程并使用C 标准提供的库。这样,您的代码是便携的。
使用pthread库不比上述选项更可取。Windows有一个Pthread实现,但是为什么只能使用C 标准提供的内容就可以遇到麻烦。
要回答您的特定查询,您可以使用睡眠原始查询,或者如果需要等待条件,则使用条件变量
在这里看看
相关文章:
- 没有找到相关文章