I have checked many StackOverflow posts, but no answers solve my problem.
I get 2 errors:
g++ .\main.cpp -fopenmp -o test
.\main.cpp:12:14: error: 'std::this_thread' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
.\main.cpp:12:37: error: 'chrono' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
My current G++ version is:
g++.exe (MinGW.org GCC Build-2) 9.2.0
The code is:
#include <iostream>
#include <chrono>
#include <thread>
#include <omp.h>
int main()
{
omp_set_num_threads(4);
#pragma omp parallel
{
std::this_thread::sleep_for(chrono::seconds(20000) );
std::cout << "Number of available threads: " << omp_get_num_threads() << std::endl;
std::cout << "Current thread number: " << omp_get_thread_num() << std::endl;
std::cout << "Hello, World!" << std::endl;
}
return 0;
}
I have already tried -std=c++11 from 11 & 14 & 17.
I'm not sure it's right but about your second error can you replace it with
std::this_thread::sleep_for(chrono::seconds(20000) );
to
std::this_thread::sleep_for(std::chrono::seconds(20000));
I think the first error depends from second, but I'm not sure.
Related
There is a problem when using std::condition_variable and compiling with g++ on Linux which has glibc-2.30. But it does not run on RHEL 7 with glibc-2.17. The reason is std::condition_variable::wait_for() is linked to pthread_cond_clockwait() function, which is introduced in glibc-2.30. The gcc packge was built from source with glibc-2.30.
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/std_mutex.h :
...
#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
void
wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time)
{
pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
&__abs_time);
}
#endif
...
https://en.cppreference.com/w/cpp/thread/condition_variable/wait_for :
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
std::condition_variable cv;
std::mutex cv_m;
int i;
void waits(int idx)
{
std::unique_lock<std::mutex> lk(cv_m);
if(cv.wait_for(lk, idx*100ms, []{return i == 1;}))
std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
void signals()
{
std::this_thread::sleep_for(120ms);
std::cerr << "Notifying...\n";
cv.notify_all();
std::this_thread::sleep_for(100ms);
{
std::lock_guard<std::mutex> lk(cv_m);
i = 1;
}
std::cerr << "Notifying again...\n";
cv.notify_all();
}
int main()
{
std::thread t1(waits, 1), t2(waits, 2), t3(waits, 3), t4(signals);
t1.join();
t2.join();
t3.join();
t4.join();
}
The source code was copied to a file test.cpp and compiled with a small script build.sh.
build.sh:
#!/usr/bin/env bash
/opt/gcc-11.3.0/bin/g++ \
-std=c++17 -O3 -pthread -static-libstdc++ -static-libgcc glibc_test.cpp -o test_glibc
Then test_glibc is copied to RHEL 7 and ldd test_glibc has delivered this issue:
./test_glibc: /lib64/libpthread.so.0: version `GLIBC_2.30' not found (required by ./test_glibc)
The only solution I have found was to modify the configure file in gcc/libstdc++-v3 folder and compile the gcc package again. glibcxx_cv_PTHREAD_COND_CLOCKWAIT=yes is modified to glibcxx_cv_PTHREAD_COND_CLOCKWAIT=no
if ac_fn_cxx_try_compile "$LINENO"; then :
glibcxx_cv_PTHREAD_COND_CLOCKWAIT=no
else
glibcxx_cv_PTHREAD_COND_CLOCKWAIT=no
fi
and
if ac_fn_cxx_try_link "$LINENO"; then :
glibcxx_cv_PTHREAD_COND_CLOCKWAIT=no
else
glibcxx_cv_PTHREAD_COND_CLOCKWAIT=no
fi
So my question, is there any other solution to solve the problem with incompatible newer glibc functions to older versions?
The program get killed as soon as I inoke GetFamilyCount() so the "Exit" is not printed.
Invoking GetLastStatus() prints value 18 which is equals to GdiplusNotInitialized
I'm not sure why it's not initialized. You can clearly see that I initialized it here,
Gdiplus::PrivateFontCollection privateFontCollection;
I'm using MinGW to build the program.
MinGW-w64 9.0.0
GCC Version 11.1
OS is Windows 10
MinGW taken from http://winlibs.com/
#include <iostream>
#include <Windows.h>
#include <Gdiplus.h>
int main() {
std::cout << "Start" << "\n";
Gdiplus::PrivateFontCollection privateFontCollection;
std::cout << privateFontCollection.GetLastStatus() << "\n";
std::cout << privateFontCollection.GetFamilyCount() << "\n";
std::cout << "Exit" << "\n";
}
I'm trying to diagnose a timing issue on a multi-core processor [Xeon Silver]. I think that the clocks have not been configured or synced between the processor. I'm using Eli Bendersky's [credited in the code snippet] threading examples to build a test instrument. I have made three changes. I made made the sleep occur first, and I added a call to std::chrono::system_clock::now() and tried to print it out. I'm building with gcc 4.8.5 on CentOS 7.5.
The code is as follows:
// // Eli Bendersky [http://eli.thegreenplace.net]
// This code is in the public domain.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <pthread.h>
int main(int argc, const char** argv) {
unsigned num_cpus = std::thread::hardware_concurrency();
std::cout << "Launching " << num_cpus << " threads\n";
// A mutex ensures orderly access to std::cout from multiple threads.
std::mutex iomutex;
std::vector<std::thread> threads(num_cpus);
for (unsigned i = 0; i < num_cpus; ++i)
{
threads[i] = std::thread([&iomutex, i]
{
// Simulate important work done by the tread by sleeping for a bit...
std::this_thread::sleep_for(std::chrono::milliseconds(200));
{
std::chrono::time_point ti = std::chrono::system_clock::now();
// Use a lexical scope and lock_guard to safely lock the mutex only for
// the duration of std::cout usage.
std::lock_guard<std::mutex> iolock(iomutex);
std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";
}
});
}
for (auto& t : threads) {
t.join();
}
return 0;
}
I build with make:
CXX = g++
CXXFLAGS = -std=c++11 -Wall -O3 -g -DNDEBUG -pthread
LDFLAGS = -lpthread -pthread
clock-check: clock-check.cpp
$(CXX) $(CXXFLAGS) $^ -o $# $(LDFLAGS)
GCC gives me the following error:
[user#sbc1 concur]$ make clock-check
g++ -std=c++11 -Wall -O3 -g -DNDEBUG -pthread clock-check.cpp -o clock-check -lpthread -pthread
clock-check.cpp: In lambda function:
clock-check.cpp:32:67: error: ‘ti’ was not declared in this scope
std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";
^
make: *** [clock-check] Error 1
ti is clearly in the same block scope as the print statement, and I'm baffled why the compiler is complaining. I have not found any restrictions on variables local to the lambda. Most of what I have found has been references to captures.
Your problem lies in this line:
std::chrono::time_point ti = std::chrono::system_clock::now();
std::chrono::time_point expect a type argument (e.g. std::chrono::time_point<std::chrono::system_clock>)
Prefer to use auto in this case:
auto ti = std::chrono::system_clock::now();
Then, you'll have an error since you try to output a std::chrono::duration in an output stream.
You should do:
std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch().count() << "\n";
It seems to be a bug in older gcc versions. With gcc 10.1 (--std=c++11) I get the error:
<source>: In lambda function:
<source>:23:34: error: missing template arguments before 'ti'
23 | std::chrono::time_point ti = std::chrono::system_clock::now();
| ^~
<source>:27:67: error: 'ti' was not declared in this scope; did you mean 'i'?
27 | std::cout << "Thread #" << i << " hit its clock at: " << ti.time_since_epoch() << "\n";
| ^~
|
The error about the missing template parameter (which is missing with gcc 4.5.8) on the declaration explains the second error.
Strangely gcc 4.8.5 with -std=c++11 happily compiles the code if you remove the line with std::cout: https://godbolt.org/z/6LREHF
Class template deduction is not available until c++17, so you need to specify your template parameters for chrono::timepoint. Alternatively, use auto:
auto ti = std::chrono::system_clock::now();
Furthermore, you attempt to stream a chrono::duration type, which is not possible. Use ti.time_since_epoch().count().
Live example
So I'm working on a project for a class and I cannot seem to get things to work. 1) Did I do this right? 2) How do I get rid of the errors?
#include <iostream>
#include <thread>
using namespace std;
void countdown(){
int count;
count = 21;
while (count<=0)
{
count--;
cout << "Count is " << count << '.' << endl;
}
}
int main() {
std::thread t1(countdown);
t1.join();
int count1;
count1 = 0;
while (count1<20)
{
count1++;
cout << "Count is " << count1 << '.' << endl;
}
return 0;
}
Error messages:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -Wl,--no-as-needed -o "src\\Critical7.o" "..\\src\\Critical7.cpp"
..\src\Critical7.cpp: In function 'int main()':
..\src\Critical7.cpp:27:2: error: 'thread' is not a member of 'std'
std::thread t1(countdown);
^
..\src\Critical7.cpp:28:2: error: 't1' was not declared in this scope
t1.join();
I've tried setting things the way other posts have said but I can't seem to get it to work.
Two modifications are necessary to run the code correctly:
replace while (count<=0) with while(count>=0), else the loop in countdown() exits immediately.
Use the -pthread linker option in order to compile the code.
I was following this beginner tutorial on boost threads:
http://www.codeproject.com/Articles/279053/How-to-get-started-using-Boost-threads
Everything was going fine with this sample they provided:
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
But then I decided to try it without the sleep function. So I commented out those lines.
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
//boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
//boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
I started to get the following compile error:
)]+0x40)||undefined reference to `_gmtime32'|
I've done quite a bit of snooping around trying to figure out what this means and why it is only happening when I remove those two lines. As of right now, I am leaning towards it being something to do with some kind of header, like time.h, that I have to include (although I tried that already obviously).
I am using a rather strange setup. Code::Blocks with mingw/gcc. I compiled the boost library myself with command line arguments following the codeblocks tutorial:
http://wiki.codeblocks.org/index.php?title=BoostWindowsQuickRef
Everything seemed to work fine with this, but I suppose I could have built the libraries incorrectly.
I would dig deeper but the file name of the error is not standard and is listed as ")]+0x40)". I am not sure what this means - is it maybe some kind of file location address?
MORE INFO:
Windows XP 32 bit
CodeBlocks 10.05
MingW GCC 4.4.3
Boost 1_53_0
BUILD LOG:
Linking console executable: bin\Debug\Bjarne_Strousup_Samples.exe
....\CodeBlocks\lib\libboost_thread-mgw44-mt-1_53.a(thread.o):thread.cpp:(.text$_ZN5boost9date_time6c_time6gmtimeEPKlP2tm[boost::date_time::c_time::gmtime(long
const*, tm*)]+0x40): undefined reference to `_gmtime32' collect2: ld
returned 1 exit status Process terminated with status 1 (0 minutes, 6
seconds) 1 errors, 0 warnings
COMMAND LINE ATTEMPT:
C:\CodeBlocks Tests\BoostExamples>g++ main.cpp -lboost_thread -lboost_system -lb
oost_chrono
main.cpp:5:28: error: boost/thread.hpp: No such file or directory
main.cpp:6:31: error: boost/date_time.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:21: error: 'boost' has not been declared
main.cpp:21: error: expected ';' before 'workerThread'
main.cpp:24: error: 'workerThread' was not declared in this scope
C:\CodeBlocks Tests\BoostExamples>
LINKER SETTINGS:
IDM Link:
http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Fctime.htm
I think the error is on this line of c_time.hpp:
static std::tm* gmtime(const std::time_t* t, std::tm* result)