c - pthread 示例中的查询 regaring 代码序列

标签 c multithreading pthreads

int loop = 4;

int a1=0,a2=10;
int b1=100,b2=1000;

int switchs=0;

void* runT1(void* args) {

    int i;
    for(i=0; i<loop; i++) {
        pthread_mutex_lock(&locks);
            while(switchs == 1) pthread_cond_wait(&conditions,&locks);
            printf("i=%d a1=%d\n",i,a1++);
            printf("i=%d a2=%d\n",i,a2++);
            switchs = 1;
            pthread_cond_signal(&conditions);
        pthread_mutex_unlock(&locks);
    }

    pthread_exit(NULL);
}

void* runT2(void* args) {

        int i;
        for(i=0; i<loop; i++) {
            pthread_mutex_lock(&locks);
                while(switchs == 0) pthread_cond_wait(&conditions,&locks);
                printf("i=%d b1=%d\n",i,b1++);
                printf("i=%d b2=%d\n",i,b2++);
                switchs = 0;
                pthread_cond_signal(&conditions);
            pthread_mutex_unlock(&locks);
        }

        pthread_exit(NULL);
}

void runs(void) {

    pthread_mutex_init(&locks,0);
    pthread_cond_init(&conditions,0);

    pthread_t T1,T2;

    pthread_create(&T1,NULL,runT1,NULL);
    pthread_create(&T2,NULL,runT2,NULL);

    pthread_join(&T1,NULL);
    pthread_join(&T2,NULL);

    pthread_mutex_destroy(&locks);
    pthread_cond_destroy(&conditions);
}

我正在从 main() 调用 runs() 方法。似乎没有提供所需的 输出打印 a1 a2 b1 b2 顺序 4 次。请帮忙!!!

最佳答案

通过编译代码所需的少量修复,它运行良好:

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

int loop = 4;

int a1=0,a2=10;
int b1=100,b2=1000;

int switchs=0;

pthread_mutex_t locks;
pthread_cond_t conditions;

void* runT1(void* args) {

    int i;
    for(i=0; i<loop; i++) {
    pthread_mutex_lock(&locks);
        while(switchs == 1) pthread_cond_wait(&conditions,&locks);
        printf("i=%d a1=%d\n",i,a1++);
        printf("i=%d a2=%d\n",i,a2++);
        switchs = 1;
        pthread_cond_signal(&conditions);
    pthread_mutex_unlock(&locks);
    }

    pthread_exit(NULL);
}

void* runT2(void* args) {

    int i;
    for(i=0; i<loop; i++) {
        pthread_mutex_lock(&locks);
        while(switchs == 0) pthread_cond_wait(&conditions,&locks);
        printf("i=%d b1=%d\n",i,b1++);
        printf("i=%d b2=%d\n",i,b2++);
        switchs = 0;
        pthread_cond_signal(&conditions);
        pthread_mutex_unlock(&locks);
    }

    pthread_exit(NULL);
}

int main(void) {

    pthread_mutex_init(&locks,0);
    pthread_cond_init(&conditions,0);

    pthread_t T1,T2;

    pthread_create(&T1,NULL,runT1,NULL);
    pthread_create(&T2,NULL,runT2,NULL);

    void *j;
    pthread_join(T1,&j);
    pthread_join(T2,&j);

    pthread_mutex_destroy(&locks);
    pthread_cond_destroy(&conditions);
    return 0;
}

输出:

i=0 a1=0
i=0 a2=10
i=0 b1=100
i=0 b2=1000
i=1 a1=1
i=1 a2=11
i=1 b1=101
i=1 b2=1001
i=2 a1=2
i=2 a2=12
i=2 b1=102
i=2 b2=1002
i=3 a1=3
i=3 a2=13
i=3 b1=103
i=3 b2=1003

关于c - pthread 示例中的查询 regaring 代码序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205432/

相关文章:

c - 使用 pixbuf 显示像素缓冲区

c - 如何使用指针在 C 中的第一个空格上分割字符串?

c - 整数从字符串[]接收到错误的值

multithreading - 使用 IOmniPipeline 下载和处理文件

c - 简单 Pthread 例程中的 GDB 断点?

c - x86 asm 崩溃的应用程序

Java组播监听和IGMP

java - 在Jetty HttpClient Hang上寻求建议

c - 将多个值返回到C中的线程时出错

c - pthread_cond_timedwait 忽略取消请求