我无法让 pthread_equal()
匹配存储的 pthread。与从 pthread_create()
获得的相比,我从 pthread_self()
获得的 pthread_t
似乎被截断了 4 个字节。比较在最后一个代码块中!
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <errno.h>
#define QUEUE_SIZE 10
pthread_t *threads_array;
int abc;
void server_loop();
void *a_loop(void *abc);
void printThreadID(pthread_t id);
int main(int argc, char **argv) {
server_loop();
return 0;
}
void server_loop() {
pthread_t thread_id;
threads_array = malloc(QUEUE_SIZE * sizeof(pthread_t));
int i;
for (i = 0; i < QUEUE_SIZE; i++) {
if (pthread_create(&thread_id, NULL, a_loop, (void*)&abc) < 0) {
perror("Thread creation failed");
fprintf(stderr, "Thread creation #%d failed\n", i);
printThreadID(thread_id);
exit(1);
}
//... store threadID in array
fprintf(stderr, "Thread created: ");
printThreadID(thread_id);
threads_array[i] = thread_id;
}
sleep(5);
}
void *a_loop(void *abc) {
fprintf(stderr, "@@@@ ");
printThreadID(pthread_self());
//... find index of matching pthread_equal(pthread_self(), array[i])
while (1) {
}
return 0;
}
// HELPER METHOD
void printThreadID(pthread_t id){
size_t i;
for (i = sizeof(i); i; --i)
fprintf(stderr, "%02x", *(((unsigned char*) &id) + i - 1));
fprintf(stderr, "\n");
}
因此,尝试遍历存储的 pthread 并与 pthread_equals 进行比较的尝试失败了。 一些比较(全部 10 个,每次都截断前 4 个字节):
Thread created ID: 00007fda1e402700
self(): 000000001e402700
Thread created ID: 00007fda1dc01700
self(): 000000001dc01700
Thread created ID: 00007fda1cbff700
self(): 000000001cbff700
// Sometimes the zeroes are replaced with ones:
Thread created ID: 00007fda1e402700
self(): ffffffff1e402700
Thread created ID: 00007fda1dc01700
self(): ffffffff1dc01700
Thread created ID: 00007fda1cbff700
self(): ffffffff1cbff700
知道为什么会这样吗?
最佳答案
这个函数体是问题的根源:
size_t i;
for (i = sizeof(i); i; --i)
{
fprintf(stderr, "%02x", *(((unsigned char*) &id) + i - 1));
}
pthread_t
的大小与“size_t”不同,代码使用 sizeof(i)
打印 pthread_t< 的字节
。建议:
for (i = sizeof(pthread_t); i; --i)
{
fprintf(stderr, "%02x", *(((unsigned char*) &id) + i - 1));
}
尽管不使用循环的更好方法是:
fprintf( stderr, "%*x", (int)(2*sizeof(pthread_t)), (int)id );
警告:下面的代码编译不干净。它输出有关未使用参数 abc
因此,使用“改进”函数:printThreadID()
产生输出的程序是:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <errno.h>
#define FROM_CREATE (0)
#define FROM_THREAD (1)
#define QUEUE_SIZE (10)
pthread_t *threads_array;
int abc;
void server_loop( void );
void *a_loop(void *abc);
void printThreadID( int, pthread_t );
int main( void )
{
server_loop();
return 0;
}
void server_loop()
{
pthread_t thread_id;
if( NULL == (threads_array = malloc(QUEUE_SIZE * sizeof(pthread_t)) ))
{
perror( "malloc for thread array failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
for (int i = 0; i < QUEUE_SIZE; i++)
{
if (pthread_create(&thread_id, NULL, a_loop, (void*)&abc) < 0)
{
perror("Thread creation failed");
fprintf(stderr, "Thread creation #%d failed\n", i);
printThreadID(FROM_CREATE, thread_id);
exit( EXIT_FAILURE );
}
// implied else, pthread_create successful
//... store threadID in array
//fprintf(stderr, "Thread created: ");
printThreadID(FROM_CREATE, thread_id);
threads_array[i] = thread_id;
}
sleep(5);
}
void *a_loop(void *abc)
{
//fprintf(stderr, "@@@@ ");
printThreadID( FROM_THREAD, pthread_self());
//... find index of matching pthread_equal(pthread_self(), array[i])
while (1)
{
}
return 0;
}
// HELPER METHOD
void printThreadID(int fromThread, pthread_t id)
{
//size_t i;
//for (i = sizeof(i); i; --i)
//{
// fprintf(stderr, "%02x", *(((unsigned char*) &id) + i - 1));
//}
if( FROM_THREAD == fromThread )
{
fprintf( stderr, "@@@@ %ld\n", (long int)id );
}
else
{
fprintf( stderr, "thread created: %ld\n", (long int)id );
}
//fprintf(stderr, "\n");
}
这是程序典型运行的输出,请注意输出的确切顺序取决于赢得比赛的线程。
thread created: 140506339571456
@@@@ 140506339571456
thread created: 140506331178752
thread created: 140506322786048
thread created: 140506314393344
thread created: 140506306000640
thread created: 140506297607936
thread created: 140506289215232
thread created: 140506280822528
thread created: 140506272429824
thread created: 140506264037120
@@@@ 140506331178752
@@@@ 140506297607936
@@@@ 140506322786048
@@@@ 140506289215232
@@@@ 140506280822528
@@@@ 140506314393344
@@@@ 140506272429824
@@@@ 140506264037120
@@@@ 140506306000640
关于C - pthread_self() 被截断前 4 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33950711/