This question already has answers here:
MinGW 4.8.1 C++11 thread support
(2 answers)
Closed 7 years ago.
I have a problem including the thread library. The following code:
#include <string>
#include <iostream>
#include <thread>
using namespace std;
//The function we want to make the thread run.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
//Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
Produces these errors:
The code is taken from the answer to another stackoverflow question. I am fairly new to codeblocks and C++ so please explain to me what I am doing wrong.
You probably have not set proper flags for compiler (so that it uses c++11). Way of doing it in codeblocks
Related
This question already has an answer here:
SFML fails in multithreading
(1 answer)
Closed 1 year ago.
I am trying to create a separate thread while using the SFML game library. This is a very simplified example.
I'm using WSL and the libsfml-dev library. My issue is that it seems like I cannot perform certain actions using the SFML library on threads other then my main thread. For example, in the code below, I get an error if I click escape. What's supposed to happen is the program simply exits as the loop conditions fail.
If you want to run the code yourself...
to grab the library: apt-get install libsfml-dev
g++ compiler arguments: g++ test.cpp -pthread -lsfml-graphics -lsfml-window -lsfml-system
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
// Create window object
sf::RenderWindow window(sf::VideoMode(800, 800), "");
using namespace std;
void *func(void *threadid) {
// Show thread was created
cout << "New thread was ran." << endl;
while (window.isOpen()) {
// This action kills the program and not in a nice way
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
}
pthread_exit(NULL);
}
int main() {
// Create new thread
pthread_t threads[1];
int val;
val = pthread_create(&threads[0], NULL, func, (void *)1);
cout << "Main thread was ran." << endl;
// This is here so that the program doesn't exit
while (window.isOpen()) {}
return 0;
}
Error message
It is not uncommon to have to do all GUI updates from the main thread. It is likely that SFML is not even thread-safe. You need to communicate from your other threads to the main thread and let them do things like window.close().
If I modify your example and simply return from the second thread instead of calling window.close(), it does not crash.
This question already has answers here:
CodeBlocks c++ - Can't use Thread because the compiler doesn't support it
(1 answer)
Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?
(1 answer)
Closed 4 years ago.
So I started to learn c++ and I come across multithreading, but there is one problem: It gives me the error main.cpp|12|error: 'thread' was not declared in this scope although I have included the <thread> library in my code.
I use Code::Blocks and GNU GCC Complier on Windows.
Here is an example of my code:
#include <iostream>
#include <thread>
using namespace std;
void test(){
cout<<"hello, I am a thread !";
}
int main(){
thread t1(test);
return 0;
}
This question already has answers here:
What are the rules about using an underscore in a C++ identifier?
(5 answers)
Closed 7 years ago.
I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the name "_end" very special in C/C++ that causes such problem? Or this can be a very serious bug... (The code is attached below, and it breaks in g++ 4.3.2, 4.5.2, 4.9.2, etc.)
#include <iostream>
using namespace std;
int _end[1111];
int main() {
for (int i=0; i<1111; i++) {
cout << i << endl;
_end[i]++;
}
return 0;
}
You can see the result at https://ideone.com/XAcUeZ.
See here also for the C compiler.
Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.
My thread program is:
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
First, are you on windows or linux?
If you are on linux, you must compile with C++11 support. Just pass -std=c++11 to g++.
I can't help you with windows.
Your IDE may not support C++11 yet. since thread is included in the standard since C++11. See this thread for CodeBlocks? http://forums.codeblocks.org/index.php?topic=15536.0
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stop GDB from executing “break main” by default in Eclipse?
I decided to learn C++ (because Java isn't as good at certain things) so I wrote a (VERY) simple program.
#include <iostream>
using namespace std;
int main() {
cout << "This is a test" << endl;
int test = 100;
cout << test << endl;
return 0;
}
It worked as expected, but Eclipse seems to be adding an automatic breakpoint at the main method:
How can I stop this automatic breakpoint insertion?
There's an option in the debug configuration that allows you to Stop on startup at (or simillar), which is enabled by default. Disable it.