ずに私が持っているのstd ::文字列を使用して、正しいベース名のFreeBSDの作業にではなく、
まず
// 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を見て、静的なvar in関数を見てください。
しかし、それのために、STD ::文字列のプログラムでも必要がない私が意味する、通常では
通常動作しますが、それは出力のみの数字、そして新しいラインのテストのために
は、私が使用します。 ./freebsd-threaded-basename | egrep -av '^[0-9\n\s]+$' | env LANG=c less
UPD私は(のstrdupを使用しようとする)やstrcpyの()結果は同じである - 正常でない
UPD * すべて *時間のstd ::文字列とバージョンが実行されますLinux manual page on pthreadsから
小さな点: 'std :: string'のバージョンで' tmpStr'に直接割り当てるのはなぜですか? 'tmpStr = basename(dataset [i]);' –
のように私はこの小さなテストプログラムを速く書いたので、私はちょうどあなたのアドバイス、結果は同じであることをこのコードでテストします – azat