顺序打印多个线程ID

本文介绍了一种通过多线程实现顺序打印线程ID的方法,提供了两种实现方案:一种使用带有超时时间的条件变量,另一种则利用条件变量或事件对象进行线程间的同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近面试时,碰到一个面试题,题目是有3个线程,顺序打印这三个线程的线程ID多次,例如ABCABC...这样打印。

第一种方法:使用pthread_cond_timedwait或WaitForSingleObject,超时时间到了就打印出线程ID,每个线程设置不同的超时等待时间。

/*
 * main.cpp
 *
 *  Created on: 2014年12月27日
 *      Author: Richard
 */

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

pthread_cond_t g_Condition;
pthread_mutex_t g_Mutex;

void *ThreadFunc(void *Param)
{
    int WaitTime = *(int *)Param;

    for (int i = 0; i < 2; ++i) {
        timespec Tm;
        clock_gettime(CLOCK_REALTIME, &Tm);
        Tm.tv_sec += WaitTime;
        pthread_mutex_lock(&g_Mutex);
        pthread_cond_timedwait(&g_Condition, &g_Mutex, &Tm);
        pthread_mutex_unlock(&g_Mutex);
        printf("%lu ", pthread_self());
    }

    return NULL;
}

int main(int argc, char **argv) {
    pthread_cond_init(&g_Condition, NULL);
    pthread_mutex_init(&g_Mutex, NULL);

    pthread_t ThreadID[3];
    int WaitTime[]={3, 4, 5};

    for (int i = 0; i < 3; ++i) {
        pthread_create(&ThreadID[i], NULL, ThreadFunc, (void *)(&WaitTime[i]));
    }

    for (int i = 0; i < 3; ++i) {
        pthread_join(ThreadID[i], NULL);
    }

    pthread_cond_destroy(&g_Condition);
    pthread_mutex_destroy(&g_Mutex);
    return 0;
}


#include <process.h>
#include <stdio.h>
#include <Windows.h>

HANDLE g_Event;

unsigned int __stdcall ThreadFunc(void *Param)
{
    int Interval = *(int *)Param;

    for (int i = 0; i < 2; i++)
    {
        WaitForSingleObject(g_Event, Interval);
        printf("%d ", GetCurrentThreadId());
    }

    return 0;
}

int main(int argc, char **argv)
{
    g_Event = CreateEvent(NULL, FALSE, FALSE, NULL);

    HANDLE Thread[3];
    int Interval[3] = {3000, 4000, 5000};

    for (int i = 0; i < 3; i++)
    {
        Thread[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void *)&Interval[i], 0, NULL);
    }

    Sleep(10000);

    for (int i = 0; i < 3; i++)
    {
        CloseHandle(Thread[i]);
    }
    CloseHandle(g_Event);

    system("Pause");
    return 0;
}


第二种方法:使用pthread_cond_wait和WaitForSingleObject,WaitForSingleObject不用指定超时时间,使用3个条件变量或Event,主线程中依次调用pthread_cond_signal或SetEvent。

/*
 * main.cpp
 *
 *  Created on: 2014年12月27日
 *      Author: Richard
 */

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_cond_t g_Condition[3];
pthread_mutex_t g_Mutex;

void *ThreadFunc(void *Param)
{
    int Index = *(int *) Param;

    for (int i = 0; i < 10; ++i)
    {
        pthread_mutex_lock(&g_Mutex);
        pthread_cond_wait(&g_Condition[Index], &g_Mutex);
        pthread_mutex_unlock(&g_Mutex);

        printf("%lu ", pthread_self());
    }

    return NULL;
}

int main(int argc, char **argv)
{
    for (int i = 0; i < 3; ++i)
    {
        pthread_cond_init(&g_Condition[i], NULL);
    }
    pthread_mutex_init(&g_Mutex, NULL);

    pthread_t ThreadID[3];
    int Index[] = { 0, 1, 2 };

    for (int i = 0; i < 3; ++i)
    {
        pthread_create(&ThreadID[i], NULL, ThreadFunc, (void *) (&Index[i]));
    }

    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            usleep(100);
            pthread_mutex_lock(&g_Mutex);
            pthread_cond_signal(&g_Condition[j]);
            pthread_mutex_unlock(&g_Mutex);
        }

    }

    for (int i = 0; i < 3; ++i)
    {
        pthread_join(ThreadID[i], NULL);
    }

    for (int i = 0; i < 3; ++i)
    {
        pthread_cond_destroy(&g_Condition[i]);
    }

    pthread_mutex_destroy(&g_Mutex);
    return 0;
}

#include <process.h>
#include <stdio.h>
#include <Windows.h>

HANDLE g_Event[3];

unsigned int __stdcall ThreadFunc(void *Param)
{
    int Index = *(int *)Param;

    for (int i = 0; i < 10; i++)
    {
        WaitForSingleObject(g_Event[Index], INFINITE);
        printf("%d ", GetCurrentThreadId());
        ResetEvent(g_Event[Index]);
    }

    return 0;
}

int main(int argc, char **argv)
{
    HANDLE Thread[3];
    int Index[3] = {0, 1, 2};

    for (int i = 0; i < 3; i++)
    {
        g_Event[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
        Thread[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (void *)&Index[i], 0, NULL);
    }

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            Sleep(300);
            SetEvent(g_Event[j]);
        }
    }

    Sleep(2000);

    for (int i = 0; i < 3; i++)
    {
        CloseHandle(g_Event[i]);
        CloseHandle(Thread[i]);
    }

    system("Pause");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值