代码是示例,我需要做的是修改。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *my_function (void*); // Function for the thread
int main ()
{
pthread_t my_thread ; // Declare a thread identifier
int rc1, x = 7;
// Create my_thread
if ( (rc1 = pthread_create (& my_thread, NULL, & my_function, (void*) &x)))
{
printf ("Error in creating thread %d\n", rc1);
}
pthread_join ( my_thread, NULL); // wait for thread to exit
return (0); // exit the main function
}
// The my_thread is created with my_function() which accepts an argument
void *my_function(void* arg)
{
int i = *(int*)arg;
printf ("The argument which this thread received is %d \n", i ) ;
pthread_exit (NULL) ; // thread exits
}
问题是:在创建线程时将一个简单的整数传递给线程的启动函数。但我不知道如何将一个简单的整数传递给线程的启动函数,我也不知道什么是线程创建时间。
答案 0 :(得分:0)
如果你的遗产有intptr_t
(大多数都有),你可以修改代码如下:
int rc1;
intptr_t x = 7
...
pthread_create (&my_thread, NULL, &my_function, (void*)x));
...
void* my_function(void* arg)
{
intptr_t i = (intptr_t)arg;
...