用C语言中的线程打印奇偶数 #包括 #包括 #包括 #包括 pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项; void*thread_偶数(void*arg); void*螺纹奇数(void*arg); int main(int argc,字符**argv){ pthread_t tid[2]; pthread_create(&tid[0],0,&thread_偶数,0); pthread_create(&tid[1],0,&thread_odd,0); pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); 返回0; } void*thread\u偶数(void*arg){ int*线程id=(int*)arg; pthread_mutex_lock(&mutex); for(int i=1;i

用C语言中的线程打印奇偶数 #包括 #包括 #包括 #包括 pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项; void*thread_偶数(void*arg); void*螺纹奇数(void*arg); int main(int argc,字符**argv){ pthread_t tid[2]; pthread_create(&tid[0],0,&thread_偶数,0); pthread_create(&tid[1],0,&thread_odd,0); pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); 返回0; } void*thread\u偶数(void*arg){ int*线程id=(int*)arg; pthread_mutex_lock(&mutex); for(int i=1;i,c,pthreads,computer-science,C,Pthreads,Computer Science,看起来您正在将0AKANULL传递到pthread\u create的最后一个参数,然后执行以下操作: #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* thread_even(void* arg); void* thread

看起来您正在将
0
AKA
NULL
传递到
pthread\u create
的最后一个参数,然后执行以下操作:

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* thread_even(void* arg);
void* thread_odd(void* arg);

int main(int argc, char** argv) {

    pthread_t tid[2];

    pthread_create(&tid[0], 0, &thread_even, 0);
    pthread_create(&tid[1], 0, &thread_odd, 0);

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}

void* thread_even(void* arg) {
    int* thread_id = (int*)arg;

    pthread_mutex_lock(&mutex);

    for(int i = 1; i <= *thread_id; i++)
    {
        if(i%2 != 0)
        {
            printf("Thread 1: %d", i);
        }
    }

    pthread_mutex_unlock(&mutex);

    return NULL;
}

void* thread_odd(void* arg) {
    int* thread_id = (int*)arg;

    pthread_mutex_lock(&mutex);

    for(int i = 1; i <= *thread_id; i++)
    {
        if(i%2 == 0)
        {
            printf("Thread 2: %d", i);
        }
    }

    pthread_mutex_unlock(&mutex);

    return NULL;
}

有很多方法可以做到这一点,而不必求助于堆分配,但这是最直接的方法。

int*thread\u id=(int*)arg;
-um…您将
NULL
作为参数传递给
pthread\u create
以获取
arg
的内容。毫不奇怪,
i那么我应该传递什么作为参数以使程序成功?谢谢!@seung,请查看我的答案。此外,您所做的锁定可能比您所期望的更粗糙。正如您所做的那样“现在就开始,实际上一个线程将完成,然后另一个线程将运行。@EvanTeran我真的很抱歉,但我真的想知道如何以我的方式完成……我能知道我应该传递给偶数和奇数函数什么参数吗?这个“交替运行线程代码”问题有数百个重复项。它们似乎都试图使用互斥锁,而当它们不运行时。”wo信号量是理智的解决方案(如果任何此类要求是理智的-这显然是一个练习/家庭作业)。为什么他们都试图使用错误的同步器?是因为“互斥体”是一个更酷的名称,还是需要更少的键入?非常感谢!但它会像这样打印线程1线程1和线程2线程2。有没有办法交替打印它们?正如我的评论所提到的,您编写的互斥体将在一个线程中执行所有操作。如何解决这个问题目前的问题是另一个问题。
int* thread_id = (int*)arg;
pthread_mutex_lock(&mutex);
for(int i = 1; i <= *thread_id; i++)
int main(int argc, char** argv) {

    pthread_t tid[2];
    int *ids = malloc(2 * sizeof(int));
    ids[0] = 10; /* upper bound for thread 1 */
    ids[1] = 10; /* upper bound for thread 2 */

    pthread_create(&tid[0], 0, &thread_even, &ids[0]);
    pthread_create(&tid[1], 0, &thread_odd, &ids[1]);

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    free(ids);
    return 0;
}