簡體   English   中英

使用pthreads殺死線程 - C.

[英]Killing Threads with pthreads - C

我有一個C pthread程序,在main中創建N個線程來更新全局變量。 Main還在所有這些更新線程上調用pthread_join以等待它們完成。 我還有2個觀察者線程使用pthread條件變量來檢查全局變量是高於還是低於某些數字,如果是,它會殺死所有更新線程和其他觀察者線程。 但是,我在這最后一部分遇到了麻煩..殺死其他線程。 我的程序完成它應該做的但卻永遠不會完成...它只是卡住了。 在每個觀察者線程結束時調用exit(0)可以正常工作,但我覺得這個解決方案太懶了,我真的想學習如何從一個單獨的線程中殺死其他線程並返回main。

這是我的代碼:

#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void *update(void *i);
void *watchIncrease();
void *watchDecrease();

//init globals
double marketValue;
int numThreads;
double *stocks;
double ceiling;
double floor_;
int flag;

pthread_t *threads;
pthread_t watchIncreaseThread;
pthread_t watchDecreaseThread;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t threshold_ceiling;
pthread_cond_t threshold_floor_;

int main(int argc, char **argv){

    numThreads = atoi(argv[1]);
    int level = atoi(argv[2]);

    marketValue = 100 * numThreads;
    //initialize personal stocks for threads
    stocks = (double *) malloc(sizeof(double) * numThreads);
    int i;
    for(i = 0; i < numThreads; i++) stocks[i] = 100;
    //initialize floor/ceiling
    double percent = (double) level / 100;

    double cap = marketValue * percent;
    ceiling = marketValue + cap;
    floor_ = marketValue - cap;

    //seed rand()
    srand(time(NULL));
    //create threads
    pthread_cond_init(&threshold_ceiling,NULL);
    pthread_cond_init(&threshold_floor_,NULL);

    int rc = pthread_create(&watchIncreaseThread,NULL,watchIncrease,NULL);
    assert(rc == 0);
    rc = pthread_create(&watchDecreaseThread,NULL,watchDecrease,NULL);
    assert(rc == 0);

    threads = (pthread_t *)malloc(sizeof(pthread_t) * numThreads);
    assert(threads != NULL);
    for(i = 0; i < numThreads; i++){
        int rc = pthread_create(&threads[i],NULL,update,(void *)i);
        assert(rc == 0);
    }

    int j;
    for(j = 0; j < numThreads; j++){
        pthread_join(threads[j],NULL);
    }

    return 0;
}

void *update(void *i){

    int index = (int)i;
    double max = 2;
    double val;

    while(1){
        int rc = pthread_mutex_lock (&lock);
        assert(rc == 0);
        val = max * ((double)rand()/(double)RAND_MAX - 0.5);
        stocks[index] += val;

        marketValue += val;
        pthread_cond_signal (&threshold_ceiling);
        pthread_cond_signal (&threshold_floor_);
        pthread_mutex_unlock(&lock);

    }

}

void *watchIncrease(){

    int rc = pthread_mutex_lock(&lock);
    assert(rc == 0);
    while(marketValue < ceiling){
        pthread_cond_wait(&threshold_ceiling, &lock);
    }
    printf("Market Up to %.2f\n",marketValue);
    int i;
    double sum = 0;
    for(i = 0; i < numThreads; i++){
        sum += stocks[i];
    }
    printf("Total Market Price of %d stocks: %.2f\n",numThreads,sum);
    for(i = 0; i < numThreads; i++){
        rc = pthread_cancel(threads[i]);
        assert(rc == 0);
    }
    pthread_cancel(watchDecreaseThread);
    pthread_mutex_unlock(&lock);
    pthread_exit(NULL);

    //exit(0);
}

void *watchDecrease(){

    int rc = pthread_mutex_lock(&lock);
    assert(rc == 0);
    while(marketValue > floor_){
        pthread_cond_wait(&threshold_floor_, &lock);
    }
    printf("Market Down to %.2f\n",marketValue);
    int i;
    double sum = 0;
    for(i = 0; i < numThreads; i++){
        sum += stocks[i];
    }
    printf("Total Market Price of %d stocks: %.2f\n",numThreads,sum);
    for(i = 0; i < numThreads; i++){
        rc = pthread_cancel(threads[i]);
        assert(rc == 0);
    }
    pthread_cancel(watchIncreaseThread);
    pthread_mutex_unlock(&lock);
    pthread_exit(NULL);
    //exit(0);


}

不鼓勵pthread_cancel ,而是正確的方法是替換while(1){ by while(!killed[index]){ 然后,殺死你設置的線程killed[index] 如果你有C11編譯器,請使用http://en.cppreference.com/w/c/atomic/atomic_flag ,否則你可能需要一個pthread_mutex來保護被killed數組。

示例: https//gustedt.wordpress.com/2012/01/22/simple-c11-atomics-atomic_flag/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM