Tag: pthreads

完成后关闭线程

你什么时候关闭线程怎么样? 比如确保什么都没有打开或跑步? 到目前为止我知道如何打开它,但是……不是如何正确地关闭它 int iret1; pthread_t thread1; char *message1; void *multithreading1( void *ptr ) { while (1) { // Our function here } } int main (int argc, char * const argv[]) { if( (iret1=pthread_create( &thread1, NULL, multithreading1, (void*) message1)) ) { printf(“Thread creation failed: %d\n”, iret1); } return 0; }

发信号通知条件变量(pthreads)

假设某些条件变量“cond”与互斥变量“mutex”相关联。 如果线程在调用pthread_cond_wait(&cond,&mutex)后在cond上hibernate,并且另一个已锁定mutex锁的线程完成,那么该线程在调用pthread_mutex_unlock(&mutex)之前或之后是否调用pthread_cond_signal(&cond)是否重要? 如果调用pthread_cond_signal(&cond) ,它是否甚至需要解锁互斥锁,因为睡眠线程无论如何都会获取互斥锁? 编辑:根据https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview ,“调用pthread_cond_signal()后未能解锁互斥锁可能不允许匹配的pthread_cond_wait()例程完成(它将保持阻塞状态)。 “ 我想那时,解锁,也许只是之后才需要。

无法设置Pthread优先级

我无法使用pthread_attr_setschedparam()设置Pthread优先级。 我试图解决这个问题,但无法解决。 我还咨询了我的教科书,该教科书也使用相同的function。 我从书中复制了这段代码。 你能告诉我如何设置线程优先级吗? 这是代码: void *Func(void *arg); int main() { pthread_t tid[5]; pthread_attr_t *tattr; struct sched_param param; int pr,error,i; do { if( (tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)) )==NULL) { printf(“Couldn’t allocate memory for attribute object\n”); } }while(tattr==NULL); if(error=pthread_attr_init(tattr)) { fprintf(stderr,”Attribute initialization failed with error %s\n”,strerror(error)); } for(i=0;i<5;i++) { //scanf("%d",&pr); error = pthread_attr_getschedparam(tattr,&param); if(error!=0) { printf("failed to […]

2+线程写/读变量的真正危险

同时读/写单个变量的真正危险是什么? 如果我使用一个线程来写一个变量而另一个线程用来读取while循环中的变量,并且如果在写入时读取变量并且使用旧值没有危险,那么还有什么危险呢? 同时读/写是否会导致线程崩溃或者在发生精确的同时读/写时在低电平上发生什么?

使用条件变量的Pthread程序死锁

该计划正在努力实现的目标: 该程序应该同步“访客”和“汽车”的几个线程。 游客们随意漫步,直到他们决定乘车。 如果他们是第一次乘坐汽车并且有车可以乘坐,那么他们必须等到他们第一次排队或者车回来。 如果没有游客排队,那么汽车会等待,直到游客想要乘车。 更多背景信息: 我使用条件变量重新构建我的线程同步程序,如此处接受的答案中所建议的那样。 我知道我在正确的轨道上,但由于某些原因,我的程序仍然陷入僵局,对于我的生活,我无法弄清楚为什么。 除非我给你代码,否则我不知道你怎么能帮助我,所以这里是: 问题: 1.)一小段后死锁 2.)有时候一位游客首先排队买车,但从不上车。 解决方案: 我的代码中有太多的错误…我想我会修复一个,我经常(不经意间)引入另一个。 我一直在删除程序的function,直到我消除了所有的错误,然后以一种不会使我的程序死锁的方式逐个构建function。 谢谢大家的建议。

C语言中的线程间通信

作为我项目的一部分,我必须使用线程修改数值积分算法。 这大致是传统顺序方法中发生的情况。 void Controller(struct DE) { //initialization step for(;;) //till the entire range has not been covered… { //compute the next time-point solution using the previously known solutions. NIiter(); //then check if the result is acceptable or not and take the necessary steps… } } 现在这就是我打算做的…… void Controller(struct DE) { //initialization step for(;;) //till the […]

Semaphore与multithreading中的条件变量?

问题 :我必须增加x1和x2变量,这应该由不同的线程完成,并且在两个变量的上一个增量都没有完成之前,不应该调用两个变量的下一个增量。 问题: x1 = 0; x2 = 0; x1++; and x2++; should run parallel on different threads and wait for printing. should print: x1 = 1 and x2 = 1 x1++; and x2++; should run parallel on different threads and wait for printing. should print: x1 = 2 and x2 = 2 x1++; and […]

对pthread_create()中的参数感到困惑

我的问题:为什么不直接传递&i作为pthread_create()的最后一个参数? 相反,他创建一个数组来保持相同的东西…. #define THREAD_CT 2 /* bump this up a few numbers if you like */ void *print_stuff(void *ptr) { int i, id= * (int *) ptr; for (i= 0; i < 5; i++) { printf("Thread %d, loop %d.\n", id, i); sleep(rand() % 2); /* sleep 0 or 1 seconds */ } printf("Thread %d exiting.\n", […]

pthread_create:按值传递参数

我想知道为什么我们不能通过值将对象传递给我们创建线程的函数。 它背后有合理的原因吗? 如果语言允许通过值传递会有害吗?

在这种情况下我真的需要互斥锁吗?

考虑我们有三个线程, bool status_flag[500]数组,以及如下工作情况: 两个线程只在不同索引处写入status_flag数组。 而第三个线程只读取任何索引。 所有三个线程写在不同的索引。 虽然所有三个线程都在读取任何索引。 在写入操作中,我们只是设置标志永远不会重置它。 status_flag [i] = true; 在阅读操作中,我们正在做类似的事情: for(;;){ //spinning to get flag true if(status_flag [i] == true){ //do_something ; break; } } 如果编译器优化(分支预测)代码会发生什么? 我已经阅读了很多关于锁的内容,但仍然对结论结果感到困惑。 请帮我总结一下。