how to create variable for all thread in c++


 
Thread Tools Search this Thread
Top Forums Programming how to create variable for all thread in c++
# 1  
Old 04-27-2009
how to create variable for all processes in c++

hello
I have this code
Code:
	
        #include <sys/types.h>
	#include <unistd.h>			
	#include <iostream>
	#include <pthread.h>
	#include<cstring>
	using namespace std;

	

	int var1=0;  
   
	void doSomething()  
	{
		var1 = 5;
		cout<<"Do S :"<<var1<<endl;	
		sleep(1);
		var1 =7;

		cout<<"Do S :"<<var1<<endl;	
		sleep(1);

		var1 =10;
		cout<<"Do S l :"<<var1<<endl;	
		sleep(1);
     
	 }  
   
	int main()  
	 {  
   
	     if (fork() != 0)  
		     doSomething();   
		 else
		     {
			cout << var1 << endl; sleep(1);
			cout << var1 << endl; sleep(1);
			cout << var1 << endl; sleep(1);
			cout << var1 << endl; sleep(1);
			}
   
		  return 0;  
	 }

my problem is
when doSomthing function change the variable var1 at the child process it doesn't change at the parent process

is there any way to make a global variable for all processes

thanks

Last edited by vip_a1; 04-28-2009 at 11:41 AM..
# 2  
Old 04-27-2009
The compiler may have optimized it away. It sees 'hmm, this value never changes! Therefore I can assume it is a hardcoded zero' and does so. If you instead declare it as 'volatile int var1' then the compiler will not make such assumptions.
# 3  
Old 04-28-2009
thanks corona but volatile doesn't work too Smilie
# 4  
Old 04-28-2009
Whoa. Total botch there. I thought you were using threads for some reason. Sorry.

The variable doesn't carry across seperate processes because they don't share that memory; to get shared memory we have to explicitly create some. Like so:
Code:
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <pthread.h>
#include<cstring>
using namespace std;

int *mem=(int *)MAP_FAILED;

#define var1 (mem[0])

void doSomething()
{
        var1 = 5;
        cout<<"Do S :"<<var1<<endl;
        sleep(1);
        var1 =7;

        cout<<"Do S :"<<var1<<endl;
        sleep(1);
        var1 =10;
        cout<<"Do S l :"<<var1<<endl;
        sleep(1);
}

int main()
{
        // Create shared memory.  Has to be done in blocks of pagesize.
        // So it could probably hold lots of integers, not just one.
        mem=(int *)mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
                MAP_SHARED|MAP_ANONYMOUS, -1, 0);
        if(mem == MAP_FAILED)
                return(1);

        if (fork() != 0)
             doSomething();
        else
        {
                cout << var1 << endl; sleep(1);
                cout << var1 << endl; sleep(1);
                cout << var1 << endl; sleep(1);
                cout << var1 << endl; sleep(1);
        }

        return 0;
}

# 5  
Old 04-28-2009
it works Smilie

thank you Corona alot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Unable to create new thread

Hi. I registered, but when trying to post, I get ... "Sorry, you do not have enough Bits to post a new thread" Google & Forum search showed some hits, but no solution. Please advide. Thanks! (1 Reply)
Discussion started by: gehe
1 Replies

2. Forum Support Area for Unregistered Users & Account Problems

I am still meet a problem when I want to reply or create a new thread

Hi, Scott, I am still meet a problem when I want to reply or create a new thread in "Shell Programming and Scripting" Forum, It reminds that: To create new threads in this forum your post count must be 10 or greater. You currently have 2 posts. To my surprise, I posted a thread in "Shell... (4 Replies)
Discussion started by: weichanghe2000
4 Replies

3. UNIX for Advanced & Expert Users

Pthread attr setting doesn't work before thread create?

Hello everyone, I created a test program for pthread priority set. Here's the code, very simple, 60 lines only. I've tried this prog on my Fedora 13(on vbox), and on my 6410 arm linux 2.6.36. Both the same result. Both environments are using root privileges. Can any body tells me why the... (15 Replies)
Discussion started by: ss1969
15 Replies

4. Post Here to Contact Site Administrators and Moderators

How to Create a New Thread

please tell me how can i post a new post. i am not getting any option to post a new question. any suggestion is highly appreciated. thanks, Arpit (1 Reply)
Discussion started by: thearpit
1 Replies

5. Programming

Unable to create a UDP client from thread?

I try to initial a UDP client from threading, but it doesn't work? why? These codes from the textbook #define ECHOMAX 255 /* Longest string to echo */ #define TIMEOUT_SECS 2 /* Seconds between retransmits */ #define MAXTRIES 5 /* Tries before giving up */... (3 Replies)
Discussion started by: sehang
3 Replies

6. UNIX for Advanced & Expert Users

Ksh Create Thread

I think I know the answer but I'll ask and see if someone with more expertise can share new light on this topic: Does Ksh have the capability to create new threads (not processes) and/or offer some multiplexing on named pipes? Thanks for your assistance! ~sreyes27 (2 Replies)
Discussion started by: sreyes27
2 Replies

7. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

8. Shell Programming and Scripting

create variable name based on another variable's value

Hello, I am needing to create a variable and assign it a value based on the value of a previosly defined variable... I am using KSH.. Example: VAR1=COMPUTER1 I need another variable like ${VAR1}_FLAG="Y", so it would actually be COMPUTER1_FLAG="Y". I will be looping through many values in... (2 Replies)
Discussion started by: benefactr
2 Replies

9. HP-UX

create thread C with JNI function with JAVA

Hello, J create a thread C with a JNI function via JAVA. J have the following message (but not in each time): Someone has an idea ? Thank. Unexpected Signal : 4 occurred at PC=0x78C103E0 Function= Library=(N/A) NOTE: We are unable to locate the function name... (0 Replies)
Discussion started by: AUBERT
0 Replies

10. Programming

TZ variable set within thread

Hello all, I'm going to be using some of the date functions from time.h to do some time stamping. I will be getting a time and date from the header of a TIFF file. I will need to be able create a time for each time zone in the U.S. The source of the time stamp will be in GMT. What I'd like to do... (2 Replies)
Discussion started by: shldBcding
2 Replies
Login or Register to Ask a Question