簡體   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