void *fun();
时间: 2024-05-22 11:12:25 浏览: 5
This is a function declaration for a function named `fun` that returns a pointer to void. The function takes no arguments, as the parameter list is empty. The implementation of the function is not included in the declaration.
相关问题
void* fun1Proc(void* lpParameter)是什么意思
`* fun1Proc(void* lpParameter)`是一个函数声明,它在C语言中常用于作为线程函数的原型。
在这个声明中,`void*`表示函数的返回类型是一个指向`void`的指针。这意味着线程函数可以返回任何类型的指针或者不返回任何值。
`void* lpParameter`是一个参数,它是一个指向`void`的指针。通常情况下,这个参数可以用来传递额外的数据给线程函数。由于`void*`指针可以被强制转换为其他类型的指针,所以在线程函数内部可以将其转换为实际需要的类型。
在使用时,你可以根据具体需要将`lpParameter`参数强制转换为你想要的类型,然后在线程函数内部使用这个参数。例如:
```cpp
void* fun1Proc(void* lpParameter)
{
int myNumber = *((int*)lpParameter);
// 在线程函数内部使用 myNumber
return NULL;
}
```
在上面的示例中,我们将`lpParameter`强制转换为`int*`类型,然后解引用该指针来获取传递给线程函数的整数值。然后你可以在线程函数内部使用这个整数值。
需要注意的是,在使用`void*`指针传递数据时,需要小心类型转换和解引用指针时的数据类型匹配,以避免潜在的错误。
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #include<unistd.h> #include<pthread.h> int var=0; void *fun(void *); int main() { pthread_t id; int res = pthread_create(&id,NULL,fun,NULL); assert(res == 0); for(int i = 0;i < 10000; i++) { var++; printf("parent is %d\n",var); } exit(0); } void* fun(void *arg) { for(int i = 0;i < 10000; i++) { var--; printf("child is %d\n",var); } }
这是一个使用 pthread 线程库的 C 语言程序,其中包含了一个主线程和一个子线程。
主函数中创建了一个线程 id,并调用 pthread_create 函数创建一个新的线程。该函数的第一个参数是线程 id,第二个参数是线程属性,第三个参数是线程函数,最后一个参数是传给线程函数的参数。在这个程序中,线程属性为 NULL,线程函数为 fun,传递的参数为 NULL。
主线程和子线程都有一个 for 循环,循环的次数为 10000,每次循环都将变量 var 的值加一或减一,并输出当前的 var 值。可以看到,主线程和子线程交替执行,输出的结果也是交替的。
由于主线程和子线程是并发执行的,因此输出的结果可能会出现不同的顺序,这是正常的现象。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)