小编Tri*_*yen的帖子

我的线程不是并行的,它们是串行的.如何使它们平行?

我正在练习多线程.

我创建两个posix线程,向屏幕显示文本(无限循环),但它似乎只是第一个线程运行.我修改程序没有循环,第一个线程打印,以下是第二个线程.似乎我的线程不是并行的,第一个线程必须在线程二开始之前完成.我怎样才能让它们平行?

谢谢,

hdr.h

#ifndef HDR_HDR_H_
#define HDR_HDR_H_
#define HDR_HDR_H_
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#endif /* HDR_HDR_H_ */
Run Code Online (Sandbox Code Playgroud)

multithread01.c

#include "../hdr/myfunc.h"
pthread_mutex_t lock;

int main(int argc, char **argv)
{
    pthread_t tid01;
    pthread_t tid02;
    void * status01;
    void * status02;

    pthread_create(&tid01, NULL, PrintOut01(), NULL);
    pthread_create(&tid02, NULL, PrintOut02(), NULL);

    pthread_join(&tid01, &status01);
    pthread_join(&tid02, &status02);

    return 0;
Run Code Online (Sandbox Code Playgroud)

}

myfunc.h

#ifndef HDR_MYFUNC_H_
#define HDR_MYFUNC_H_
#include "../hdr/hdr.h"
void * PrintOut01 (void);
void * PrintOut02 (void);
#endif /* HDR_MYFUNC_H_ */
Run Code Online (Sandbox Code Playgroud)

myfunc.c

#include "../hdr/hdr.h"

extern pthread_mutex_t …
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads

4
推荐指数
1
解决办法
66
查看次数

标签 统计

c ×1

multithreading ×1

pthreads ×1