繁体   English   中英

调用pthread_join函数后main不会继续

[英]main doesn't continue after call pthread_join function

我是pthread和多线程的新手,我已经编写了这样的代码。

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


void *nfc_read(void *arg)
{
    int fd = -1;   
    int ret;
    uint8_t read_data[24];

    while(1){
        ret = read_block(fd, 8, read_data);
        if(!ret){
            return (void)read_data;
        }
    }
}

int main(int argc, char *argv[])
{
    pthread_t my_thread;
    void  *returnValue;

    pthread_create(&my_thread, NULL, nfc_read, NULL);
    pthread_join(my_thread, &returnValue);

    printf("NFC card value is : %s \n", (char)returnValue);

    printf("other process");

  return 0;
}

直到从nfc_read函数返回值,因为我将要运行其他代码,而我不想在main中阻塞。 我怎样才能做到这一点?

这是一个示例,其中读取线程与正在执行其他工作的“主”线程同时运行(在这种情况下,是打印点并休眠)。

为简单起见,模拟了从输入设备读取的操作,并逐个字符地复制了恒定的字符串。 我想这是合理的,因为线程的同步是集中的。

为了实现线程同步,我将atomic_boolatomic_store()atomic_load() ,这是由Atomic Library提供的(自C11起)。

我的示例应用程序test-concurrent-read.c

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

/* sampe input */
const char sampleInput[]
  = "This is sample input which is consumed as if it was read from"
    " a (very slow) external device.";

atomic_bool readDone = ATOMIC_VAR_INIT(0);

void* threadRead(void *pArg)
{
  char **pPBuffer = (char**)pArg;
  size_t len = 0, size = 0;
  int c; const char *pRead;
  for (pRead = sampleInput; (c = *pRead++) > 0; sleep(1)) {
    if (len + 1 >= size) {
      if (!(*pPBuffer = realloc(*pPBuffer, (size + 64) * sizeof(char)))) {
        fprintf(stderr, "ERROR! Allocation failed!\n");
        break;
      }
      size += 64;
    }
    (*pPBuffer)[len++] = c; (*pPBuffer)[len] = '\0';
  }
  atomic_store(&readDone, 1);
  return NULL;
}

int main()
{
  /* start thread to read concurrently */
  printf("Starting thread...\n");
  pthread_t idThreadRead; /* thread ID for read thread */
  char *pBuffer = NULL; /* pointer to return buffer from thread */
  if (pthread_create(&idThreadRead, NULL, &threadRead, &pBuffer)) {
    fprintf(stderr, "ERROR: Failed to start read thread!\n");
    return -1;
  }
  /* start main loop */
  printf("Starting main loop...\n");
  do {
    putchar('.'); fflush(stdout);
    sleep(1);
  } while (!atomic_load(&readDone));
  putchar('\n');
  void *ret;
  pthread_join(idThreadRead, &ret);
  /* after sync */
  printf("\nReceived: '%s'\n", pBuffer ? pBuffer : "<NULL>");
  free(pBuffer);
  /* done */
  return 0;
}

在Windows 10(64位)上的cygwin中使用gcc进行编译和测试:

$ gcc -std=c11 -pthread -o test-concurrent-read test-concurrent-read.c

$ ./test-concurrent-read
Starting thread...
Starting main loop...
.............................................................................................

Received: 'This is sample input which is consumed as if it was read from a (very slow) external device.'

$ 

我想,值得一提的是为什么在main()threadRead()都没有为pBuffer互斥保护。

  1. 在调用pthread_create() 之前,先main()初始化pBuffer

  2. thread_read()运行时, pBuffer专门用于它(通过其在pPBuffer传递的地址)。

  3. 再次在main()访问它,但不能在 pthread_join() 之前访问它,这会授权threadRead()已经结束。

我试图找到Google的参考,以确认此过程定义明确且合理。 我能找到的最好的是SO:pthread_create(3)和SMP架构中的内存同步保证 ,引用了《开放组基础规范》第7-4.12节“内存同步”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM