C++;-Pthread,与一个函数相关的多个线程,具有无限循环 我是C++新手,尝试使用pTrand和NcRISS库。我正在制作一个程序,在终端屏幕上显示飞行的球。我创建了一个Ball类:
在文件Ball.h声明中,在Ball.c实现中。 B.h:C++;-Pthread,与一个函数相关的多个线程,具有无限循环 我是C++新手,尝试使用pTrand和NcRISS库。我正在制作一个程序,在终端屏幕上显示飞行的球。我创建了一个Ball类:,c++,multithreading,loops,pthreads,infinite-loop,C++,Multithreading,Loops,Pthreads,Infinite Loop,在文件Ball.h声明中,在Ball.c实现中。 B.h: class Ball { public: //ATTRIBUTES char sign; int x, y, direction, speed, color; int width, height; //area, field size //CONSTRUCTORS Ball(); ~Ball(); Ball(int d, int s, int yy, int
class Ball {
public:
//ATTRIBUTES
char sign;
int x, y, direction, speed, color;
int width, height; //area, field size
//CONSTRUCTORS
Ball();
~Ball();
Ball(int d, int s, int yy, int xx, int c, int fH, int fW);
//s - start direction, v - speed, x,y- position, c-color
//GETTERS
//METHODS
void setAreaSize(int, int);
void moveBall(void);
};
在另一个文件中是我的程序,它使用这个类:
.../*including some libs */...
.../*declaring some attributes */...
Ball *balls;
pthread_t *threads;
int howManyBalls;
int i; //helper for loops
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
struct timespec delay = { //our speed or rather delay ;)
1,
0
};
/* Function declarations */
void initBalls(void);
void initThreads(void);
void *threadBallFunction(void *arg);
//MAIN FUNCTION ----------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
... /*some code*/...
initBalls();
initThreads();
... /* some code */
return 0;
}
//FUNCTIONS IMPLEMENTATIONS ----------------------------------------------------------------------------------
/* INIT BALLS */
void initBalls(void){
balls = new Ball[howManyBalls]; //creating our balls array with appropriate size
int ballY, ballX, ballDirection, ballColor;
srand(time(0)); //resetting the random number generator
for(i=0;i<howManyBalls;i++){
ballY = (rand()%(frameWidth-1))-i;
ballX = (rand()%(frameHeight-1))-i;
ballDirection = rand()%8+1;
ballColor = rand()%7+1;
balls[i] = Ball(ballDirection,2,ballX,ballY,ballColor, frameHeight, frameWidth);
}
}
void *threadBallFunction(void *threadIndex) {
do{
/* WHAT HERE ?? */
}
while(1);
}
/* INIT THREADS */
void initThreads(void){
threads = new pthread_t[howManyBalls];
void *exit_status;
int threadIdx;
for (threadIdx = 0; threadIdx < howManyBalls; threadIdx++) {
pthread_create(&threads[threadIdx], NULL, threadBallFunction, &threadIdx);
pthread_join(threads[threadIdx], &exit_status);
}
}
../*包括一些lib*/。。。
…/*声明某些属性*/。。。
球*球;
pthread_t*线程;
int有多少个球;
int i//循环的辅助对象
pthread_mutex_t mutex1=pthread_mutex_初始值设定项;
struct timespec delay={//我们的速度或者更确切地说是延迟;)
1.
0
};
/*函数声明*/
空心球(空心球);
void(void);
void*threadball函数(void*arg);
//主要功能----------------------------------------------------------------------------------------------
int main(int argc,char*argv[])
{
…/*一些代码*/。。。
initBalls();
initThreads();
…/*一些代码*/
返回0;
}
//函数实现----------------------------------------------------------------------------------
/*初始球*/
void初始化球(void){
balls=新球[howManyBalls];//创建大小合适的球数组
int-ballY、ballX、ballDirection、ballColor;
srand(时间(0));//重置随机数生成器
因为(i=0;i互斥体也很容易理解和使用,
假设你有一门课
class A{
static int counter;
public:
static void IncCounterBy(int increment){
counter += increment;
}
};
假设有100个线程都在访问该函数,在任何时候,2个或更多线程可以同时访问同一函数,每个线程将复制原始值,然后递增该值
所以如果A::counter=3
螺纹1-螺纹接头(2)
螺纹2-螺纹接头(5)
螺纹3-螺纹接头(1)
这将以=>>A::counter=3结束
如何使用Boost::Mutex
class A{
static int counter;
boost::mutex Guard;
public:
static void IncCounterBy(int increment){
Guard.lock();
counter += increment;
Guard.unlock();
//Or you can use "boost::mutex::scoped_lock lock(Guard);" to guard a function
};
您的具体问题:“是否可以在此函数中使用同步线程的条件变量方法”
对于你真正要求的行为,我仍然不清楚,所以让我们考虑两种情况:
案例1:您希望线程自动运行,并且每个线程在自己的计划周期内独立更新计数器:
答案是,对于此应用程序,条件变量不是正确的同步方法。条件变量实际上是针对生产者-消费者设计模式的。特别是:
*如果cond上当前没有阻塞的线程,pthread_cond_broadcast()和pthread_cond_signal()函数将无效*
您要求的是对共享状态对象(您的计数器)的自主处理。在这种情况下,Ahmed是正确的,因为共享状态应该简单地由互斥来保护,以防止同时访问
案例2:您希望每个线程执行某种循环处理,其中一个线程运行,等待1秒,然后根据其参数量更新计数器。然后下一个线程运行,等待1秒,并根据其不同的参数量更新计数器。依此类推,直到您循环回第一个线程。
此更多情况与您的示例计数器增量流相匹配。在这种情况下,只要线程数为2,就可以使用条件变量,并确保以确定的方式启动它们。如果线程数超过2,则除非它们具有不同的优先级,否则无法保证运行顺序。也可以像这样在循环中启动它们es不保证先执行哪个
在这种情况下,当所有其他线程都在等待时,一次只有一个线程在工作。这不是多线程解决方案的好选择。更好的模式是一个线程和一个工作队列。我的意思是创建一个包含各种所需增量的循环队列,并让一个线程在循环中运行,在循环中等待1秒,pull从队列中读取下一个增量,并递增计数器,递增队列指针,然后重复。web上有很多关于Pthreads的教程,请尝试阅读其中一个,然后返回具体问题。但是对于初学者,您的inithreads
函数创建线程,然后等待它完成,然后创建另一个线程,等待它完成,然后另一个线程。您要创建它们,然后等待它们。您也应该考虑使用,它更容易使用,在此之上,我相当倾向于认为<代码> NcRESs> <代码>不是特别线程安全的,所以这一努力可能会有点有趣…谢谢您的评论。!我读了很多教程,但仍能找到(仍在寻找)任何与我的问题相匹配的-多线程+所有线程的一个线程函数+无限循环。谢谢,但我还有一个问题,请看编辑的第一篇帖子:)首先,我对迟到1年的回复感到非常抱歉:看,你可以定义一个计数器变量m_currIdx=0;如果m_currIdx==我线程的idx(线程#0),那么让每个线程执行以下操作-让它执行它的函数-将m_currIdx增加1-等待0.1s(或其他什么)P.s:不要让它像(while(true))那样循环它会消耗处理器的电源:)