Multy thread Global Variable

Hello everyone.

In my program i have main() which runs 2 different cpp files, of which one reads info from TCP and the other uses information to control movement. My problem is, how in real time (variable from 'receive' changes 125 times in a second) share global variable info between 2 cpp scripts.

main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <windows.h>
#include <process.h>         // needed for _beginthread()
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <limits.h>

using namespace std;
void send(void *P);
void receive(void *P);
int brojac = 0;
double faza;

int main()
{

	printf("Now in the main() function.\n");

	HANDLE hThreads[2];
	hThreads[0] = (HANDLE)_beginthread(send, 0, (void*)0);
	hThreads[1] = (HANDLE)_beginthread(receive, 0, (void*)0);



	WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);

	Sleep(2000);
}




Now, in my 'receive' file i read status from server, and i would love to share it with 'send' file. How do i do that?

Thank you a lot in advance.

Kind regards.
These 'send' and 'receive' files are part of the same executable...
Simply declare a global variable in your main.cpp and both of them should see it.
You need to do an extern declaration for the global variable in the other files.
Here is a link to a question on stackoverflow that shows why extern is necessary and how to use it
http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c
I would also recommend that you should know how #include works before diving into that example code. #include "file" or #include <file> both do the same thing, they look for the file named 'file' anywhere in the list of folders that your compiler has been configured to include. When the file 'file' is found the text of 'file' is put in the same place that #include "file" was. So, in other words, it replaces the #include with the contents of the file. Knowing this should help you see where the declarations are coming from in the example code.
Thank you for your answer.

I tried what you said, i used extern and everything, thing is, source file receive get target force 125 times in a second, and file 'send' gets it online once, on start, original state, it doesn't change. I tryed with main source and everyhing, still nothing. I need to communicate iner-process. All source files are part of 1 executable. I would really appriciate it if you could give me a hand. I'm really inexperienced in C++.

Thank you, kind regards.
What you need is condition_variable, see:

http://www.cplusplus.com/reference/condition_variable/

There are examples and all, but I suggest that you make a test project in order to get used to this.
Declare the shared variable as volatile, e.g., extern volatile int myVar;. This tells the optimizer that the variable can change, even if a section of code indicates that it won't. That prevents the optimizer from optimizing away accesses to the variable.

If it's anything larger than an int, then you will need to protect it with a mutex lock. This guards against the following:
thread 1 starts to update the variable
thread 2 reads the partially updated (and thus invalid) value in the variable
thread 1 finishes updating the variable.
thread 2 crashes because it has no idea what the gibberish that it read really is.
Within a thread of execution, accesses (reads and writes) to all volatile objects are guaranteed to not be reordered relative to each other, but this order is not guaranteed to be observed by another thread, since volatile access does not establish inter-thread synchronization.
In addition, volatile accesses are not atomic (concurrent read and write is a data race) and do not order memory (non-volatile memory accesses may be freely reordered around the volatile access).
...
Standard volatile semantics are not applicable to multithreaded programming, although they are sufficient for e.g. communication with a std::signal handler.
http://en.cppreference.com/w/cpp/atomic/memory_order


Atomic objects are the only C++ objects free of data races; that is, if one thread writes to an atomic while another thread reads from it, the behavior is well-defined.
http://en.cppreference.com/w/cpp/atomic
Topic archived. No new replies allowed.