如何使用它来杀死线程数组?

这段代码工作正常,但如何使用它来杀死剩余线程数组?

#include #include #include void *print1(void *tid) { pthread_t *td= tid; pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&lock1); printf("1"); printf("2"); printf("3"); printf("4\n"); printf("Coming out of thread1 \n"); sleep(2); pthread_mutex_unlock(&lock1); pthread_kill(*td,SIGKILL);//killing remaining all threads return NULL; } void *print2(void *arg) { pthread_mutex_t *lock = arg; pthread_mutex_lock(lock); sleep(5); printf("5"); sleep(5); printf("6"); sleep(5); printf("7"); sleep(5); printf("8\n"); fflush(stdout); pthread_mutex_unlock(lock); return NULL; } int main() { int s; pthread_t tid1, tid2; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; printf("creating Thread 1and 2 \n"); sleep(2); pthread_create(&tid1, NULL, print1,&tid2); pthread_create(&tid2, NULL, print2,&lock); printf("Running Thread 1\n"); sleep(2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; } 

评论:请删除此内容并添加一些有关代码的额外信息。 编辑器不允许我编辑代码。

 here is an edited version of the code along with some commentary #include #include void *print1(void *tid) { // should be in global space, so no need to pass pthread_t *td= tid; // this is a whole new mutex, //should be in global space so other threads can access it pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER; // why bother with the mutex, it does nothing useful pthread_mutex_lock(&lock1); printf("1"); printf("2"); printf("3"); printf("4\n"); printf("Coming out of thread1 \n"); sleep(2); pthread_mutex_unlock(&lock1); pthread_kill(*td,SIGKILL);//killing remaining all threads return NULL; // this exit is not correct, it should be this call: // void pthread_exit(void *rval_ptr); } // end function: print1 void *print2(void *arg) { // this should be in global memory so all threads using same mutex pthread_mutex_t *lock = arg; pthread_mutex_lock(lock); sleep(5); printf("5"); sleep(5); printf("6"); sleep(5); printf("7"); sleep(5); printf("8\n"); fflush(stdout); pthread_mutex_unlock(lock); // this exit is not correct, it should be this call: // void pthread_exit(void *rval_ptr); return NULL; } // end function: print2 int main() { int s; // this should be in global memory // so no need to pass to threads pthread_t tid1, tid2; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; printf("creating Thread 1and 2 \n"); // why bother to sleep here? sleep(2); // in these calls, the last parm should be NULL // and the related data should be in global memory pthread_create(&tid1, NULL, print1,&tid2); pthread_create(&tid2, NULL, print2,&lock); // we are still in main, so this printf is misleading printf("Running Thread 1\n"); // no need to sleep() // as the pthread_join calls will wait for the related thread to exit sleep(2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; } // end function: main 

随你怎么便。 没有“一种正确的方法”来做到这一点。

可能最简单的方法是通过调用使用pthread_cond_timedwait的函数和导致线程调用pthread_exit的谓词来替换对sleep的调用。

在伪代码中,使用以下逻辑替换对sleep调用:

  1. 计算睡觉的时间,直到。

  2. 锁定互斥锁。

  3. 检查谓词是否设置为exit,如果是,则调用pthread_exit

  4. 调用pthread_cond_timedwait

  5. 检查谓词是否设置为exit,如果是,则调用pthread_exit

  6. 检查时间是否到期,如果没有,请停止4。

要终止线程,请执行以下操作:

  1. 锁定互斥锁。

  2. 将谓词设置为退出。

  3. 调用pthread_cond_broadcast

  4. 释放互斥锁。

  5. 在线程上调用pthread_join ,等待它们终止。