I am running 32 bits
of OS. Now the thread I created will return a int value which it could be larger than 4G
. How can I receive this value from my main()
function by pthread_join()
? Looks like in 32 bits
system, (void *)
is 4 bytes.
我正在运行32位操作系统。现在我创建的线程将返回一个int值,它可能大于4G。如何通过pthread_join()从main()函数接收此值?在32位系统中,(void *)是4个字节。
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void* thread_function(void)
{
uint64_t nbytes = 0;
//assign values to nbytes, it could be larger than 4G.
return (void *)nbytes;
}
int main()
{
pthread_t thread_id;
uint64_t nbytes;
pthread_create (&thread_id, NULL, &thread_function, NULL);
pthread_join(thread_id,(void**)&nbytes);
}
2 个解决方案
#1
2
Like this:
void* thread_function(void *)
{
uint64_t nbytes = 0;
//assign values to nbytes, it could be larger than 4G.
void *retval = malloc (sizeof (nbytes));
memcpy (retval, &nbytes, sizeof (nbytes));
return retval;
}
int main()
{
pthread_t thread_id;
uint64_t nbytes;
pthread_create (&thread_id, NULL, &thread_function, NULL);
void *ret;
pthread_join(thread_id, &ret);
memcpy (nbytes, ret, sizeof (nbytes));
free (ret);
}
This is a common pattern for transferring a value from one thread to another. The sending thread allocates memory, copies in the value, and passes a pointer. The receiving thread gets the pointer, copies out the value and frees the pointer.
这是将值从一个线程传输到另一个线程的常见模式。发送线程分配内存,复制值,并传递指针。接收线程获取指针,复制出值并释放指针。
#2
2
David Schwartz's solution is well known but a bit too much to pass a simple integer; malloc()
is expensive and not necessarily thread safe(highly unlikely but with all of the embeded stuff today…).
David Schwartz的解决方案众所周知,但传递一个简单的整数有点过分; malloc()是昂贵的,不一定是线程安全的(非常不可能,但今天所有嵌入的东西......)。
Picking up the idea of the first two commenters to the OP (WhozCraig and Eugene Sh.)
将前两位评论者的想法提交给OP(WhozCraig和Eugene Sh。)
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
void *thread_function(void *arg)
{
/*
No direct dereferencing
*arg = 0xdeadbeefcafe;
would give a compile error. With GCC it would be
threadargs.c:8:5: warning: dereferencing ‘void *’ pointer [enabled by default]
*/
uint64_t *nbytes = arg;
*nbytes = 0xdeadbeefcafe;
// you can return a simple status here, for example an error
return 0;
}
int main()
{
pthread_t thread_id;
uint64_t nbytes;
pthread_create(&thread_id, NULL, &thread_function, &nbytes);
pthread_join(thread_id, NULL);
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
printf("nbytes = %" PRIx64 "\n", nbytes);
return 0;
}
Should do the job in another way, maybe a better one for this type of usage.
应该以另一种方式完成工作,对于这种类型的使用可能更好。
Disadvantage: every thread wants its own variable to fill, so it is a better fit for a fixed, small amount of threads, otherwise you are allocating from the heap and have won nothing, just the opposite: it would be more complicated to keep hold off all the malloc()/free()
. David Schwartz's method would be much more apt in that case.
缺点:每个线程都需要自己的变量来填充,因此它更适合固定的少量线程,否则你从堆中分配并没有赢得任何东西,恰恰相反:保持持久会更复杂关闭所有malloc()/ free()。在这种情况下,大卫施瓦茨的方法会更加贴切。
#1
2
Like this:
void* thread_function(void *)
{
uint64_t nbytes = 0;
//assign values to nbytes, it could be larger than 4G.
void *retval = malloc (sizeof (nbytes));
memcpy (retval, &nbytes, sizeof (nbytes));
return retval;
}
int main()
{
pthread_t thread_id;
uint64_t nbytes;
pthread_create (&thread_id, NULL, &thread_function, NULL);
void *ret;
pthread_join(thread_id, &ret);
memcpy (nbytes, ret, sizeof (nbytes));
free (ret);
}
This is a common pattern for transferring a value from one thread to another. The sending thread allocates memory, copies in the value, and passes a pointer. The receiving thread gets the pointer, copies out the value and frees the pointer.
这是将值从一个线程传输到另一个线程的常见模式。发送线程分配内存,复制值,并传递指针。接收线程获取指针,复制出值并释放指针。
#2
2
David Schwartz's solution is well known but a bit too much to pass a simple integer; malloc()
is expensive and not necessarily thread safe(highly unlikely but with all of the embeded stuff today…).
David Schwartz的解决方案众所周知,但传递一个简单的整数有点过分; malloc()是昂贵的,不一定是线程安全的(非常不可能,但今天所有嵌入的东西......)。
Picking up the idea of the first two commenters to the OP (WhozCraig and Eugene Sh.)
将前两位评论者的想法提交给OP(WhozCraig和Eugene Sh。)
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
void *thread_function(void *arg)
{
/*
No direct dereferencing
*arg = 0xdeadbeefcafe;
would give a compile error. With GCC it would be
threadargs.c:8:5: warning: dereferencing ‘void *’ pointer [enabled by default]
*/
uint64_t *nbytes = arg;
*nbytes = 0xdeadbeefcafe;
// you can return a simple status here, for example an error
return 0;
}
int main()
{
pthread_t thread_id;
uint64_t nbytes;
pthread_create(&thread_id, NULL, &thread_function, &nbytes);
pthread_join(thread_id, NULL);
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
printf("nbytes = %" PRIx64 "\n", nbytes);
return 0;
}
Should do the job in another way, maybe a better one for this type of usage.
应该以另一种方式完成工作,对于这种类型的使用可能更好。
Disadvantage: every thread wants its own variable to fill, so it is a better fit for a fixed, small amount of threads, otherwise you are allocating from the heap and have won nothing, just the opposite: it would be more complicated to keep hold off all the malloc()/free()
. David Schwartz's method would be much more apt in that case.
缺点:每个线程都需要自己的变量来填充,因此它更适合固定的少量线程,否则你从堆中分配并没有赢得任何东西,恰恰相反:保持持久会更复杂关闭所有malloc()/ free()。在这种情况下,大卫施瓦茨的方法会更加贴切。