이 웹 사이트 http://theboostcpplibraries.com/boost.lockfree에서 예제 46.3의 변형을 실행하고 싶습니다. 나는 리눅스 시스템에있다.실행 부스트 : 여러 파일에 걸쳐 전역으로 빈 큐를 잠급니다.
대기열 q를 헤더 파일에 정의하고 싶습니다. 나는 생산물을 가지고 있고 기능을 다른 파일에 사용하고 싶습니다.
void *produce(void*)
{
for (int i = 1; i <= 10000; ++i)
q.push(i);
}
나는
을 consume.cpp이 좋아 포함됩니다 : 그래서 내가 다음 produce.cpp을하고 싶은static boost::lockfree::queue<int> q{100};
static std::atomic<int> sum{0};
void *produce (void*);
void *consume (void*);
를 포함 global.h하고 싶은 포함 그때 내 MAIN.CPP을하고 싶은
void *consume (void*)
{
int i;
while (q.pop(i))
sum += i;
}
는
#include iosteam
#include iomanip
#include global
#include pthread
int main()
{pthread_t t1;
pthread_t t2;
pthread_t t3;
int t1_iret;
t1_iret = pthread_create(&t1, NULL, produce, NULL);
if(t1_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t1_iret);
exit(EXIT_FAILURE);
}
int t2_iret;
t2_iret = pthread_create(&t2, NULL, consume, NULL);
if(t2_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t2_iret);
exit(EXIT_FAILURE);
}
int t3_iret;
t3_iret = pthread_create(&t3, NULL, consume, NULL);
if(t3_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t3_iret);
exit(EXIT_FAILURE);
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0; }
를 포함
또한 정수가 아닌 문자열로 설명 된 것을 수행 할 수 있는지 궁금합니다.
EDIT1 : 나는 시도 할 때 큐는 내가 할 문자열의 큐 될 ::
/usr/local/include/boost/lockfree/queue.hpp : '클래스 부스트 :: 리터 ockfree의 인스턴스에서 :: queue> ': /home/ubuntu/Project/src/main.cpp:15:37 : 여기에서 필요합니다 /usr/local/include/boost/lockfree/queue.hpp:87:5 : 오류 : 정적 어설 션이 실패했습니다 : (boost :: has_trivial_destructor :: value) BOOST_STATIC_ASSERT ((boost :: has_trivial_destructor :: value)); (boost :: has_trivial_assign :: value) BOOST_STATIC_ASSERT (0120) ; ^ /usr/local/include/boost/lockfree/queue.hpp:21:0, /home/ubuntu/Project/src/main.cpp:5의 에서 파일에 있음 : /usr/local/include /boost/lockfree/detail/copy_payload.hpp : 'static void boost :: lockfree :: detail :: copy_constructible_and_copyable :: copy (T &, U &)의 인스턴스 생성시 [T = std :: basic_string; U = int] ': /usr/local/include/boost/lockfree/detail/copy_payload.hpp:49:25 :'void boost :: lockfree :: detail :: copy_payload (T &, U &)에서 필요합니다. T = std :: basic_string; U = 정수] ' /usr/local/include/boost/lockfree/queue.hpp:402:61 :'bool boost :: lockfree :: queue :: pop (U &)에서 필요합니다. [with U = int; T = std :: basic_string; A0 = boost :: parameter :: void_; A1 = boost :: parameter :: void_; A2 = boost :: par ameter :: void_] ' /home/ubuntu/Project/src/main.cpp:21:24 : 여기에서 필요합니다. /usr/local/include/boost/lockfree/detail/copy_payload.hpp : 38 : 11 : 오류 : 'std :: basic_string'유형에서 'int'유형으로의 유효하지 않은 캐스트 u = U (t);
당신은global.h
에, 당신의 변수를 정의 선언,하지만 필요
음, 시도해 보니 두 가지 문제가 있습니다. 1) 글로벌 큐가 헤더 파일에서 업데이트되지 않는 것 같습니다. 2) 게다가, 나는 문자열의 대기열을 만들 수 없습니다. –
@ArielBaron : 확인해 주셔서 감사합니다. 수정 (1)에 대한 내 대답을 업데이 트했습니다. (2)에 관해서, "할 수 없다"는 것은 무엇을 의미합니까? 오류 메시지가 나타 났습니까? –
2) 오류가 발생하여 1) 대기열이 채워지지 않음을 나타내는 0이 표시됩니다. –