gpt4 book ai didi

c - 线程已创建但无法相应工作

转载 作者:行者123 更新时间:2023-11-30 14:39:59 25 4
gpt4 key购买 nike

我正在编写的代码遇到问题,但我真的无法识别其原因,因此非常感谢您的帮助。
案例很简单
--> 从命令行给出线程数作为参数
--> 创建N个线程,带参数
--> 每个线程都打招呼及其参数并退出
这是我的代码:

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

void* client_func(void *arg) {
int myid = *((int *) arg);
printf("hello!\n my id is %d\n",myid);
return NULL;
}

int main(int argc, char *argv[] ){
int i,j = 0;
int N_cust = atoi(argv[1]);

//creation of client threads
pthread_t tid[N_cust];
int err;

for (i = 0; i < N_cust; i++){
int *id = malloc(sizeof(*id));
err = pthread_create(&tid[i], NULL, &client_func,(void *) id);
if(err !=0){
perror("pthread_create() error");
exit(1);
}
else{
printf("\n Thread created successfully\n");
}
}

return 0;
}

我正在等待获得带有线程 ID 的“hello”消息,但我得到的是:

$ ./proj1 3 5

Thread created successfully
Thread created successfully
Thread created successfully

我是线程新手,但根据我的理解,线程根本没有执行。对我做错的事情有什么帮助吗?谢谢

最佳答案

正如我在 comment 中指出的那样,您的代码中存在许多问题:

  • 您为 id 指针分配了指向的空间,但从未将其设置为任何已知值。
  • 您的线程函数还忘记释放为其分配的内存。
  • 您的主程序可能还应该等待子线程使用 pthread_join() 完成,然后再退出(通过 return 0;)。

调用 exit() — 甚至通过从 main() 返回间接调用 — 意味着进程(以及其中的所有线程)立即终止。您可以在 main() 中使用 pthread_exit(0); 而不是 return 0;,这样您的线程就会运行完成。

以下是已修复这些问题的代码的两个变体。变体 1 (pthr41.c) 的主线程退出为 pthread_exit(0);。变体 2 (pthr43.c) 的主线程使用 pthread_join()。添加了一些错误检测。

pthr41.c

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

static void *client_func(void *arg)
{
int myid = *((int *)arg);
printf("\nhello! my id is %d\n", myid);
free(arg);
return NULL;
}

int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s numthreads\n", argv[0]);
return 1;
}
int N_cust = atoi(argv[1]);
if (N_cust <= 0 || N_cust > 1000)
{
fprintf(stderr, "%s: number of threads %s out of range 1..1000\n", argv[0], argv[1]);
return 1;
}

// creation of client threads
pthread_t tid[N_cust];

for (int i = 0; i < N_cust; i++)
{
int *id = malloc(sizeof(*id));
*id = i + 1;
int err = pthread_create(&tid[i], NULL, client_func, id);
if (err != 0)
{
perror("pthread_create() error");
exit(1);
}
else
{
printf("\nThread %d created successfully\n", i + 1);
}
}

printf("\nMain thread exits\n");
pthread_exit(0);
//return 0;
}

示例输出

在获得此输出之前,需要运行多次,但这显示线程 4 在主线程之后完成:

$ pthr41 4

Thread 1 created successfully

Thread 2 created successfully

hello! my id is 1

hello! my id is 2

hello! my id is 3

Thread 3 created successfully

Thread 4 created successfully

Main thread exits

hello! my id is 4
$

pthr43.c

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

static void *client_func(void *arg)
{
int myid = *((int *)arg);
printf("\nhello! my id is %d\n", myid);
free(arg);
return NULL;
}

int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s numthreads\n", argv[0]);
return 1;
}
int N_cust = atoi(argv[1]);
if (N_cust <= 0 || N_cust > 1000)
{
fprintf(stderr, "%s: number of threads %s out of range 1..1000\n", argv[0], argv[1]);
return 1;
}

// creation of client threads
pthread_t tid[N_cust];

for (int i = 0; i < N_cust; i++)
{
int *id = malloc(sizeof(*id));
*id = i + 1;
int err = pthread_create(&tid[i], NULL, client_func, id);
if (err != 0)
{
perror("pthread_create() error");
exit(1);
}
else
{
printf("\nThread %d created successfully\n", i + 1);
}
}

for (int i = 0; i < N_cust; i++)
{
void *vp;
int err = pthread_join(tid[i], &vp);
if (err != 0)
{
fprintf(stderr, "%s: error %d (%s) from joining thread %d\n",
argv[0], err, strerror(err), i + 1);
}
else
assert(vp == NULL);
}
printf("All threads complete\n");

//pthread_exit(0);
return 0;
}

示例输出

$ pthr43 4

Thread 1 created successfully

hello! my id is 1

hello! my id is 2

Thread 2 created successfully

Thread 3 created successfully

hello! my id is 3

Thread 4 created successfully

hello! my id is 4
All threads complete
$

关于c - 线程已创建但无法相应工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778925/

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