pthread_create invalid conversion from int to void*
时间: 2023-08-24 17:13:05 浏览: 170
这个问题可能是因为 `pthread_create` 函数的第三个参数需要传入一个 `void*` 类型的指针,但是你传入了一个 `int` 类型的值。
解决方法是将 `int` 类型的值转换成 `void*` 类型,然后再传入函数中。可以使用 C 语言中的类型转换操作符 `(void*)`,如下所示:
```
int arg = 123;
pthread_create(&thread_id, NULL, my_thread_function, (void*)arg);
```
在线程函数中,需要将参数再转换回 `int` 类型,如下所示:
```
void* my_thread_function(void* arg) {
int my_arg = (int)arg;
// do something with my_arg
}
```
注意,这种方法只适用于传递简单类型的参数,如果需要传递复杂的数据结构,建议使用指针。
相关问题
int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg) __THROWNL __nonnull ((1, 3));
这是 pthread_create 函数的声明,它是 POSIX 线程库中的一个函数,在多线程编程中经常使用。
该函数的作用是创建一个新的线程,并在新线程中执行指定的函数。参数说明如下:
- __newthread:一个指向 pthread_t 类型的指针,用于存储新线程的 ID。
- __attr:一个指向 pthread_attr_t 类型的指针,用于设置新线程的属性,如果不需要设置,则传入 NULL。
- __start_routine:一个指向函数的指针,该函数将在新线程中执行,函数的返回值为 void *,参数为 void *。
- __arg:一个指向 void 类型的指针,用于传递给 __start_routine 函数的参数。
该函数将返回一个整数值,表示线程创建是否成功。如果返回值为 0,则表示创建线程成功,否则表示失败。
需要注意的是,该函数的参数中有一些限制条件,例如 __newthread 和 __start_routine 参数不能为 NULL,__start_routine 函数需要是可重入的等等,具体可以查看相关文档。
使用pthread_create传一个int类型参数
使用pthread_create传递一个int类型参数的方法可以有两种。
第一种方法是将int类型参数强制转换为void指针类型传递,然后在线程函数中再将void指针类型转换为int类型。这样可以避免报警问题。例如,在代码中可以使用以下方式传递int类型参数:
```c
int arg = 10;
pthread_create(&thread, &threadAttr, PthreadTest, (void *)(long)arg);
```
然后在PthreadTest函数中将参数转换回int类型:
```c
int param = (int)(long)(arg);
```
这样就可以正确地传递和使用int类型参数了。
第二种方法是使用intptr_t类型来传递int类型参数。intptr_t是一种整数类型,可以保证和指针类型有相同的大小。在代码中可以使用以下方式传递int类型参数:
```c
int arg = 10;
pthread_create(&thread, &threadAttr, PthreadTest, (void *)(intptr_t)arg);
```
然后在PthreadTest函数中将参数转换回int类型:
```c
int param = (int)(intptr_t)(arg);
```
这样也可以正确地传递和使用int类型参数,并且不会出现警告问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Linux多线程编程pthread_create传参的问题](https://blog.csdn.net/hexf9632/article/details/130853933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Linux线程:创建(pthread_create),等待(pthread_join),退出(pthread_exit)](https://blog.csdn.net/m0_74985965/article/details/128815940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)