gpt4 book ai didi

c - 空线程阻塞的所有线程均被读取

转载 作者:行者123 更新时间:2023-12-03 12:52:16 27 4
gpt4 key购买 nike

我试图自学有关C(Linux)中的多线程和多进程编程的知识。我写了一个简短的程序,它产生了一个新线程,该线程转到一个例程,该例程尝试从空FIFO进行阻塞读取,而主线程继续并打印到STDOUT。 (注意:在执行程序之前,我确实在终端中使用mkfifo newfifo创建了一个FIFO)

我期望程序打印到屏幕“主线程”,然后等待我将数据放入FIFO时阻塞。而是,整个过程被阻塞,消息“主线程”仅在我将数据放入FIFO后才打印。

我在这里想念什么吗?即使生成的线程被阻塞,主线程也不应该继续运行吗?我尝试了使用fork创建一个子进程的测试,并得到了相同的结果(两个进程都被从空FIFO读取而阻止了)。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pthread.h>

#define NEWPIPE "./newfifo"

typedef struct __attribute__ ((__packed__)) {
int reserved :30;
int threadStarted :1;
int msgRcved :1;
} Status;

void* thread_routine(int fd, char* buffer, Status* myStatus)
{
int great_success = 0;

myStatus->threadStarted = 1;
printf("Side thread\n");

great_success = read(fd, buffer, sizeof(buffer));

if (great_success < 0) {
printf("pipe failed\n");
} else {
myStatus->msgRcved = 1;
}
}

void main()
{
int fd;
int cnt = 0;
char buffer[20];
Status* myStatus;
pthread_t thread_id;

myStatus = (Status*) malloc(sizeof(Status));
myStatus->reserved = 0;
myStatus->threadStarted = 0;
myStatus->msgRcved = 0;

fd = open(NEWPIPE, O_RDONLY);

pthread_create(&thread_id,
NULL,
(void *) thread_routine(fd, buffer, myStatus),
NULL);

printf("Main thread\n");

while (!myStatus->threadStarted) {
printf("Main thread: side thread started!\n");
}

while (!myStatus->msgRcved) {
sleep(1);
cnt++;
}

printf("buffer (cnt = %d): %s\n", cnt, buffer);

}

编辑:最新代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pthread.h>

#define NEWPIPE "./newfifo"

struct Status {
unsigned int reserved :30;
unsigned int threadStarted :1;
unsigned int msgRcved :1;
};

void* thread_routine(void *arg)
{
int great_success = 0;
int fd;
char buffer[20];
struct Status* myStatus;

fd = open(NEWPIPE, O_RDONLY);

myStatus = arg;

myStatus->threadStarted = 1;
printf("Side thread\n");

while (1) {
great_success = read(fd, buffer, 20);

if (great_success < 0) {
printf("pipe failed\n");
} else {
printf("buffer : %s\n", buffer);
printf("great_success = %d\n", great_success);
great_success = 0;
}
}
}

void main()
{
int cnt = 0;
struct Status* myStatus;
pthread_t thread_id;

myStatus = (struct Status*) malloc(sizeof(struct Status));
myStatus->reserved = 0;
myStatus->threadStarted = 0;
myStatus->msgRcved = 0;

pthread_create(&thread_id,
NULL,
&thread_routine,
(void *) myStatus); // arguments to pass to the function!


printf("Main thread\n");

while (!myStatus->msgRcved) {
printf("Main thread: side thread started!\n");

if (myStatus->threadStarted) {
printf("spawned thread started!\n");
}

sleep(1);
}

pthread_exit(NULL);

}

最佳答案

Instead, the entire process is blocked, and the message "Main thread" only prints after I've put data into the FIFO.

Am I missing something here?



您的主线程在此行被阻止:
fd = open(NEWPIPE, O_RDONLY);

因为FIFO的非阻塞,只读打开将阻塞,直到写入器可用为止。您的主线程最终将被解除阻塞,而不是在将数据写入FIFO时,而只是在打开FIFO进行写入时才解除阻塞。

代码中还有其他问题,如@JohnBollinger discusses in his answer。但是,FIFO语义是为什么您看不到预期的初始输出的原因。

关于c - 空线程阻塞的所有线程均被读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com