我正在编写的代码遇到问题,但是我真的不知道它是由什么引起的,因此非常感谢您的帮助。
情况很简单
->从命令行给出线程数作为参数
->使用参数创建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
线程创建成功
线程创建成功
线程创建成功
我是线程的新手,但据我了解,线程根本没有执行。 我做错了什么吗? 谢谢
答案 0 :(得分:1)
正如我在comment中所指出的那样,您的代码中存在许多问题:
sudo pip install awscli --force-reinstall --upgrade
指针分配了指向的空间,但从未将其设置为任何已知值。id
),等待子线程以pthread_join()
完成。调用return 0;
-甚至通过从exit()
返回来间接调用-意味着该进程(及其中的所有线程)都将立即终止。您可以在main()
中使用pthread_exit(0);
而不是main()
,这样您的线程才能运行完成。
这是您的代码的两个变体,已修复了这些问题。变体1(return 0;
)的主线程出口为pthr41.c
。变体2(pthread_exit(0);
)的主线程使用pthr43.c
。添加了少量的错误检测。
pthread_join()
pthr41.c
在获得此输出之前,已经运行了许多次,但这表明线程4在主线程之后完成:
#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;
}
$ 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;
}