如何在一个线程中杀死另一个线程

发布于 2024-10-17 12:35:34 字数 2033 浏览 8 评论 0原文

实际上,主要场景是:从主线程有两个线程正在运行。通过使用条件变量,两个线程将运行和睡眠,然后它将返回主线程。我的意思是我不想要不同的输出模式。只有一种模式:from main->thread1->thread2->main。 我写了一个C线程的代码。它有时显示我想要的结果,有时不显示。例如,输出是:

I am in thread 1 
before conditional wait
I am in thread 2
before conditional release
i am again in thread 2
i am again in thread 1
main exits here

问题是有时“main exits here”不执行。请帮助我。需要注意的是我不能使用 pthread_join()。下面给出了我的代码

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;

pthread_mutex_t mLock;
pthread_cond_t mCondition;

void initialize()
{
      pthread_mutex_init(&gLock, NULL);
      pthread_cond_init (&gCondition, NULL);
      pthread_mutex_init(&mLock, NULL);
      pthread_cond_init (&mCondition, NULL);

      return;
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    pthread_cond_wait(&gCondition,&gLock);
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
        pthread_t thread1;
        pthread_t thread2;

        char * msg1="I am in thread 1";
        char * msg2="I am in thread 2";
        initialize();

        pthread_create(&thread1,NULL,threadOne,(void*) msg1);
        pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

        pthread_mutex_lock(&mLock);
        pthread_cond_wait(&mCondition,&mLock);
        pthread_mutex_unlock(&mLock);

        printf("main exits here");

        return 0;
}

Actually,the main scenerio is that : from main thread there are two thread running.By using conditional variable,two threads will be running and sleeping and then it will return to main thread.I mean I dont want different output pattern.just one pattern:from main->thread1->thread2->main.
I have written a code for C thread.It shows the result I want sometimes and sometimes not.as for example,the output is:

I am in thread 1 
before conditional wait
I am in thread 2
before conditional release
i am again in thread 2
i am again in thread 1
main exits here

The problem is sometimes "main exits here" does not execute.Please help me.It is to be noted that I cant use pthread_join().my code is given below

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;

pthread_mutex_t mLock;
pthread_cond_t mCondition;

void initialize()
{
      pthread_mutex_init(&gLock, NULL);
      pthread_cond_init (&gCondition, NULL);
      pthread_mutex_init(&mLock, NULL);
      pthread_cond_init (&mCondition, NULL);

      return;
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    pthread_cond_wait(&gCondition,&gLock);
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
        pthread_t thread1;
        pthread_t thread2;

        char * msg1="I am in thread 1";
        char * msg2="I am in thread 2";
        initialize();

        pthread_create(&thread1,NULL,threadOne,(void*) msg1);
        pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

        pthread_mutex_lock(&mLock);
        pthread_cond_wait(&mCondition,&mLock);
        pthread_mutex_unlock(&mLock);

        printf("main exits here");

        return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-10-24 12:35:34

问题是您错误地使用了条件变量。条件变量只是一种通知机制,而不是标志。除了当前正在等待的线程列表之外,它没有任何内部状态。因此,如果当其他线程调用 pthread_cond_signal()main() 尚未实际执行到 pthread_cond_wait() 调用,则信号丢失,main() 将永远等待。

您需要使用与条件变量关联的单独标志。然后,main() 可以检查该标志,并且仅在未设置该标志时等待。此外,它必须在循环中检查此标志,以确保处理“虚假唤醒”,其中 pthread_cond_wait() 返回但没有相应的信号。这同样适用于 threadOnethreadTwo 之间的通知。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;
int gFlag=0;

pthread_mutex_t mLock;
pthread_cond_t mCondition;
int mFlag=0;

void initialize()
{
    pthread_mutex_init(&gLock, NULL);
    pthread_cond_init (&gCondition, NULL);
    pthread_mutex_init(&mLock, NULL);
    pthread_cond_init (&mCondition, NULL);
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    while(!gFlag)
    {
        pthread_cond_wait(&gCondition,&gLock);
    }
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    mFlag=1;
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    gFlag=1;
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
    pthread_t thread1;
    pthread_t thread2;

    char * msg1="I am in thread 1";
    char * msg2="I am in thread 2";
    initialize();

    pthread_create(&thread1,NULL,threadOne,(void*) msg1);
    pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

    pthread_mutex_lock(&mLock);
    while(!mFlag)
    {
        pthread_cond_wait(&mCondition,&mLock);
    }
    pthread_mutex_unlock(&mLock);

    printf("main exits here");

    return 0;
}

The problem is that you are using the condition variable incorrectly. A condition variable is just a notification mechanism, not a flag. It has no internal state other than the list of threads currently waiting. Consequently, if main() has not actually executed as far as the pthread_cond_wait() call when the other threads call pthread_cond_signal() then the signal is lost, and main() will wait forever.

You need to use a separate flag associated with the condition variable. main() can then check this flag, and only wait if the flag is not set. Also, it must check this flag in a loop, to ensure that "spurious wakeups" are handled, where pthread_cond_wait() returns without a corresponding signal. The same applies to the notification between threadOne and threadTwo.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;
int gFlag=0;

pthread_mutex_t mLock;
pthread_cond_t mCondition;
int mFlag=0;

void initialize()
{
    pthread_mutex_init(&gLock, NULL);
    pthread_cond_init (&gCondition, NULL);
    pthread_mutex_init(&mLock, NULL);
    pthread_cond_init (&mCondition, NULL);
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    while(!gFlag)
    {
        pthread_cond_wait(&gCondition,&gLock);
    }
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    mFlag=1;
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    gFlag=1;
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
    pthread_t thread1;
    pthread_t thread2;

    char * msg1="I am in thread 1";
    char * msg2="I am in thread 2";
    initialize();

    pthread_create(&thread1,NULL,threadOne,(void*) msg1);
    pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

    pthread_mutex_lock(&mLock);
    while(!mFlag)
    {
        pthread_cond_wait(&mCondition,&mLock);
    }
    pthread_mutex_unlock(&mLock);

    printf("main exits here");

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文