Hello i have written a program with threads and its doing some weird things i dont understand. i think its an error somewhere but i don't know where could someone explain what's going on ?
Your two threads are reading and writing to the same object (the global variable x) without synchronization. This is a form of undefined behavior known as "data race". Anything at all can happen.
In addition, of course, they are running at the same time, so there is no reason after some thread prints 1, to expect the main thread to be the next to add ten and to print 11 while your second thread would wait.
so using a mutex would solve the problem or not ? or do i have to do something else to avoid that data race ?
i used this code now and it still doesnt really work :/
That eliminates the error, but it doesn't make it print what you expected, of course, since it has no possible way to do so: as written, it will either print the ten numbers from the main thread, then the ten numbers from the second thread, or the other way around.
To produce the output of 1, 11, 12, 22, 23, 33, ..., your threads need to communicate: the second thread should begin waiting for the message from the main thread. the main thread should increment x and print "1" and then message the second thread and begin waiting for the message from the second thread. The second thread, once the message comes, can then add 10 to x, print 11, message the main thread and begin waiting for the message from the main thread, and so on. I don't know SFML (does it have semaphores?), but in plain C++ this can be done with condition variables and mutexes.