【发布时间】:2012-10-13 20:48:19
【问题描述】:
我要小程序
第一
// compile with -lpthread
// TEST:
// basename
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <limits.h>
#include <inttypes.h>
// DATASET_LEN
#ifndef DATASET_LEN
#define DATASET_LEN 10000
#endif
// THREADS_NUM
#ifndef THREADS_NUM
#define THREADS_NUM 16
#endif
// need to call free(3) after
char** generateArray() {
char** dataset = (char**)malloc(sizeof(char*) * DATASET_LEN);
// fill dataset
for (size_t i = 0; i < DATASET_LEN; ++i) {
dataset[i] = (char*)malloc(sizeof(char) * CHAR_MAX);
sprintf(dataset[i], "%i/%i/", rand(), rand());
}
return dataset;
}
// pthread_create(3) callback
void* run(void* args) {
char** dataset = generateArray();
char* baseName;
for (size_t i = 0; i < DATASET_LEN; ++i) {
baseName = basename(dataset[i]);
printf("%s\n", baseName);
free(dataset[i]);
}
free(dataset);
}
// main
int main(int argc, char** argv) {
pthread_t* threads = (pthread_t*)malloc(sizeof(pthread_t) * THREADS_NUM);
// threads start
for (int i = 1; i <= THREADS_NUM; ++i) {
pthread_create(&threads[i-1], NULL, run, NULL);
fprintf(stderr, "Thread %u started\n", i);
}
// threads join
for (int i = 1; i <= THREADS_NUM; ++i) {
pthread_join(threads[i-1], NULL);
fprintf(stderr, "Thread %u finished\n", i);
}
free(threads);
return EXIT_SUCCESS;
}
第二:
// compile with -lpthread
// TEST:
// basename
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <limits.h>
#include <inttypes.h>
#include <string>
// DATASET_LEN
#ifndef DATASET_LEN
#define DATASET_LEN 10000
#endif
// THREADS_NUM
#ifndef THREADS_NUM
#define THREADS_NUM 16
#endif
// need to call free(3) after
char** generateArray() {
char** dataset = (char**)malloc(sizeof(char*) * DATASET_LEN);
// fill dataset
for (size_t i = 0; i < DATASET_LEN; ++i) {
dataset[i] = (char*)malloc(sizeof(char) * CHAR_MAX);
sprintf(dataset[i], "%i/%i/", rand(), rand());
}
return dataset;
}
// pthread_create(3) callback
void* run(void* args) {
char** dataset = generateArray();
char* baseName;
std::string tmpStr;
for (size_t i = 0; i < DATASET_LEN; ++i) {
baseName = basename(dataset[i]);
tmpStr = std::string(baseName);
printf("%s\n", tmpStr.c_str());
free(dataset[i]);
}
free(dataset);
}
// main
int main(int argc, char** argv) {
pthread_t* threads = (pthread_t*)malloc(sizeof(pthread_t) * THREADS_NUM);
// threads start
for (int i = 1; i <= THREADS_NUM; ++i) {
pthread_create(&threads[i-1], NULL, run, NULL);
fprintf(stderr, "Thread %u started\n", i);
}
// threads join
for (int i = 1; i <= THREADS_NUM; ++i) {
pthread_join(threads[i-1], NULL);
fprintf(stderr, "Thread %u finished\n", i);
}
free(threads);
return EXIT_SUCCESS;
}
这两个程序在 linux 上都能正常工作,但在 freebsd 上(没有 std::string)不能工作
谁能解释一下原因?
我在/usr/src/lib/libc/gen/basename.c 看到了freebsd src,并在函数中看到了一个静态变量。
但正因为如此,用 std::string 程序也不能正常工作
我的意思是,它只输出数字和新行
对于我使用的测试:
./freebsd-threaded-basename | egrep -av '^[0-9\n\s]+$' | env LANG=c less
UPD 我尝试使用 strdup() 或 strcpy() 结果是一样的 - 不正常
UPD *每个*运行带有 std::string 的版本时,它按预期工作
【问题讨论】:
-
一个小点:为什么不在
std::string的版本中直接赋值给tmpStr呢?喜欢tmpStr = basename(dataset[i]); -
因为我写这个小测试程序很快,我只是用你建议的那句话测试这段代码,结果是一样的