我无法让pthread_equal()
与匹配存储的pthreads相匹配。我从pthread_t
获得的pthread_self()
似乎被截断了4个字节,而我从pthread_create()
得到的字节。比较在最后一个代码块中!
#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进行比较失败。 一些比较(所有十次,每次都被截断前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
知道为什么会这样吗?
答案 0 :(得分:1)
这个函数体是问题的根源:
size_t i;
for (i = sizeof(i); i; --i)
{
fprintf(stderr, "%02x", *(((unsigned char*) &id) + i - 1));
}
pthread_t
与&#39; size_t&#39;的大小不同并且代码使用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
所以,通过改进&#39; function: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