繁体   English   中英

编程线程

[英]Programming thread

我在程序中创建了5个线程,并分别为其分配ID 1、2、3、4、5。 每个线程将尝试访问Next_ID。 当线程获取Next_ID时,它将其ID与Next_ID进行比较。 如果匹配,则我打印我的回合,而其他人则不打印我的回合。 并将Next_ID递增1。如果Next_ID达到6,它将重置为1。但是我的代码显示以下内容:

My Turn: 1
Not My Turn: 3
My Turn: 4
My Turn: 5
Not My Turn: 2
Not My Turn: 1
Not My Turn: 3
My Turn: 4
My Turn: 5
Not My Turn: 2
Not My Turn: 1
Not My Turn: 3
My Turn: 4
My Turn: 5
Not My Turn: 2
Not My Turn: 1
Not My Turn: 3
My Turn: 4
My Turn: 5
Not My Turn: 2
Not My Turn: 1
Not My Turn: 3
My Turn: 4
My Turn: 5

预期输出应为:

My Turn: 1
not my Turn: 2
not my Turn: 3
not my Turn: 4
not my Turn: 5
not my Turn: 1
My Turn: 2
not my Turn: 3
not my Turn: 4
not my Turn: 5

我的代码:

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

#define NUM_THREADS 5 

int Next_ID = 1; 

pthread_t threads[NUM_THREADS];
pthread_mutex_t mutex;

typedef struct threadArgs { 
    int threadId;
    int numOfCalls;
} ThreadArgs;

ThreadArgs threadArgsArray[NUM_THREADS]; 

void * printThread(void *pThreadArgs) {
    ThreadArgs *threadArgs = (ThreadArgs *) pThreadArgs; 
    int *threadId = &threadArgs->threadId; 

    for(int i = 0; i < 20; i++) { 
        pthread_mutex_lock(&mutex); 
        if (Next_ID == *threadID) {
            printf("My Turn: %d\n", *threadId);
        } else {
            printf("Not My Turn: %d\n", *threadId);
        }

        Next_ID = (*threadId == 5) ? 1 : *threadId + 1;
        pthread_mutex_unlock (&mutex);
    }
    pthread_exit(NULL);
}


void * printThread(void *); 

int main(int argc, char const *argv[]) {
    pthread_attr_t attr;
    void *status;

    pthread_mutex_init(&mutex, NULL); 
    pthread_attr_init(&attr); 
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    for (int i = 0; i < NUM_THREADS; i++) {
        threadArgsArray[i].threadId = i + 1;

        int rc = pthread_create(&threads[i], &attr, printThread,
                                &threadArgsArray[i]);
    }

    for (int i = 0; i < NUM_THREADS; i++) {
        int rc = pthread_join(threads[i], &status);
    }

    pthread_attr_destroy(&attr);
    pthread_mutex_destroy(&mutex);

    pthread_exit(NULL);
}

为了从线程1到线程5顺序运行,可以使用conditional variablepthread_cond_t )。 current变量以指定可以运行的线程。

另一种显而易见的方法是使用pipe

下面的代码使用conditional variable

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

#define NUM_THREADS 5

int Next_ID = 1;

pthread_t threads[NUM_THREADS];
pthread_mutex_t mutex;
pthread_cond_t cond;
int current;

typedef struct threadArgs {
    int threadId;
    int numOfCalls;
} ThreadArgs;

ThreadArgs threadArgsArray[NUM_THREADS];

void * printThread(void *pThreadArgs) {
    ThreadArgs *threadArgs = (ThreadArgs *) pThreadArgs;
    int *threadId = &threadArgs->threadId;

    for(int i = 0; i < 20; i++) {
        pthread_mutex_lock(&mutex);
        while (current+1!=*threadId){
            pthread_cond_wait(&cond, &mutex);
        }
        if (Next_ID == *threadId) {
            printf("My Turn: %d\n", *threadId);
        } else {
            printf("Not My Turn: %d\n", *threadId);
        }

        if (*threadId==5){
            Next_ID++;
            if (Next_ID==6){
                Next_ID=1;
            }
        }
        current=(current+1)%NUM_THREADS;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}


void * printThread(void *);

int main(int argc, char const *argv[]) {
    pthread_attr_t attr;
    void *status;

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    current=0;
    for (int i = 0; i < NUM_THREADS; i++) {
        threadArgsArray[i].threadId = i + 1;

        int rc = pthread_create(&threads[i], &attr, printThread,
                    &threadArgsArray[i]);
    }

    for (int i = 0; i < NUM_THREADS; i++) {
        int rc = pthread_join(threads[i], &status);
    }

    pthread_attr_destroy(&attr);
    pthread_mutex_destroy(&mutex);

    pthread_exit(NULL);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM